1 /************************************************************************************************* 2 * The utility API of Tokyo Tyrant 3 * Copyright (C) 2006-2010 FAL Labs 4 * This file is part of Tokyo Tyrant. 5 * Tokyo Tyrant is free software; you can redistribute it and/or modify it under the terms of 6 * the GNU Lesser General Public License as published by the Free Software Foundation; either 7 * version 2.1 of the License or any later version. Tokyo Tyrant is distributed in the hope 8 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 10 * License for more details. 11 * You should have received a copy of the GNU Lesser General Public License along with Tokyo 12 * Tyrant; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 13 * Boston, MA 02111-1307 USA. 14 *************************************************************************************************/ 15 16 17 #ifndef _TTUTIL_H /* duplication check */ 18 #define _TTUTIL_H 19 20 #if defined(__cplusplus) 21 #define __TTUTIL_CLINKAGEBEGIN extern "C" { 22 #define __TTUTIL_CLINKAGEEND } 23 #else 24 #define __TTUTIL_CLINKAGEBEGIN 25 #define __TTUTIL_CLINKAGEEND 26 #endif 27 __TTUTIL_CLINKAGEBEGIN 28 29 30 #include <tcutil.h> 31 #include <tchdb.h> 32 #include <tcbdb.h> 33 #include <tcfdb.h> 34 #include <tctdb.h> 35 #include <tcadb.h> 36 #include <pthread.h> 37 38 39 40 /************************************************************************************************* 41 * basic utilities 42 *************************************************************************************************/ 43 44 45 #define TTIOBUFSIZ 65536 /* size of an I/O buffer */ 46 #define TTADDRBUFSIZ 1024 /* size of an address buffer */ 47 48 typedef struct { /* type of structure for a socket */ 49 int fd; /* file descriptor */ 50 char buf[TTIOBUFSIZ]; /* reading buffer */ 51 char *rp; /* reading pointer */ 52 char *ep; /* end pointer */ 53 bool end; /* end flag */ 54 double to; /* timeout */ 55 double dl; /* deadline time */ 56 } TTSOCK; 57 58 59 /* String containing the version information. */ 60 extern const char *ttversion; 61 62 63 /* Get the primary name of the local host. 64 `name' specifies the pointer to the region into which the host name is written. The size of 65 the buffer should be equal to or more than `TTADDRBUFSIZ' bytes. 66 If successful, the return value is true, else, it is false. */ 67 bool ttgetlocalhostname(char *name); 68 69 70 /* Get the address of a host. 71 `name' specifies the name of the host. 72 `addr' specifies the pointer to the region into which the address is written. The size of the 73 buffer should be equal to or more than `TTADDRBUFSIZ' bytes. 74 If successful, the return value is true, else, it is false. */ 75 bool ttgethostaddr(const char *name, char *addr); 76 77 78 /* Open a client socket of TCP/IP stream to a server. 79 `addr' specifies the address of the server. 80 `port' specifies the port number of the server. 81 The return value is the file descriptor of the stream, or -1 on error. */ 82 int ttopensock(const char *addr, int port); 83 84 85 /* Open a client socket of UNIX domain stream to a server. 86 `path' specifies the path of the socket file. 87 The return value is the file descriptor of the stream, or -1 on error. */ 88 int ttopensockunix(const char *path); 89 90 91 /* Open a server socket of TCP/IP stream to clients. 92 `addr' specifies the address of the server. If it is `NULL', every network address is binded. 93 `port' specifies the port number of the server. 94 The return value is the file descriptor of the stream, or -1 on error. */ 95 int ttopenservsock(const char *addr, int port); 96 97 98 /* Open a server socket of UNIX domain stream to clients. 99 `addr' specifies the address of the server. If it is `NULL', every network address is binded. 100 `port' specifies the port number of the server. 101 The return value is the file descriptor of the stream, or -1 on error. */ 102 int ttopenservsockunix(const char *path); 103 104 105 /* Accept a TCP/IP connection from a client. 106 `fd' specifies the file descriptor. 107 `addr' specifies the pointer to the region into which the client address is written. The size 108 of the buffer should be equal to or more than `TTADDRBUFSIZ' bytes. 109 `pp' specifies the pointer to a variable to which the client port is assigned. If it is 110 `NULL', it is not used. 111 The return value is the file descriptor of the stream, or -1 on error. */ 112 int ttacceptsock(int fd, char *addr, int *pp); 113 114 115 /* Accept a UNIX domain connection from a client. 116 `fd' specifies the file descriptor. 117 The return value is the file descriptor of the stream, or -1 on error. */ 118 int ttacceptsockunix(int fd); 119 120 121 /* Shutdown and close a socket. 122 `fd' specifies the file descriptor. 123 If successful, the return value is true, else, it is false. */ 124 bool ttclosesock(int fd); 125 126 127 /* Wait an I/O event of a socket. 128 `fd' specifies the file descriptor. 129 `mode' specifies the kind of events; 0 for reading, 1 for writing, 2 for exception. 130 `timeout' specifies the timeout in seconds. 131 If successful, the return value is true, else, it is false. */ 132 bool ttwaitsock(int fd, int mode, double timeout); 133 134 135 /* Create a socket object. 136 `fd' specifies the file descriptor. 137 The return value is the socket object. */ 138 TTSOCK *ttsocknew(int fd); 139 140 141 /* Delete a socket object. 142 `sock' specifies the socket object. */ 143 void ttsockdel(TTSOCK *sock); 144 145 146 /* Set the lifetime of a socket object. 147 `sock' specifies the socket object. 148 `lifetime' specifies the lifetime seconds of each task. By default, there is no limit. */ 149 void ttsocksetlife(TTSOCK *sock, double lifetime); 150 151 152 /* Send data by a socket. 153 `sock' specifies the socket object. 154 `buf' specifies the pointer to the region of the data to send. 155 `size' specifies the size of the buffer. 156 If successful, the return value is true, else, it is false. */ 157 bool ttsocksend(TTSOCK *sock, const void *buf, int size); 158 159 160 /* Send formatted data by a socket. 161 `sock' specifies the socket object. 162 `format' specifies the printf-like format string. 163 The conversion character `%' can be used with such flag characters as `s', `d', `o', `u', 164 `x', `X', `c', `e', `E', `f', `g', `G', `@', `?', `%'. `@' works as with `s' but escapes meta 165 characters of XML. `?' works as with `s' but escapes meta characters of URL. The other 166 conversion character work as with each original. 167 The other arguments are used according to the format string. 168 If successful, the return value is true, else, it is false. */ 169 bool ttsockprintf(TTSOCK *sock, const char *format, ...); 170 171 172 /* Receive data by a socket. 173 `sock' specifies the socket object. 174 `buf' specifies the pointer to the region of the data to be received. 175 `size' specifies the size of the buffer. 176 If successful, the return value is true, else, it is false. False is returned if the socket 177 is closed before receiving the specified size of data. */ 178 bool ttsockrecv(TTSOCK *sock, char *buf, int size); 179 180 181 /* Receive one byte by a socket. 182 `sock' specifies the socket object. 183 The return value is the received byte. If some error occurs or the socket is closed by the 184 server, -1 is returned. */ 185 int ttsockgetc(TTSOCK *sock); 186 187 188 /* Push a character back to a socket. 189 `sock' specifies the socket object. 190 `c' specifies the character. */ 191 void ttsockungetc(TTSOCK *sock, int c); 192 193 194 /* Receive one line by a socket. 195 `sock' specifies the socket object. 196 `buf' specifies the pointer to the region of the data to be received. 197 `size' specifies the size of the buffer. 198 If successful, the return value is true, else, it is false. False is returned if the socket 199 is closed before receiving linefeed. */ 200 bool ttsockgets(TTSOCK *sock, char *buf, int size); 201 202 203 /* Receive one line by a socket into allocated buffer. 204 `sock' specifies the socket object. 205 If successful, the return value is the pointer to the result buffer, else, it is `NULL'. 206 `NULL' is returned if the socket is closed before receiving linefeed. 207 Because the region of the return value is allocated with the `malloc' call, it should be 208 released with the `free' call when it is no longer in use. */ 209 char *ttsockgets2(TTSOCK *sock); 210 211 212 /* Receive an 32-bit integer by a socket. 213 `sock' specifies the socket object. 214 The return value is the 32-bit integer. */ 215 uint32_t ttsockgetint32(TTSOCK *sock); 216 217 218 /* Receive an 64-bit integer by a socket. 219 `sock' specifies the socket object. 220 The return value is the 64-bit integer. */ 221 uint64_t ttsockgetint64(TTSOCK *sock); 222 223 224 /* Check whehter a socket is end. 225 `sock' specifies the socket object. 226 The return value is true if the socket is end, else, it is false. */ 227 bool ttsockcheckend(TTSOCK *sock); 228 229 230 /* Check the size of prefetched data in a socket. 231 `sock' specifies the socket object. 232 The return value is the size of the prefetched data. */ 233 int ttsockcheckpfsiz(TTSOCK *sock); 234 235 236 /* Fetch the resource of a URL by HTTP. 237 `url' specifies the URL. 238 `reqheads' specifies a map object contains request header names and their values. The header 239 "X-TT-Timeout" specifies the timeout in seconds. If it is `NULL', it is not used. 240 `resheads' specifies a map object to store response headers their values. If it is NULL, it 241 is not used. Each key of the map is an uncapitalized header name. The key "STATUS" means the 242 status line. 243 `resbody' specifies a extensible string object to store the entity body of the result. If it 244 is `NULL', it is not used. 245 The return value is the response code or -1 on network error. */ 246 int tthttpfetch(const char *url, TCMAP *reqheads, TCMAP *resheads, TCXSTR *resbody); 247 248 249 /* Serialize a real number. 250 `num' specifies a real number. 251 `buf' specifies the pointer to the region into which the result is written. The size of the 252 buffer should be 16 bytes. */ 253 void ttpackdouble(double num, char *buf); 254 255 256 /* Redintegrate a serialized real number. 257 `buf' specifies the pointer to the region of the serialized real number. The size of the 258 buffer should be 16 bytes. 259 The return value is the original real number. */ 260 double ttunpackdouble(const char *buf); 261 262 263 264 /************************************************************************************************* 265 * server utilities 266 *************************************************************************************************/ 267 268 269 #define TTDEFPORT 1978 /* default port of the server */ 270 #define TTMAGICNUM 0xc8 /* magic number of each command */ 271 #define TTCMDPUT 0x10 /* ID of put command */ 272 #define TTCMDPUTKEEP 0x11 /* ID of putkeep command */ 273 #define TTCMDPUTCAT 0x12 /* ID of putcat command */ 274 #define TTCMDPUTSHL 0x13 /* ID of putshl command */ 275 #define TTCMDPUTNR 0x18 /* ID of putnr command */ 276 #define TTCMDOUT 0x20 /* ID of out command */ 277 #define TTCMDGET 0x30 /* ID of get command */ 278 #define TTCMDMGET 0x31 /* ID of mget command */ 279 #define TTCMDVSIZ 0x38 /* ID of vsiz command */ 280 #define TTCMDITERINIT 0x50 /* ID of iterinit command */ 281 #define TTCMDITERNEXT 0x51 /* ID of iternext command */ 282 #define TTCMDFWMKEYS 0x58 /* ID of fwmkeys command */ 283 #define TTCMDADDINT 0x60 /* ID of addint command */ 284 #define TTCMDADDDOUBLE 0x61 /* ID of adddouble command */ 285 #define TTCMDEXT 0x68 /* ID of ext command */ 286 #define TTCMDSYNC 0x70 /* ID of sync command */ 287 #define TTCMDOPTIMIZE 0x71 /* ID of optimize command */ 288 #define TTCMDVANISH 0x72 /* ID of vanish command */ 289 #define TTCMDCOPY 0x73 /* ID of copy command */ 290 #define TTCMDRESTORE 0x74 /* ID of restore command */ 291 #define TTCMDSETMST 0x78 /* ID of setmst command */ 292 #define TTCMDRNUM 0x80 /* ID of rnum command */ 293 #define TTCMDSIZE 0x81 /* ID of size command */ 294 #define TTCMDSTAT 0x88 /* ID of stat command */ 295 #define TTCMDMISC 0x90 /* ID of misc command */ 296 #define TTCMDREPL 0xa0 /* ID of repl command */ 297 298 #define TTTIMERMAX 8 /* maximum number of timers */ 299 300 typedef struct _TTTIMER { /* type of structure for a timer */ 301 pthread_t thid; /* thread ID */ 302 bool alive; /* alive flag */ 303 struct _TTSERV *serv; /* server object */ 304 double freq_timed; /* frequency of timed handler */ 305 void (*do_timed)(void *); /* call back function for timed handler */ 306 void *opq_timed; /* opaque pointer for timed handler */ 307 } TTTIMER; 308 309 typedef struct _TTREQ { /* type of structure for a server */ 310 pthread_t thid; /* thread ID */ 311 bool alive; /* alive flag */ 312 struct _TTSERV *serv; /* server object */ 313 int epfd; /* polling file descriptor */ 314 double mtime; /* last modified time */ 315 bool keep; /* keep-alive flag */ 316 int idx; /* ordinal index */ 317 } TTREQ; 318 319 typedef struct _TTSERV { /* type of structure for a server */ 320 char host[TTADDRBUFSIZ]; /* host name */ 321 char addr[TTADDRBUFSIZ]; /* host address */ 322 uint16_t port; /* port number */ 323 TCLIST *queue; /* queue of requests */ 324 pthread_mutex_t qmtx; /* mutex for the queue */ 325 pthread_cond_t qcnd; /* condition variable for the queue */ 326 pthread_mutex_t tmtx; /* mutex for the timer */ 327 pthread_cond_t tcnd; /* condition variable for the timer */ 328 int thnum; /* number of threads */ 329 double timeout; /* timeout milliseconds of each task */ 330 bool term; /* terminate flag */ 331 void (*do_log)(int, const char *, void *); /* call back function for logging */ 332 void *opq_log; /* opaque pointer for logging */ 333 TTTIMER timers[TTTIMERMAX]; /* timer objects */ 334 int timernum; /* number of timer objects */ 335 void (*do_task)(TTSOCK *, void *, TTREQ *); /* call back function for task */ 336 void *opq_task; /* opaque pointer for task */ 337 void (*do_term)(void *); /* call back gunction for termination */ 338 void *opq_term; /* opaque pointer for termination */ 339 } TTSERV; 340 341 enum { /* enumeration for logging levels */ 342 TTLOGDEBUG, /* debug */ 343 TTLOGINFO, /* information */ 344 TTLOGERROR, /* error */ 345 TTLOGSYSTEM /* system */ 346 }; 347 348 349 /* Create a server object. 350 The return value is the server object. */ 351 TTSERV *ttservnew(void); 352 353 354 /* Delete a server object. 355 `serv' specifies the server object. */ 356 void ttservdel(TTSERV *serv); 357 358 359 /* Configure a server object. 360 `serv' specifies the server object. 361 `host' specifies the name or the address. If it is `NULL', If it is `NULL', every network 362 address is binded. 363 `port' specifies the port number. If it is not less than 0, UNIX domain socket is binded and 364 the host name is treated as the path of the socket file. 365 If successful, the return value is true, else, it is false. */ 366 bool ttservconf(TTSERV *serv, const char *host, int port); 367 368 369 /* Set tuning parameters of a server object. 370 `serv' specifies the server object. 371 `thnum' specifies the number of worker threads. By default, the number is 5. 372 `timeout' specifies the timeout seconds of each task. If it is not more than 0, no timeout is 373 specified. By default, there is no timeout. */ 374 void ttservtune(TTSERV *serv, int thnum, double timeout); 375 376 377 /* Set the logging handler of a server object. 378 `serv' specifies the server object. 379 `do_log' specifies the pointer to a function to do with a log message. Its first parameter is 380 the log level, one of `TTLOGDEBUG', `TTLOGINFO', `TTLOGERROR'. Its second parameter is the 381 message string. Its third parameter is the opaque pointer. 382 `opq' specifies the opaque pointer to be passed to the handler. It can be `NULL'. */ 383 void ttservsetloghandler(TTSERV *serv, void (*do_log)(int, const char *, void *), void *opq); 384 385 386 /* Add a timed handler to a server object. 387 `serv' specifies the server object. 388 `freq' specifies the frequency of execution in seconds. 389 `do_timed' specifies the pointer to a function to do with a event. Its parameter is the 390 opaque pointer. 391 `opq' specifies the opaque pointer to be passed to the handler. It can be `NULL'. */ 392 void ttservaddtimedhandler(TTSERV *serv, double freq, void (*do_timed)(void *), void *opq); 393 394 395 /* Set the response handler of a server object. 396 `serv' specifies the server object. 397 `do_task' specifies the pointer to a function to do with a task. Its first parameter is 398 the socket object connected to the client. Its second parameter is the opaque pointer. Its 399 third parameter is the request object. 400 `opq' specifies the opaque pointer to be passed to the handler. It can be `NULL'. */ 401 void ttservsettaskhandler(TTSERV *serv, void (*do_task)(TTSOCK *, void *, TTREQ *), void *opq); 402 403 404 /* Set the termination handler of a server object. 405 `serv' specifies the server object. 406 `do_term' specifies the pointer to a function to do with a task. Its parameter is the opaque 407 pointer. 408 `opq' specifies the opaque pointer to be passed to the handler. It can be `NULL'. */ 409 void ttservsettermhandler(TTSERV *serv, void (*do_term)(void *), void *opq); 410 411 412 /* Start the service of a server object. 413 `serv' specifies the server object. 414 If successful, the return value is true, else, it is false. */ 415 bool ttservstart(TTSERV *serv); 416 417 418 /* Send the terminate signal to a server object. 419 `serv' specifies the server object. 420 If successful, the return value is true, else, it is false. */ 421 bool ttservkill(TTSERV *serv); 422 423 424 /* Call the logging function of a server object. 425 `serv' specifies the server object. 426 `level' specifies the logging level. 427 `format' specifies the message format. 428 The other arguments are used according to the format string. */ 429 void ttservlog(TTSERV *serv, int level, const char *format, ...); 430 431 432 /* Check whether a server object is killed. 433 `serv' specifies the server object. 434 The return value is true if the server is killed, or false if not. */ 435 bool ttserviskilled(TTSERV *serv); 436 437 438 /* Break a simple server expression. 439 `expr' specifies the simple server expression. It is composed of two substrings separated 440 by ":". The former field specifies the name or the address of the server. The latter field 441 specifies the port number. If the latter field is omitted, the default port number is 442 specified. 443 `pp' specifies the pointer to a variable to which the port number is assigned. If it is 444 `NULL', it is not used. 445 The return value is the string of the host name. 446 Because the region of the return value is allocated with the `malloc' call, it should be 447 released with the `free' call when it is no longer in use. */ 448 char *ttbreakservexpr(const char *expr, int *pp); 449 450 451 452 /************************************************************************************************* 453 * features for experts 454 *************************************************************************************************/ 455 456 457 #define _TT_VERSION "1.1.41" 458 #define _TT_LIBVER 324 459 #define _TT_PROTVER "0.91" 460 461 462 /* Switch the process into the background. 463 If successful, the return value is true, else, it is false. */ 464 bool ttdaemonize(void); 465 466 467 /* Get the load average of the system. 468 The return value is the load average of the system. */ 469 double ttgetloadavg(void); 470 471 472 /* Convert a string to a time stamp. 473 `str' specifies the string. 474 The return value is the time stamp. */ 475 uint64_t ttstrtots(const char *str); 476 477 478 /* Get the command name of a command ID number. 479 `id' specifies the command ID number. 480 The return value is the string of the command name. */ 481 const char *ttcmdidtostr(int id); 482 483 484 /* tricks for backward compatibility */ 485 #define tcrdbqrysetmax(TC_tdb, TC_max) \ 486 tcrdbqrysetlimit((TC_tdb), (TC_max), 0) 487 488 489 490 __TTUTIL_CLINKAGEEND 491 #endif /* duplication check */ 492 493 494 /* END OF FILE */ 495