00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef TUPLE_H_INCLUDED
00036 #define TUPLE_H_INCLUDED
00037
00038 #ifndef va_list
00039 #define va_list __gnuc_va_list
00040 #endif
00041
00042 #ifdef DEBUG
00043 #define _DBG_(x) x
00044 #else
00045 #define _DBG_(x)
00046 #endif
00047
00048 #define ASSERT(cond) \
00049 _DBG_(if (!(cond)) { DBGPRINTF("assert failed: %s\n", \
00050 #cond); exit(1); })
00051
00052 #define DBGPRINTF(fmt,a...) \
00053 _DBG_(fprintf(stderr, "%d " __FILE__ ":%d " fmt, \
00054 debug_counter++, __LINE__, ##a))
00055
00056 #define TELL(x) \
00057 DBGPRINTF(#x " = %d\n", x)
00058
00059 #if defined(DEBUG)
00060 static inline void * mymalloc(int size, char *file, int line)
00061 {
00062 void *p;
00063 p = malloc(size);
00064 fprintf(stderr, "%s:%d malloc %08x\n",
00065 file, line, (unsigned int) p);
00066 return p;
00067 }
00068 static inline void myfree(void *p, char *file, int line)
00069 {
00070 fprintf(stderr, "%s:%d free %08x\n",
00071 file, line, (unsigned int) p);
00072 free(p);
00073 }
00074 #define MALLOC(n) mymalloc(n,__FILE__,__LINE__)
00075 #define FREE(n) myfree(n,__FILE__,__LINE__)
00076 #else
00077 #define MALLOC malloc
00078 #define FREE free
00079 #endif
00080
00081 #ifdef DEBUG
00082
00083 #define EXIT() *((int*) 0) = 11
00084 #else
00085 #define EXIT() exit(1)
00086 #endif
00087
00088
00089
00090 #define TIMEVAL_DIFF(before,after) \
00091 ((after.tv_sec - before.tv_sec) + \
00092 1.0e-6 * (after.tv_usec - before.tv_usec))
00093
00094
00095
00096
00097 #define SERVERNAME_ENVVAR "LINUXTUPLES_HOST"
00098 #define PORTNUMBER_ENVVAR "LINUXTUPLES_PORT"
00099
00100 enum message_op {
00101 PUT = 0,
00102 GET = 1,
00103 READ = 2,
00104 GET_NB = 3,
00105 READ_NB = 4,
00106 DUMP = 5,
00107 LOG = 6,
00108 };
00109
00114 struct element {
00131 int tag;
00137 union {
00142 int i;
00147 double d;
00154 struct {
00158 char *ptr;
00166 int len;
00167 } s;
00168 } data;
00169 };
00170
00174 #define I_AM_A_TUPLE 0x31415926
00175
00181 struct tuple {
00192 int tag;
00196 int num_elts;
00201 int string_length;
00205 struct element *elements;
00215 char *string_space;
00216 };
00217
00222 struct tuple_list {
00223 struct tuple_list *next;
00224 struct tuple *tup;
00225 };
00226
00227
00237 struct context {
00242 char servername[100];
00247 int portnumber;
00251 int sock;
00257 pthread_t thr;
00258 };
00259
00260
00261
00262
00263
00264 #ifdef DEBUG
00265 extern int debug_counter;
00266 #endif
00267 extern int i_am_server;
00268 extern char logbuf[8192];
00269 extern int logptr;
00270 extern int get_server_portnumber(struct context *ctx);
00271 extern void print_element(struct element *e);
00272 extern void print_tuple(struct tuple *s);
00273 extern struct tuple *make_tuple(char *fmt, ...);
00274 extern void destroy_tuple(struct tuple *t);
00275 extern int tuples_match(struct tuple *s, struct tuple *t);
00276 extern unsigned int random_int(void);
00277 extern int send_chunk(struct context *ctx, char *buf, int bytes_to_send);
00278 extern int send_tuple(struct context *ctx, struct tuple *t);
00279 extern int recv_chunk(struct context *ctx, char *buf, int size);
00280 extern struct tuple *recv_tuple(struct context *ctx);
00281 extern int put_tuple(struct tuple *s, struct context *ctx);
00282 extern struct tuple *get_tuple(struct tuple *s, struct context *ctx);
00283 extern struct tuple *read_tuple(struct tuple *s, struct context *ctx);
00284 extern struct tuple *get_nb_tuple(struct tuple *s, struct context *ctx);
00285 extern struct tuple *read_nb_tuple(struct tuple *s, struct context *ctx);
00286 extern struct tuple_list *dump_tuple_space(struct tuple_list *templates,
00287 struct context *ctx);
00288 extern int tuple_server_log(FILE *stream, struct context *ctx);
00289 extern int tuple_int_field(struct tuple *s, int n);
00290 extern double tuple_double_field(struct tuple *s, int n);
00291 extern char *tuple_string_field(struct tuple *s, int *len, int n);
00292
00293 #endif