/** * This function represents server thread. Thanks to this function we can * receive data send by client. * * @param nth - not used but demanded by pthread_create() convencion * @return no value returned */ void* receive (void* nth) { my_data_t msg; int rcvid; /* Trying to register and create channel */ if ((attach = name_attach(NULL, my_channel, 0)) == NULL) { std::cerr<< "Can't create Channel\n"; exit(-1); } /* main loop - receiving messages */ while(true) { rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL); /* checking if message was received correctly */ if (rcvid == -1) { std::cerr<<"Error in receiving message. \n"; break; } /* checking if message is a pulse */ if (rcvid == 0) { switch (msg.header.code) { case _PULSE_CODE_DISCONNECT: /* Canceling client thread */ pthread_cancel(client); /* Waiting for thread to finish executing */ pthread_join(client, NULL); /* Closing connection and destroying channel */ name_close(coid); name_detach(attach, 0); /* Finish server thread */ pthread_exit (0); } } /* check if message is connection request */ if (msg.header.type == _IO_CONNECT) { MsgReply(rcvid, EOK, NULL, 0); continue; } /* chcek id message is unimplemented system communicate */ if (msg.header.type > _IO_BASE && msg.header.type <= _IO_MAX) { MsgError (rcvid, ENOSYS); continue; } printf("\n"); printf("[%s]: %s\n", stranger_channel, msg.data); printf("[%s]: ", my_channel); fflush(stdout); //std::cout<<"Server received: " << msg.data<< "\n "; MsgReply(rcvid, EOK, NULL, 0); } }
void handle_signal(int sig) { /* Canceling server and client threads */ pthread_cancel(client); pthread_cancel(server); /* Waiting for threads to finish executing */ pthread_join(client, NULL); pthread_join(server, NULL); /* Closing connection and destroying channel */ name_close(coid); name_detach(attach, 0); }
void create_archive() { register char *p; open_archive(0); /* Open for writing */ while (p = name_next()) { dump_file(p, -1); } write_eot(); close_archive(); name_close(); }
/*** Client Side of the code ***/ int client() { mss_t msg; int server_coid; mss_t rmsg; if ((server_coid = name_open(ATTACH_POINT, 0)) == -1) { return EXIT_FAILURE; } /* We would have pre-defined data to stuff here */ // msg.hdr.type = 0x00; //msg.hdr.subtype = 0x00; msg.ile = 0; int typ = 1; msg.typ = typ; int from = getpid(); msg.from = from; int i = 0; int count = 0; /* Do whatever work you wanted with server connection */ printf("what you want Send?"); scanf("%s", msg.text); for(i = 0; i < strlen(msg.text); i++){ if(islower(msg.text[i]) ) count++; } printf("count : %d ", count); msg.ile = count; printf("Client sending msg, lower case letters: %d \n", msg.ile); if (MsgSend(server_coid, &msg, sizeof(msg), &rmsg, sizeof(rmsg)) == -1) { perror("MsgSend"); exit(EXIT_FAILURE); } printf("received message %s from server %d\n",rmsg.text, rmsg.from); /* Close the connection */ name_close(server_coid); return EXIT_SUCCESS; }
void create_archive() { register char *p; char *name_from_list(); open_archive(0); /* Open for writing */ if(f_gnudump) { char buf[MAXNAMLEN],*q,*bufp; collect_and_sort_names(); while(p=name_from_list()) dump_file(p,-1); /* if(!f_dironly) { */ blank_name_list(); while(p=name_from_list()) { strcpy(buf,p); if(p[strlen(p)-1]!='/') strcat(buf,"/"); bufp=buf+strlen(buf); for(q=gnu_list_name->dir_contents;*q;q+=strlen(q)+1) { if(*q=='Y') { strcpy(bufp,q+1); dump_file(buf,-1); } } } /* } */ } else { while (p = name_next(1)) { dump_file(p, -1); } } write_eot(); close_archive(); name_close(); }
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; }