void *srv_graph(void *v) { name_attach_t* pnat; char srv_name[BUFFER_SIZE] = SRV_NAME; char cmd[BUFFER_SIZE], param[BUFFER_SIZE]; int rcvid; frame_t frame; size_t i; iov_t *piov, header; FILE * pfcfg = fopen(FILE_NAME_CFG, "r"); if(NULL != pfcfg) { while(0 == feof(pfcfg)) { fscanf(pfcfg, "%s %s", cmd, param); if(0 == strcmp(cmd, "srv_name")) { strcpy(srv_name, param); } } fclose(pfcfg); } //log_start("srv_graph_log", "srv_graph_err"); pnat = name_attach(NULL, srv_name, NAME_FLAG_ATTACH_GLOBAL); SETIOV(&header, &frame, sizeof(frame_t)); while(true) { //log_update(); rcvid = MsgReceivev(pnat->chid, &header, 1, NULL); __ERROR_CONT(rcvid, -1, MSG_ERR_MSGREC); if(0 == rcvid) { switch(frame.header.code) { case _PULSE_CODE_DISCONNECT: printf("Client has gone\n"); break; case _PULSE_CODE_UNBLOCK: printf("_PULSE_CODE_UNBLOCK\n"); __ERROR_CHECK(MsgError(frame.header.value.sival_int, ETIME),-1,MSG_ERR_MSGERR); break; default: break; } } else { if (frame.header.type == _IO_CONNECT) { printf("Send OK\n"); frame_reply(rcvid, NULL); continue; } frame_datareceive(rcvid, &frame); frame_reply(rcvid, NULL); i=0; while(i<frame.size) { processtask(frame.ptask+i); ++i; } frame_destroy(&frame); } //log_stop(); } }
codeword_t bch_encoder_encode (bch_encoder_t self, frame_t frame) { log2(self->log, "encoding started\n"); codeword_t codeword = bch_encoder_kernel_encode(self->bch_encoder_kernel, frame); self->encoded_frames_cnt++; self->generated_codewords_cnt++; self->transmitted_bits_cnt += self->code_length; frame_destroy(frame); log2(self->log, "encoding finished\n"); return codeword; }
int receiver_receive_frame (receiver_t self, frame_t const frame_out, frame_t const frame_in) { self->received_frames_cnt++; self->received_bits_cnt += self->frame_size; if (frame_cmp(frame_in, frame_out)) { self->received_frames_valid_cnt++; self->received_bits_valid_cnt += frame_find_valid_bits(frame_in, frame_out); } else { self->received_frames_invalid_cnt++; /* find valid and invalid both */ self->received_bits_valid_cnt += frame_find_valid_bits(frame_in, frame_out); self->received_bits_invalid_cnt += frame_find_invalid_bits(frame_in, frame_out); } int i = 0; if(self->data_size - received_bits_cnt >= self->frame_size) { for (i = 0; i < self->frame_size; i++) self->data[received_bits_cnt++] = frame_out->xframe[i]; } else { int bits_q = self->data_size - received_bits_cnt; for (i = 0; i < bits_q; i++) self->data[received_bits_cnt++] = frame_out->xframe[i]; } frame_destroy(frame_out); frame_destroy(frame_in); return 0; }
void frame_factory_destroy(void) { if (texture_cfg != NULL) { iniparser_freedict(texture_cfg); texture_cfg = NULL; } if (frame_cache != NULL) { int i; frame_t *frame; for (i = 0; i < frame_cache->size; ++ i) { if (frame_cache->key[i] != NULL) { frame = frame_factory_get(frame_cache->key[i]); frame_destroy(frame); } } dictionary_del(frame_cache); frame_cache = NULL; } return; }
void *worker(void *v) { wrk_info_t wrk_info = *(wrk_info_t*)v; int result; struct sigevent clientevent, timeoutevent; struct sigaction timeoutact; iov_t *piov, header; frame_t frame; timer_t timer; syncsig_t *psync = &wrk_info.psync[wrk_info.id]; struct itimerspec timeout, stoptimeout; struct timespec slp; slp.tv_nsec = 10000000; slp.tv_sec = 0; timeout.it_interval.tv_sec = 0; timeout.it_interval.tv_nsec = 0; timeout.it_value.tv_sec = 0; timeout.it_value.tv_nsec = 0; stoptimeout = timeout; sem_init(&psync->sem, 0, 1); SETIOV(&header, &frame, sizeof(frame_t)); SIGEV_SIGNAL_VALUE_INIT(&timeoutevent, SIGUSR1, psync); //SIGEV_SIGNAL_INIT(&timeoutevent, SIGUSR1) ; __ERROR_CHECK(timer_create(CLOCK_MONOTONIC, &timeoutevent, &timer),-1,"timer_create"); __ERROR_CHECK(signal(SIGUSR1, wrk_sigusr1),-1,"signal"); //timeoutact.sa_handler = &wrk_sigusr1; //sigaction(SIGUSR1, &timeoutact, NULL); while(true) { psync->rcvid = -1; wrk_info.psync[wrk_info.id].status = READY; psync->rcvid = MsgReceivev(wrk_info.chid, &header, 1, NULL); __ERROR_CONT(psync->rcvid, -1, MSG_ERR_MSGREC); wrk_info.psync[wrk_info.id].status = WORK; if(0 == psync->rcvid) { switch(frame.header.code) { case _PULSE_CODE_DISCONNECT: printf("Client has gone\n"); break; case _PULSE_CODE_UNBLOCK: printf("_PULSE_CODE_UNBLOCK\n"); for(size_t i=0; i<wrk_info.wrk_amount; ++i) { if(wrk_info.psync[i].rcvid == frame.header.value.sival_int) { if(wrk_info.psync[i].status != SEND) { __ERROR_CHECK(MsgError(frame.header.value.sival_int, ETIME),-1,MSG_ERR_MSGERR); } break; } } break; default: break; } } else { if (frame.header.type == _IO_CONNECT) { printf("Send OK\n"); frame_reply(psync->rcvid, NULL); continue; } wrk_info.proute[frame.cid] = wrk_info.id; timeout.it_value = frame.timeout; //printf("%u %u\n", timeout.it_value.tv_sec, timeout.it_value.tv_nsec); sem_wait(&psync->sem); if(frame.protocol != REPLY) { __ERROR_CHECK(timer_settime(timer, 0, &timeout, NULL),-1,"timer_settime"); } //printf("Thread %i, client %i\n", wrk_info.id, frame.cid); frame_datareceive(psync->rcvid, &frame); sem_post(&psync->sem); //sleep(1); nanosleep( &slp, NULL); if(frame.protocol == NOREPLY) { SIGEV_PULSE_INIT( &clientevent, frame.coid, SIGEV_PULSE_PRIO_INHERIT, PULSE_CODE_DATA_READY, 0); frame_reply(psync->rcvid, NULL); } if(frame.protocol != REPLY) { frame_repinit(&frame, &wrk_info.pcash[frame.cid].framerep); for(size_t i=0; i<frame.size; ++i) { processtask(frame.ptask+i, wrk_info.pcash[frame.cid].framerep.ptask+i, &wrk_info.pcash[frame.cid].spline); } __ERROR_CHECK(timer_settime(timer, 0, &stoptimeout, NULL),-1,"timer_settime"); } wrk_info.psync[wrk_info.id].status = SEND; if(frame.protocol == NOREPLY) { result = -1; for(int i=0; i<3 && result == -1; ++i) { result = MsgDeliverEvent(psync->rcvid, &clientevent); __ERROR_CHECK(result, -1, "MsgDeliverEvent"); } } else { frame_reply(psync->rcvid, &wrk_info.pcash[frame.cid].framerep); frame_destroy(&wrk_info.pcash[frame.cid].framerep); } } } }
int main(int argc, char **argv) { if(argc != 2) { printf("Set client id\n"); return EXIT_SUCCESS; } uint32_t cid = atoi(argv[1]);//!! struct timespec timeout, stime, etime; frame_t frame, rframe; char buf[BUFFER_SIZE], cmd[BUFFER_SIZE], param[BUFFER_SIZE]; int itr = -1, n, k; FILE *pfile = 0; uint32_t itr_max = ITR; double_t *pfcn, *psig, init; uint64_t timeoutsend = 200000000; //***************************************************************************** timeout.tv_nsec = 0; timeout.tv_sec = 1; strcpy(buf, CFGFILENAME); strcat(buf, argv[1]); pfile = fopen(buf, "r"); if(NULL != pfile) { while(0 == feof(pfile)) { fscanf(pfile, "%s %s", cmd, param); if(0 == strcmp(cmd, "itr_max")) { itr_max = atoi(param); } else if(0 == strcmp(cmd, "timeoutsend")) { timeoutsend = atol(param); } else if(0 == strcmp(cmd, "timeout_sec")) { timeout.tv_sec = atol(param); } else if(0 == strcmp(cmd, "timeout_nsec")) { timeout.tv_nsec = atol(param); } } fclose(pfile); } else { perror(MSG_ERR_CFGFILE); } strcpy(buf, OUTFILENAME); strcat(buf, argv[1]); pfile = fopen(buf, "w"); fprintf(pfile, "itr_max %i\ntimeoutsend %llu\ntimeout %lu %lu\n", itr_max, timeoutsend, timeout.tv_sec, timeout.tv_nsec); pfcn = malloc(sizeof(double_t)*(SIZE*2)); psig = malloc(sizeof(double_t)*((SIZE-1)*2)); srand(time(NULL)); for(size_t i = 0; i<SIZE; i++) { pfcn[i] = i*STEP; pfcn[i+SIZE] = sin(pfcn[i])+2; } for(size_t i = 0; i<SIZE-1; i++) { psig[i] = SHIFT + (rand()%SIZE)*STEP; psig[i+SIZE-1] = sin(psig[i])+2; } frame.cid = cid; frame.fid = 0; frame.size = 2; //***************************************************************************** int srv_coid = name_open(SRV_NAME, NAME_FLAG_ATTACH_GLOBAL); __ERROR_EXIT(srv_coid, -1, SRV_NAME); frame.ptask = malloc(sizeof(task_t)*2); frame.ptask[0].cmd = SPLINE_INIT; frame.ptask[0].n = SIZE*2*sizeof(double_t); frame.ptask[0].px = (void*)pfcn; frame.timeout = timeout; frame.ptask[1].cmd = SPLINE_GETVAL; frame.ptask[1].n = 2*sizeof(double_t); frame.ptask[1].px = (void*)psig; frame_repinit(&frame, &rframe); __ERROR_CHECK(TimerTimeout(CLOCK_REALTIME, _NTO_TIMEOUT_SEND | _NTO_TIMEOUT_REPLY, NULL, &timeoutsend, NULL), -1, "TimerTimeout"); // send init data about spline __ERROR_CHECK(frame_send(srv_coid, &frame, &rframe), -1, "sendv"); frame_destroy(&rframe); free(frame.ptask); frame.ptask = malloc(sizeof(task_t)); frame.ptask[0].cmd = SPLINE_GETVAL; frame.ptask[0].n = sizeof(double_t); frame.size = 1; frame_repinit(&frame, &rframe); k=0; for(uint32_t i=0; i<itr_max; ++i) { frame.fid = i; n = rand()%(SIZE-1); frame.ptask[0].px = (void*)&psig[n]; trace_msg_start(); // send tasks to server and wait for results __ERROR_CONT(frame_send(srv_coid, &frame, &rframe), -1, "sendv"); trace_msg_stop(); clock_gettime(CLOCK_MONOTONIC, &etime); fprintf(pfile, "%f %f %u %u\n", psig[n], *(double_t*)rframe.ptask->px, etime.tv_sec-stime.tv_sec, etime.tv_nsec-stime.tv_nsec); printf("I receive data\n"); } frame.ptask[0].cmd = SPLINE_DESTROY; frame.ptask[0].n = 0; frame.ptask[0].px = 0; frame.fid++; // say server to destroy our spline __ERROR_CHECK(frame_send(srv_coid, &frame, &rframe), -1, "sendv"); fclose(pfile); frame_destroy(&rframe); free(pfcn); free(psig); __ERROR_CHECK(name_close(srv_coid), -1, "name_close"); return EXIT_SUCCESS; }