//This function adds a Resource Handler for the pages stats.html and formtest.html and associates stats , form and their callback functions void init_dynamic_content() { indexPage=AmmServer_ReadFileToMemory(indexPagePath,&indexPageLength); if (indexPage==0) { AmmServer_Error("Could not find Index Page file %s ",indexPagePath); exit(0); } AmmServer_AddResourceHandler(default_server,&screenContext,"screen.jpg",512000,400,&prepare_screen_content_callback,SAME_PAGE_FOR_ALL_CLIENTS); AmmServer_AddResourceHandler(default_server,&indexPageContext,"/index.html",4096,0,&prepare_index_content_callback,SAME_PAGE_FOR_ALL_CLIENTS); AmmServer_AddResourceHandler(default_server,&commandContext,"/cmd",4096,0,&prepare_command_content_callback,DIFFERENT_PAGE_FOR_EACH_CLIENT); }
int AmmServer_AddScheduler ( struct AmmServer_Instance * instance, const char * resource_name , void * callback, unsigned int delayMilliseconds, unsigned int repetitions ) { AmmServer_Error("Scheduler Code not implemented\n"); }
int main(int argc, char *argv[]) { printf("\nAmmar Server %s starting up..\n",AmmServer_Version()); //Check binary and header spec AmmServer_CheckIfHeaderBinaryAreTheSame(AMMAR_SERVER_HTTP_HEADER_SPEC); //Register termination signal for when we receive SIGKILL etc AmmServer_RegisterTerminationSignal(&close_dynamic_content); char bindIP[MAX_IP_STRING_SIZE]; strcpy(bindIP,"0.0.0.0"); unsigned int port=DEFAULT_BINDING_PORT; //Kick start AmmarServer , bind the ports , create the threads and get things going..! default_server = AmmServer_StartWithArgs( "MySearch", argc,argv , //The internal server will use the arguments to change settings //If you don't want this look at the AmmServer_Start call bindIP, port, 0, /*This means we don't want a specific configuration file*/ webserver_root, templates_root ); if (!default_server) { AmmServer_Error("Could not start server , shutting down everything.."); exit(1); } //Create dynamic content allocations and associate context to the correct files init_dynamic_content(); //stats.html and formtest.html should be availiable from now on..! while ( (AmmServer_Running(default_server)) ) { //Main thread should just sleep and let the background threads do the hard work..! //In other applications the programmer could use the main thread to do anything he likes.. //The only caveat is that he would takeup more CPU time from the server and that he would have to poll //the AmmServer_Running() call once in a while to make sure everything is in order //usleep(60000); sleep(1); } //Delete dynamic content allocations and remove stats.html and formtest.html from the server close_dynamic_content(); //Stop the server and clean state AmmServer_Stop(default_server); AmmServer_Warning("Ammar Server stopped\n"); return 0; }
int AmmServer_AddResourceHandler ( struct AmmServer_Instance * instance, struct AmmServer_RH_Context * context, const char * resource_name , const char * web_root, unsigned int allocate_mem_bytes, unsigned int callback_every_x_msec, void * callback, unsigned int scenario ) { if ( context->requestContext.content!=0 ) { AmmServer_Warning("Context in AmmServer_AddResourceHandler for %s appears to have an already initialized memory part\n",resource_name); AmmServer_Warning("Make sure that you are using a seperate context for each AmmServer_AddResourceHandler call you make..\n"); } memset(context,0,sizeof(struct AmmServer_RH_Context)); strncpy(context->web_root_path,web_root,MAX_FILE_PATH); strncpy(context->resource_name,resource_name,MAX_RESOURCE); context->requestContext.instance=instance; // Remember the instance that created this.. context->requestContext.MAXcontentSize=allocate_mem_bytes; context->callback_every_x_msec=callback_every_x_msec; context->last_callback=0; //This is important because a random value here will screw up things with callback_every_x_msec.. context->callback_cooldown=0; context->RH_Scenario = scenario; if ( allocate_mem_bytes>0 ) { context->requestContext.content = (char*) malloc( sizeof(char) * allocate_mem_bytes ); if (context->requestContext.content==0) { AmmServer_Warning("Could not allocate space for request Context"); } } context->dynamicRequestCallbackFunction=callback; if (callback==0) { AmmServer_Warning("No callback passed for a new AmmServer_AddResourceHandler "); } int returnValue = cache_AddMemoryBlock(instance,context); if (!returnValue) { AmmServer_Error("Failed adding new resource handler\n Resource name `%s` will be unavailable\n",resource_name); } return returnValue; }
int initAmmarServer() { //Kick start AmmarServer , bind the ports , create the threads and get things going..! default_server = AmmServer_StartWithArgs( "SimpleTemplate", 0,0 , //The internal server will use the arguments to change settings //If you don't want this look at the AmmServer_Start call bindIP, port, 0, /*This means we don't want a specific configuration file*/ webserver_root, templates_root ); if (!default_server) { AmmServer_Error("Could not start server , shutting down everything.."); exit(1); } init_dynamic_content(); }
//Binary Search inline unsigned int Find_longURL(char * shortURL,int * found) { #if USE_BINARY_SEARCH *found = 0; if (shortURL==0) { return 0; } if (loaded_links==0) { return 0; } if (sorted_links!=0) { unsigned long our_hash = hashURL(shortURL); unsigned int binarySearchLastElement = sorted_links; unsigned int beg=0,mid=0,fin=binarySearchLastElement-1; while ( beg <= fin ) { mid=(unsigned int) beg + ( (fin-beg)/2 ); if (mid >= binarySearchLastElement) { AmmServer_Error("Binary Search overflowed ( beg %u mid %u fin %u ) , binarySearchLastElement %u \n",beg,mid,fin,binarySearchLastElement); break; } else if (our_hash<links[mid].shortURLHash) { fin=mid-1; } else if (our_hash>links[mid].shortURLHash) { beg=mid+1; } else { *found = 1; AmmServer_Success("Found %s using binary search\n",shortURL); return mid; } } } //TODO : Remove this message in the future ------------- AmmServer_Warning("Binary Search couldn't find result , extending search to unsorted list\n"); return Find_longURLSerial(shortURL,found); //---------------------------------------- #else // USE_BINARY_SEARCH return Find_longURLSerial(shortURL,found); #endif return 0; }
void * favicon_callback(struct AmmServer_DynamicRequest * rqst) { if (!AmmServer_DynamicRequestReturnFile(rqst,"src/Services/MySearch/res/favicon.ico") ) { AmmServer_Error("Could not return favicon"); } rqst->contentSize=strlen(rqst->content); return 0; }
void * prepare_index_content_callback(struct AmmServer_DynamicRequest * rqst) { if (!AmmServer_DynamicRequestReturnFile(rqst,"src/Services/MySearch/res/search.html") ) { AmmServer_Error("Could not return default thumbnail"); } rqst->contentSize=strlen(rqst->content); return 0; }
//This function prepares the content of random_chars context , ( random_chars.content ) void * prepare_command_content_callback(struct AmmServer_DynamicRequest * rqst) { if (!allowControl) { strcpy(rqst->content,"<html>Disabled</html>"); rqst->contentSize=strlen(rqst->content); return 0; } #if ALLOW_REMOTE_CONTROL unsigned int donothing=0; unsigned int x=0,y=0,doclick=0,do2xclick=0,dokey=0; char xCoord[128]={0}; char yCoord[128]={0}; char commandStr[128]={0}; if ( _GETcpy(rqst,"x",xCoord,128) ) { x=atoi(xCoord); } if ( _GETcpy(rqst,"y",yCoord,128) ) { y=atoi(yCoord); } if ( _GETcpy(rqst,"k",commandStr,128) ) { fprintf(stderr,"Keystroke %s",commandStr); dokey=atoi(commandStr); } if ( _GETcpy(rqst,"click",commandStr,128) ) { doclick=1; } if ( _GETcpy(rqst,"dblclick",commandStr,128) ) { do2xclick=1; } if ( (x>5) || (y>5) ) { fprintf(stderr,"---------------------- MouseMovement \n"); snprintf(commandStr,128,"xdotool mousemove --sync %u %u ",x,y); } else if (dokey) { char keypressedFiltered[32]={0}; char keyPressed = (char) dokey; if (keyPressed<200) { fprintf(stderr,"---------------------- Key (%c) \n",keyPressed); snprintf(keypressedFiltered,32,"%c",keyPressed); if (keyPressed==' ') { snprintf(keypressedFiltered,32,"space"); } else if (keyPressed==' ') { snprintf(keypressedFiltered,10,"Return"); } else if (keyPressed==' ') { snprintf(keypressedFiltered,13,"Return"); } snprintf(commandStr,128,"xdotool key '%s'",keypressedFiltered); } else { donothing=1; } } else if (do2xclick) { fprintf(stderr,"---------------------- 2x Click \n"); snprintf(commandStr,128,"xdotool click 1"); int i=system(commandStr); if (i!=0) { AmmServer_Error("Could not execute %s\n",commandStr); } usleep(1000); } else if (doclick) { fprintf(stderr,"---------------------- Click \n"); snprintf(commandStr,128,"xdotool click 1"); } else { fprintf(stderr,"---------------------- Nothing \n"); donothing=1; } if (!donothing) { int i=system(commandStr); if (i!=0) { AmmServer_Error("Could not execute %s\n",commandStr); } } strcpy(rqst->content,"<html>Ok</html>"); rqst->contentSize=strlen(rqst->content); #else strcpy(rqst->content,"<html>Remote Control Disabled</html>"); rqst->contentSize=strlen(rqst->content); #endif return 0; }