void serverloop(void *_args) { int32_t n; #ifdef INSIDE_MGW int32_t make_MGWbus(uint16_t port,char *bindaddr,char serverips[MAX_MGWSERVERS][64],int32_t n); int32_t mgw_processbus(char *retbuf,char *jsonstr,cJSON *json); int32_t len; char retbuf[8192],*jsonstr; cJSON *json; if ( SUPERNET.gatewayid >= 0 ) MGW.all.socks.both.bus = make_MGWbus(MGW.port,SUPERNET.myipaddr,MGW.serverips,SUPERNET.numgateways+1*0); #endif sleep(3); printf("start serverloop\n"); while ( OS_getppid() == SUPERNET.ppid ) { #ifdef INSIDE_MGW if ( SUPERNET.gatewayid >= 0 && (len= nn_recv(MGW.all.socks.both.bus,&jsonstr,NN_MSG,0)) > 0 ) { if ( (json= cJSON_Parse(jsonstr)) != 0 ) { mgw_processbus(retbuf,jsonstr,json); free_json(json); } //printf("MGW bus recv.%d json.%p\n",len,json); nn_freemsg(jsonstr); } #endif n = busdata_poll(); if ( n == 0 && SUPERNET.APISLEEP > 0 ) msleep(SUPERNET.APISLEEP); } printf("finished serverloop\n"); }
int32_t PLUGNAME(_process_json)(char *forwarder,char *sender,int32_t valid,struct plugin_info *plugin,uint64_t tag,char *retbuf,int32_t maxlen,char *jsonstr,cJSON *json,int32_t initflag,char *tokenstr) { char *SuperNET_install(char *plugin,char *jsonstr,cJSON *json); char *retstr,*resultstr,*methodstr,*destplugin; int32_t len; retbuf[0] = 0; //printf("<<<<<<<<<<<< INSIDE PLUGIN.(%s)! (%s) initflag.%d process %s\n",plugin->name,jsonstr,initflag,plugin->name); if ( initflag > 0 ) { SUPERNET.ppid = OS_getppid(); SUPERNET.argjson = cJSON_Duplicate(json,1); SUPERNET.readyflag = 1; if ( SUPERNET.UPNP != 0 ) { char portstr[64]; sprintf(portstr,"%d",SUPERNET.serviceport), upnpredirect(portstr,portstr,"TCP","SuperNET"); sprintf(portstr,"%d",SUPERNET.port + LB_OFFSET), upnpredirect(portstr,portstr,"TCP","SuperNET"); sprintf(portstr,"%d",SUPERNET.port + PUBGLOBALS_OFFSET), upnpredirect(portstr,portstr,"TCP","SuperNET"); sprintf(portstr,"%d",SUPERNET.port + PUBRELAYS_OFFSET), upnpredirect(portstr,portstr,"TCP","SuperNET"); } } else { if ( plugin_result(retbuf,json,tag) > 0 ) return((int32_t)strlen(retbuf)); methodstr = cJSON_str(cJSON_GetObjectItem(json,"method")); resultstr = cJSON_str(cJSON_GetObjectItem(json,"result")); if ( (destplugin= cJSON_str(cJSON_GetObjectItem(json,"name"))) == 0 ) destplugin = cJSON_str(cJSON_GetObjectItem(json,"path")); printf("SUPERNET\n"); if ( resultstr != 0 && strcmp(resultstr,"registered") == 0 ) { plugin->registered = 1; retstr = "return registered"; } else if ( methodstr != 0 && strcmp(methodstr,"install") == 0 && destplugin != 0 && destplugin[0] != 0 ) retstr = SuperNET_install(destplugin,jsonstr,json); else retstr = "return JSON result"; strcpy(retbuf,retstr); len = (int32_t)strlen(retbuf); while ( --len > 0 ) if ( retbuf[len] == '}' ) break; sprintf(retbuf + len,",\"debug\":%d,\"USESSL\":%d,\"MAINNET\":%d,\"DATADIR\":\"%s\",\"NXTAPI\":\"%s\",\"WEBSOCKETD\":\"%s\",\"SUPERNET_PORT\":%d,\"APISLEEP\":%d,\"domain\":\"%s\"}",Debuglevel,SUPERNET.usessl,SUPERNET.ismainnet,SUPERNET.DATADIR,SUPERNET.NXTAPIURL,SUPERNET.WEBSOCKETD,SUPERNET.port,SUPERNET.APISLEEP,SUPERNET.hostname); } return((int32_t)strlen(retbuf)); }
int32_t main #endif (int argc,const char *argv[]) { char *retbuf,*line,*jsonargs,*transportstr,registerbuf[MAX_JSON_FIELD]; struct plugin_info *plugin; double startmilli; cJSON *argjson; int32_t max,sendflag,sleeptime=1,len = 0; #ifndef BUNDLED portable_OS_init(); #endif milliseconds2(); max = 1000000; retbuf = malloc(max+1); plugin = calloc(1,sizeof(*plugin));// + PLUGIN_EXTRASIZE); //plugin->extrasize = PLUGIN_EXTRASIZE; plugin->ppid = OS_getppid(); strcpy(plugin->name,PLUGINSTR); if ( 0 ) { int32_t i; for (i=0; i<argc; i++) printf("(%s) ",argv[i]); printf("argc.%d\n",argc); } //printf("%s (%s).argc%d parent PID.%d\n",plugin->name,argv[0],argc,plugin->ppid); plugin->timeout = 1; if ( argc <= 2 ) { jsonargs = (argc > 1) ? stringifyM((char *)argv[1]) : stringifyM("{}"); configure_plugin(retbuf,max,plugin,jsonargs,-1); free(jsonargs); //fprintf(stderr,"PLUGIN_RETURNS.[%s]\n",retbuf), fflush(stdout); return(0); } randombytes((uint8_t *)&plugin->myid,sizeof(plugin->myid)); plugin->permanentflag = atoi(argv[1]); plugin->daemonid = calc_nxt64bits(argv[2]); #ifdef BUNDLED plugin->bundledflag = 1; #endif transportstr = get_localtransport(plugin->bundledflag); sprintf(plugin->connectaddr,"%s://SuperNET.agents",transportstr); sprintf(plugin->bindaddr,"%s://%llu",transportstr,(long long)plugin->daemonid); jsonargs = (argc >= 3) ? ((char *)argv[3]) : 0; configure_plugin(retbuf,max,plugin,jsonargs,1); printf("CONFIGURED.(%s) argc.%d: %s myid.%llu daemonid.%llu NXT.%s serviceNXT.%s\n",plugin->name,argc,plugin->permanentflag != 0 ? "PERMANENT" : "WEBSOCKET",(long long)plugin->myid,(long long)plugin->daemonid,plugin->NXTADDR,plugin->SERVICENXT);//,jsonargs!=0?jsonargs:""); if ( init_pluginsocks(plugin,plugin->permanentflag,plugin->myid,plugin->daemonid,plugin->timeout) == 0 ) { argjson = cJSON_Parse(jsonargs); if ( (len= registerAPI(registerbuf,sizeof(registerbuf)-1,plugin,argjson)) > 0 ) { //fprintf(stderr,">>>>>>>>>>>>>>> plugin.(%s) sends REGISTER SEND.(%s)\n",plugin->name,registerbuf); nn_local_broadcast2(plugin->pushsock,0,0,(uint8_t *)registerbuf,(int32_t)strlen(registerbuf)+1), plugin->numsent++; } else printf("error register API\n"); if ( argjson != 0 ) free_json(argjson); } else printf("error init_pluginsocks\n"); while ( OS_getppid() == plugin->ppid ) { retbuf[0] = 0; if ( (len= nn_recv(plugin->pullsock,&line,NN_MSG,0)) > 0 ) { len = (int32_t)strlen(line); //printf("(s%d r%d) <<<<<<<<<<<<<< %s.RECEIVED (%s).%d -> bind.(%s) connect.(%s) %s\n",plugin->numsent,plugin->numrecv,plugin->name,line,len,plugin->bindaddr,plugin->connectaddr,plugin->permanentflag != 0 ? "PERMANENT" : "WEBSOCKET"), fflush(stdout); if ( (len= process_plugin_json(retbuf,max,&sendflag,plugin,plugin->permanentflag,plugin->daemonid,plugin->myid,line)) > 0 ) { if ( plugin->bundledflag == 0 ) printf("%s\n",retbuf), fflush(stdout); if ( sendflag != 0 ) { nn_local_broadcast2(plugin->pushsock,0,0,(uint8_t *)retbuf,(int32_t)strlen(retbuf)+1), plugin->numsent++; //fprintf(stderr,">>>>>>>>>>>>>> returned.(%s)\n",retbuf); } } //else printf("null return from process_plugin_json\n"); nn_freemsg(line); } else { startmilli = milliseconds2(); if ( PLUGNAME(_idle)(plugin) == 0 ) { if ( plugin->sleepmillis != 0 ) { sleeptime = plugin->sleepmillis - (milliseconds2() - startmilli); //printf("%s sleepmillis.%d sleeptime.%d\n",plugin->name,plugin->sleepmillis,sleeptime); if ( sleeptime > 0 ) msleep2(sleeptime); } } } } fprintf(stderr,"ppid.%d changed to %d\n",plugin->ppid,OS_getppid()); PLUGNAME(_shutdown)(plugin,len); // rc == 0 -> parent process died nn_shutdown(plugin->pushsock,0); if ( plugin->pushsock != plugin->pullsock ) nn_shutdown(plugin->pullsock,0); free(plugin); return(len); }
static ERL_NIF_TERM os_get_ppid_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { int ret = OS_getppid(); return enif_make_int(env, ret); }