// ============================================================================ // C servlet sample for the G-WAN Web Application Server (http://trustleap.ch/) // ---------------------------------------------------------------------------- // persistence.c: using G-WAN's persistence pointers to attach data structures // that can be shared between G-WAN scripts, handlers, etc. // ============================================================================ #include "gwan.h" // G-WAN exported functions #include #include // ---------------------------------------------------------------------------- // Server, Handler and VirtualHost persistence pointers (see gwan.h): // US_SERVER_DATA, // G-WAN-wide global pointer (for maintenance script) // US_HANDLER_DATA=200, // Listener-wide pointer // US_VHOST_DATA, // Virtual-Host-wide pointer // ---------------------------------------------------------------------------- typedef struct { kv_t *kv; // a Key-Value store char *blah; // a string int val; // a counter } data_t; // ---------------------------------------------------------------------------- int main(int argc, char *argv[]) { // get the Handler persistent pointer to attach anything you need //data_t **data = (data_t**)get_env(argv, US_HANDLER_DATA); //data_t **data = (data_t**)get_env(argv, US_VHOST_DATA); data_t **data = (data_t**)get_env(argv, US_SERVER_DATA); xbuf_t *reply = get_reply(argv); xbuf_cat(reply, "

Using G-WAN's persistence Pointers

"); if(!data[0]) // first time: persistent pointer is uninitialized { data[0] = (data_t*)calloc(1, sizeof(data_t)); if(!data[0]) return 500; // out of memory xbuf_cat(reply, "initialized data
"); } // not thread-safe: you should either use an atomic operation // or // a counter per G-WAN worker thread; use gettid() to find who's who) data[0]->val++; xbuf_xcat(reply, "Value: %d", data[0]->val); return 200; // return an HTTP code (200:'OK') } // ============================================================================ // End of Source Code // ============================================================================