src/main/EventServer.cpp File Reference

server based on events More...

#include "EventServer.h"
#include "LightString.h"

Go to the source code of this file.

Defines

#define SHORTEST_CMD   "get "
 shortest command.
#define BSWAP_64(x)
 Reverses the 64bit numeric (used in binaryHandler function only).
#define INVERT_HEADER(header)
 Reverses the request header (used in binaryHandler function only).
#define INVERT_SET_HEADER_PART(header)
 Reverses the set request header (used in binaryHandler function only).
#define __STAT(_arg, format, value)
 Prepares single stats entry (used in getStat function only).
#define __STAT2(_arg, format, value, value2)
 Prepares single stats entry (used in getStat function only).
#define __CHECK_STATE(s, st)
 Checks if request is of given type (used in parseState function only).
#define __CHECK_OPT_N(opt, var)
 Extracts configuration option (number) (used in loadConfFile function only).
#define __CHECK_OPT_S(opt, var)
 Extracts configuration option (string) (used in loadConfFile function only).
#define __COMMAND(__code, __text)
 Prints type of request in human readable form (binary protocol debugging) (used in printBinaryCommand function only).

Functions

int main (int argc, char **argv)
 Server's main function.
void acceptCallback (int serverSocket, short event, void *arg)
 accepting callback
void pipeCallback (int fd, short event, void *arg)
 hollow callback.
void readCallback (int socket, short event, void *arg)
 whole parsing of the clients' input.
void binaryHandler (Info *i)
 Provides binary protocol parsing and executes methods from libdreamcache library as well as generates response and tries to send it immediately.
void asciiHandler (Info *i)
 Provides text protocol parsing and executes methods from libdreamcache library as well as generates response and tries to send it immediately.
void writeCallback (int socket, short event, void *arg)
 function that cares about sending response to clients.
void prepareStatBinaryResponse (request_header *header, Info *i)
 Prepares binary stats request response.
int getStat (Stats *stats, const char *arg, char *buffer, int maxLength)
 Prepares stats request response.
void * threadWorker (void *arg)
 threads main body (which is simply event_base_loop())
void resetBuffer (Info *i)
 when many requests are sent in one package and there is too little place for a new request pending ones are copied to the beginning of i->buffer.
void parseState (Info *i)
 reads i->buffer at i->cursor position and determines the command which was sent by a client.
void loadConfFile (const string &serverConfFilename)
 reads configuration file and stored data into global variables
void printBinaryBuffer (void *buffer, int n, const char *label)
 For debug purpose only.
void printBinaryCommand (uint8_t cmd)
 For debug purpose only.
void freeQueue (Info *i)
 function that frees the reponse queue
void signalHandler (int n)
 This method is called when server receives signals.
void daemonize ()
 demonizes server


Detailed Description

server based on events

Definition in file EventServer.cpp.


Define Documentation

#define __CHECK_OPT_N ( opt,
var   ) 

Value:

if (++index + 1 && !s.compare(0, sizeof(opt), opt "=")) { \
                var = extractValue(s, sizeof(opt), serverConfFilename, line); \
        } else
Extracts configuration option (number) (used in loadConfFile function only).

#define __CHECK_OPT_S ( opt,
var   ) 

Value:

if (++index + 1 && !s.compare(0, sizeof(opt), opt "=")) { \
                var = s.substr(sizeof(opt)); \
        } else
Extracts configuration option (string) (used in loadConfFile function only).

#define __CHECK_STATE ( s,
st   ) 

Value:

if (*((int*)(i->buffer + i->cursor)) == *((int*)(s))) { \
        if (st == GET_HEADER || st <= ADD_HEADER){ \
                i->state = st; \
                i->cursor += 4; \
                return; \
        } else if (!strncmp(i->buffer+i->cursor+4, s+4, \
                        sizeof(s)-5)){ \
                i->state = st; \
                i->cursor += sizeof(s)-1; \
                return; \
        } \
}
Checks if request is of given type (used in parseState function only).

#define __COMMAND ( __code,
__text   ) 

Value:

if (cmd == __code) { \
        printf(__text); \
}
Prints type of request in human readable form (binary protocol debugging) (used in printBinaryCommand function only).

#define __STAT ( _arg,
format,
value   ) 

Value:

if (!strcmp(arg, _arg)) { \
        return snprintf(buffer, maxLength, format, value); \
}
Prepares single stats entry (used in getStat function only).

#define __STAT2 ( _arg,
format,
value,
value2   ) 

Value:

if (!strcmp(arg, _arg)) { \
        return snprintf(buffer, maxLength, format, value, value2); \
}
Prepares single stats entry (used in getStat function only).

#define BSWAP_64 (  ) 

Value:

src = (struct trick *) &x; \
        tmp = src->l; \
        src->l = htonl(src->r); \
        src->r = htonl(tmp);
Reverses the 64bit numeric (used in binaryHandler function only).

#define INVERT_HEADER ( header   ) 

Value:

header->header.bodylen = ntohl(header->header.bodylen); \
        header->header.keylen = ntohs(header->header.keylen); \
        BSWAP_64(header->header.cas);
Reverses the request header (used in binaryHandler function only).

#define INVERT_SET_HEADER_PART ( header   ) 

Value:

header->set_header.expiration = ntohl(header->set_header.expiration); \
        header->set_header.flags = ntohl(header->set_header.flags);
Reverses the set request header (used in binaryHandler function only).


Function Documentation

void parseState ( Info i  )  [inline]

reads i->buffer at i->cursor position and determines the command which was sent by a client.

It modifies i->state and i->cursor

Definition at line 1475 of file EventServer.cpp.

void pipeCallback ( int  fd,
short  event,
void *  arg 
) [inline]

hollow callback.

Used to avoid event_base_loop from ever returning

Definition at line 250 of file EventServer.cpp.

void printBinaryBuffer ( void *  buffer,
int  n,
const char *  label = NULL 
)

For debug purpose only.

Prints binary buffer's content in programmer - friendly form.

Definition at line 1583 of file EventServer.cpp.

void printBinaryCommand ( uint8_t  cmd  ) 

For debug purpose only.

Prints request type in human-readable form.

Definition at line 1617 of file EventServer.cpp.

void readCallback ( int  socket,
short  event,
void *  arg 
) [inline]

whole parsing of the clients' input.

90% of server's job is done in this function. To lessen the number of calls to event_del and event_add we invoke one send() at the end of readCallback. If this will send whole response we do not have to register writeCallback (which is almost always)

Definition at line 292 of file EventServer.cpp.

void resetBuffer ( Info i  )  [inline]

when many requests are sent in one package and there is too little place for a new request pending ones are copied to the beginning of i->buffer.

If the amount of free space in i->buffer is sufficient copying is not done. In case if whole recieved message was parsed i->cursor and i->offset are reset.

Definition at line 1462 of file EventServer.cpp.

void writeCallback ( int  socket,
short  event,
void *  arg 
) [inline]

function that cares about sending response to clients.

It is used only when send() at the end of the readCallback() will not send whole message.

Definition at line 1310 of file EventServer.cpp.


Generated on Fri Jan 8 10:38:13 2010 for dreamcache by  doxygen 1.5.5