pcunite

node.native fantasy pseudo

Nov 22nd, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. // This is not real code. Fantasy pseudo
  2. // Read the following functions in this order:
  3. // main(), onClient(), Route(), then aboutFunc()
  4.  
  5.  
  6. #include "node_native_amalgamation.c"
  7.  
  8.  
  9. // aboutFunc
  10. // Optional function - Created by the user of our library
  11. static int aboutFunc(pClient)
  12. {
  13.     // we could simply reply with a file from the RAM Cache
  14.     // pClient->Write(MyCachedHTMLFile);
  15.     // return INT_HANDLED;
  16.    
  17.  
  18.     // or we need to run a long process. So, let's add our function to a message (threaded) queue.
  19.    
  20.     // 1. setup a new queue item
  21.     NNQUEUE * pQueueItem = new NNQUEUE;
  22.  
  23.     // 2. Optionally add a custom object to it
  24.     Custom_t * pCustom = new Custom_t;
  25.     pCustom.str        = "something";
  26.     pQueueItem.parm    = pCustom;
  27.    
  28.     // 3. Add the lengthy function to be called in a thread. Required
  29.     pQueueItem.function = aboutFunc2();
  30.  
  31.     // 4. Add to the message queue. Returns immediately. Will clean up itself.
  32.     pClient.AddQueuePool(pQueueItem);
  33.  
  34.     // return this so that the caller does not call pClient->Wait();
  35.     return INT_HANDLED_VIA_THREAD;
  36. }
  37.  
  38.  
  39. // Route
  40. // Required function - Maps a URL to a function
  41. static int NNHTTP::Route(NNCLIENT* pClient)
  42. {
  43.     // read in the data
  44.     std::string sResponse = pClient->Read();
  45.  
  46.     if(sResponse == "/about")
  47.     {
  48.         return aboutFunc(pClient);
  49.     }
  50.     // etc ...
  51. }
  52.  
  53.  
  54. // onClient
  55. // Required function - Respond to a new client. Called automatically via the handleClients() thread
  56. static void NNHTTP::onClient(NNHTTP* pMyServer)
  57. {
  58.     // Accept the connection
  59.     NNCLIENT * pClient = new NNCLIENT;
  60.     pMyServer->Accept(pClient);
  61.  
  62.     // You could parse the string manually. Instead we'll see if we have a route for it and let it
  63.     // call our mapped function.
  64.     if(pMyServer->Route(pClient) != INT_HANDLED)
  65.     {
  66.         // we don't recognize the URL. We could close the connection, call a 40* function
  67.         // or just keep it persistent.
  68.         // pClient->Close();
  69.     }
  70.  
  71.     // processed Client command. Keep it persistent by waiting for more URL commands.
  72.     if(pClient->Wait() == true)
  73.     {
  74.         pMyServer->Route(pClient);
  75.     }
  76.     else
  77.     {
  78.         pClient->Close();
  79.     }
  80. }
  81.  
  82.  
  83. int main()
  84. {
  85.     // instantiate node.native server and read in configuration
  86.     myServer NNHTTP;
  87.     myServer.loadCfg("myDomain2.com","/path/config.ini");
  88.  
  89.     // optionally run as service or daemon
  90.     myServer.asService("MySvcName");
  91.  
  92.     // Handle clients. This call actually runs our NNHTTP in a separate thread. It will call
  93.     // onClient() for every client event. Easy API.
  94.     myServer.handleClients();
  95.  
  96.     // wait for the signal to shut down
  97.     myServer.asServiceStop();
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment