// p is a malloc'ed buffer // int handle_command(char* p) { char cmd[256]; int id; sscanf(p, "%s", cmd); if (!strcasecmp(cmd, "VERSION")) { printf("S "); print_version(); } else if (!strcasecmp(cmd, "COMMANDS")) { printf("S ASYNC_MODE_OFF ASYNC_MODE_ON BOINC_ABORT_JOBS BOINC_FETCH_OUTPUT BOINC_PING BOINC_QUERY_BATCHES BOINC_RETIRE_BATCH BOINC_SELECT_PROJECT BOINC_SUBMIT COMMANDS QUIT RESULTS VERSION\n"); } else if (!strcasecmp(cmd, "RESPONSE_PREFIX")) { printf("S\n"); strcpy(response_prefix, p+strlen("RESPONSE_PREFIX ")); } else if (!strcasecmp(cmd, "ASYNC_MODE_ON")) { printf("S\n"); } else if (!strcasecmp(cmd, "ASYNC_MODE_OFF")) { printf("S\n"); } else if (!strcasecmp(cmd, "QUIT")) { exit(0); } else if (!strcasecmp(cmd, "RESULTS")) { printf("S %d\n", n_results()); vector<COMMAND*>::iterator i = commands.begin(); while (i != commands.end()) { COMMAND *c2 = *i; if (c2->out) { printf("%s%d %s\n", response_prefix, c2->id, c2->out); free(c2->out); free(c2->in); free(c2); i = commands.erase(i); } else { i++; } } } else if (!strcasecmp(cmd, "BOINC_SELECT_PROJECT")) { int n = sscanf(p, "%s %s %s", cmd, project_url, authenticator); if (n ==3) { printf("S\n"); } else { printf("E\n"); } } else { // asynchronous commands go here // COMMAND *cp = new COMMAND(p); int retval = cp->parse_command(); if (retval) { printf("E\n"); delete cp; return 0; } if (debug_mode) { handle_command_aux(cp); printf("result: %s\n", cp->out); delete cp; } else { printf("S\n"); commands.push_back(cp); pthread_t thread_handle; pthread_attr_t thread_attrs; pthread_attr_init(&thread_attrs); pthread_attr_setstacksize(&thread_attrs, 32768); int retval = pthread_create( &thread_handle, &thread_attrs, &handle_command_aux, cp ); if (retval) { fprintf(stderr, "can't create thread\n"); return -1; } } } return 0; }
// p is a malloc'ed buffer // int handle_command(char* p) { char cmd[256]; int id; cmd[0] = '\0'; sscanf(p, "%s", cmd); if (!strcasecmp(cmd, "VERSION")) { print_version(false); } else if (!strcasecmp(cmd, "COMMANDS")) { BPRINTF("S ASYNC_MODE_OFF ASYNC_MODE_ON BOINC_ABORT_JOBS BOINC_FETCH_OUTPUT BOINC_PING BOINC_QUERY_BATCHES BOINC_RETIRE_BATCH BOINC_SELECT_PROJECT BOINC_SET_LEASE BOINC_SUBMIT COMMANDS QUIT RESULTS VERSION\n"); } else if (!strcasecmp(cmd, "RESPONSE_PREFIX")) { flockfile(stdout); BPRINTF("S\n"); strlcpy(response_prefix, p+strlen("RESPONSE_PREFIX "), sizeof(response_prefix)); funlockfile(stdout); } else if (!strcasecmp(cmd, "ASYNC_MODE_ON")) { flockfile(stdout); BPRINTF("S\n"); async_mode = true; funlockfile(stdout); } else if (!strcasecmp(cmd, "ASYNC_MODE_OFF")) { flockfile(stdout); BPRINTF("S\n"); async_mode = false; funlockfile(stdout); } else if (!strcasecmp(cmd, "QUIT")) { exit(0); } else if (!strcasecmp(cmd, "RESULTS")) { flockfile(stdout); int cnt = n_results(); BPRINTF("S %d\n", cnt); vector<COMMAND*>::iterator i = commands.begin(); int j = 0; while (i != commands.end() && j < cnt) { COMMAND *c2 = *i; if (c2->out) { BPRINTF("%d %s\n", c2->id, c2->out); delete c2; i = commands.erase(i); j++; } else { ++i; } } wrote_r = false; funlockfile(stdout); } else if (!strcasecmp(cmd, "BOINC_SELECT_PROJECT")) { int n = sscanf(p, "%s %s %s", cmd, project_url, authenticator); if (n ==3) { BPRINTF("S\n"); } else { BPRINTF("E\n"); } } else { // asynchronous commands go here // COMMAND *cp = new COMMAND(p); p = NULL; int retval = cp->parse_command(); if (retval) { BPRINTF("E\n"); delete cp; return 0; } if (debug_mode) { handle_command_aux(cp); BPRINTF("result: %s\n", cp->out); delete cp; } else { printf("S\n"); commands.push_back(cp); pthread_t thread_handle; pthread_attr_t thread_attrs; pthread_attr_init(&thread_attrs); pthread_attr_setstacksize(&thread_attrs, 256*1024); int retval = pthread_create( &thread_handle, &thread_attrs, &handle_command_aux, cp ); if (retval) { fprintf(stderr, "can't create thread\n"); return -1; } } } free(p); return 0; }