void S_create_push_threads(pTHX, HV* options, SV* startup) { struct thread_create thread_options; int clone_number; int counter; Zero(&thread_options, 1, struct thread_create); clone_number = prepare_thread_create(aTHX, &thread_options, options, startup); for (counter = 0; counter < clone_number; ++counter) { mthread* thread; const message* to_run; const message* modules; thread = mthread_alloc(aTHX); if (thread_options.monitor) thread_add_listener(aTHX, thread->id, thread_options.parent_id); modules = message_clone(thread_options.modules); queue_enqueue(thread->queue, modules, NULL); push_thread(aTHX, thread); to_run = message_clone(thread_options.to_run); queue_enqueue(thread->queue, to_run, NULL); start_thread(thread, thread_options.stack_size); } destroy_message(thread_options.to_run); if (thread_options.modules) destroy_message(thread_options.modules); }
//function responsible for creation of thread- to be called once int intialize_thread() // return 1 on success else 0. { int i; struct threadID *new_thread=NULL; for(i=0;i<max_slave_server;i++) { new_thread=malloc(sizeof(struct threadID));// allocating space to new node if(new_thread==NULL) { printf("\nMemory overflow"); return 0; } else { new_thread->client.port=global_port+1+i; // allocating port number to newly created thread if(sem_init(&(new_thread->binary_sem),0,0)==0) new_thread->status=0; pthread_create(&(new_thread->ID),NULL,handle,new_thread); // opening port for accepting connections if(common_connect_server(&host_ipaddress,&new_thread->client.server_socket,new_thread->client.port,(const char *)NULL)==0) { printf("\n Not able to open connection"); return 1; } printf("\n Able to open new port\n"); push_thread(new_thread); // pushing newly created thread into stack } } return 1; }
/*/////////////////////////////////////////////////////////// Function for operation on threads /////////////////////////////////////////////////////////////*/ void * handle(void * data) { struct threadID * temp_thread=(struct threadID *) data; int quit1=1; int *temp=(int *) data; char buffer[512]; TCPsocket client_socket; while(1) // to make sure thread doesn't terminate { if(temp_thread->status==0) { sem_wait(&(temp_thread->binary_sem)); } printf("\n Activating thread"); // here thread start receiving data from client while(quit1) { if(client_socket=SDLNet_TCP_Accept(temp_thread->client.server_socket)) { printf("\nGetting request from client"); quit1=1; while(quit1) { if(temp_thread->client.client_ipaddress=SDLNet_TCP_GetPeerAddress(client_socket)) printf("\n Listening from client:%x",SDLNet_Read32(&temp_thread->client.client_ipaddress->host)); if(SDLNet_TCP_Recv(client_socket,buffer,512)) { printf("\nClient send:%s",buffer); if(!strcmp(buffer,"exit")) { printf("\n Client requesting for closing connection"); quit1=0; } if(!strcmp(buffer,"quit")) { quit1=0; quit=0; } } } printf("\nClosing client socket"); SDLNet_TCP_Close(client_socket); } } quit1=1; //so that this thread can be used again temp_thread->status=0; push_thread(temp_thread); }// here it ends printf("\n Terminating thread"); //TODO }
static inline void push_runnable(struct thread *t) { push_thread(&runnable_threads, t); }
static inline void free_thread(struct thread *t) { push_thread(&free_threads, t); }
int main(int argc, char **argv) { int opt, num_threads; time_t start, end; double run_time; long threads[MAX_THREADS]; long i; Octstr *fos; gwlib_init(); num_threads = 1; while ((opt = getopt(argc, argv, "HhBbnEpv:qr:t:c:a:i:e:k:d:s:S:I:m:u:")) != EOF) { switch(opt) { case 'v': log_set_output_level(atoi(optarg)); break; case 'q': verbose = 0; break; case 'r': max_pushes = atoi(optarg); break; case 'i': wait_seconds = atof(optarg); break; case 't': num_threads = atoi(optarg); if (num_threads > MAX_THREADS) num_threads = MAX_THREADS; break; case 'H': use_hardcoded = 1; break; case 'c': content_flag = octstr_create(optarg); if (octstr_compare(content_flag, octstr_imm("wml")) != 0 && octstr_compare(content_flag, octstr_imm("si")) != 0 && octstr_compare(content_flag, octstr_imm("sl")) != 0 && octstr_compare(content_flag, octstr_imm("nil")) != 0 && octstr_compare(content_flag, octstr_imm("mms")) != 0 && octstr_compare(content_flag, octstr_imm("scrap")) != 0 && octstr_compare(content_flag, octstr_imm("multipart")) != 0) { octstr_destroy(content_flag); error(0, "TEST_PPG: Content type not known"); help(); exit(1); } break; case 'a': appid_flag = octstr_create(optarg); if (octstr_compare(appid_flag, octstr_imm("any")) != 0 && octstr_compare(appid_flag, octstr_imm("ua")) != 0 && octstr_compare(appid_flag, octstr_imm("mms")) != 0 && octstr_compare(appid_flag, octstr_imm("nil")) != 0 && octstr_compare(appid_flag, octstr_imm("scrap")) != 0) { octstr_destroy(appid_flag); error(0, "TEST_PPG: Push application id not known"); help(); exit(1); } break; case 'n': use_numeric = 1; break; case 's': appid_string = octstr_create(optarg); use_string = 1; break; case 'S': content_header = octstr_create(optarg); use_content_header = 1; break; case 'e': content_transfer_encoding = octstr_create(optarg); if (octstr_compare(content_transfer_encoding, octstr_imm("base64")) != 0) { octstr_destroy(content_transfer_encoding); error(0, "TEST_PPG: unknown content transfer" " encoding \"%s\"", octstr_get_cstr(content_transfer_encoding)); help(); exit(1); } break; case 'k': connection = octstr_create(optarg); if (octstr_compare(connection, octstr_imm("close")) != 0 && octstr_compare(connection, octstr_imm("keep-alive")) != 0) { octstr_destroy(connection); error(0, "TEST_PPG: Connection-header unacceptable"); help(); exit(1); } break; case 'h': help(); exit(1); case 'b': use_headers = 1; break; case 'B': accept_binary = 1; break; case 'd': delimiter = octstr_create(optarg); if (octstr_compare(delimiter, octstr_imm("crlf")) != 0 && octstr_compare(delimiter, octstr_imm("lf")) != 0) { octstr_destroy(delimiter); error(0, "illegal d value"); help(); exit(1); } break; case 'E': add_epilogue = 1; break; case 'p': add_preamble = 1; break; case 'I': initiator_uri = octstr_create(optarg); break; case 'm': use_dlr_mask = 1; dlr_mask = octstr_create(optarg); break; case 'u': use_dlr_url = 1; dlr_url = octstr_create(optarg); break; case '?': default: error(0, "TEST_PPG: Invalid option %c", opt); help(); error(0, "Stopping"); exit(1); } } if (optind == argc) { help(); exit(1); } push_data = argv + optind; num_urls = argc - optind; if (content_flag == NULL) content_flag = octstr_imm("si"); if (appid_flag == NULL) appid_flag = octstr_imm("ua"); if (appid_string == NULL) appid_string = octstr_imm("x-wap-application-id: wml.ua"); if (content_header == NULL) use_content_header = 0; if (dlr_mask == NULL) use_dlr_mask = 0; if (dlr_url == NULL) use_dlr_url = 0; if (delimiter == NULL) delimiter = octstr_imm("crlf"); if (use_hardcoded) { username = octstr_imm("troo"); password = octstr_imm("far"); } if (push_data[0] == NULL) { error(0, "No ppg address or config file, stopping"); exit(1); } use_config = 0; if (!use_hardcoded) { if (push_data[1] == NULL) { info(0, "a configuration file input assumed"); read_test_ppg_config(fos = octstr_format("%s", push_data[0])); octstr_destroy(fos); use_config = 1; } } if (!use_config) push_url = octstr_format("%s", push_data[0]); if (!use_hardcoded && !use_config && push_data[1] != NULL) { if (push_data[2] == NULL) { error(0, "no pap control document, stopping"); exit(1); } else { info(0, "an input without a configuration file assumed"); content_file = octstr_create(push_data[1]); pap_file = octstr_create(push_data[2]); debug("test.ppg", 0, "using %s as a content file", push_data[1]); debug("test.ppg", 0, "using %s as a control file", push_data[2]); } } boundary = "asdlfkjiurwghasf"; counter = counter_create(); time(&start); if (num_threads == 0) push_thread(http_caller_create()); else { for (i = 0; i < num_threads; ++i) threads[i] = gwthread_create(push_thread, http_caller_create()); for (i = 0; i < num_threads; ++i) gwthread_join(threads[i]); } time(&end); run_time = difftime(end, start); info(0, "TEST_PPG: %ld requests in %f seconds, %f requests per second", max_pushes, run_time, max_pushes / run_time); octstr_destroy(content_flag); octstr_destroy(appid_flag); octstr_destroy(content_header); octstr_destroy(content_file); octstr_destroy(pap_file); octstr_destroy(ssl_client_certkey_file); octstr_destroy(username); octstr_destroy(password); octstr_destroy(push_url); octstr_destroy(connection); octstr_destroy(delimiter); octstr_destroy(dlr_mask); octstr_destroy(dlr_url); counter_destroy(counter); gwlib_shutdown(); exit(0); }