static void slv_done( slv_db_typ *pdb, dl_head_typ *pusr, comm_clt_typ *psvc_clt, int name_id, char *pservname, void *name_struct ) { int xport = psvc_clt->xport; if( pusr != NULL ) dl_free( pusr ); switch (xport) { case COMM_QNX6_XPORT: if ( NULL != name_struct ) { name_detach( (name_attach_t *) name_struct, 0 ); } break; case COMM_QNX_XPORT: if( name_id != ERROR ) qnx_name_detach( 0, name_id ); break; case COMM_PSX_XPORT: // terminate and clean-up after database queue comm_cleanup(psvc_clt, pservname, 0); break; default: fprintf(stderr, " Transport %d not implemented\n", xport); break; } if( psvc_clt != NULL ) comm_done( psvc_clt ); if( pdb != NULL ) slv_db_destroy( pdb ); }
/** * 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); }
int server() { name_attach_t *attach; mss_t msg; mss_t rmsg; //my_data_t msg; int rcvid; /* Create a local name (/dev/name/local/...) */ if ((attach = name_attach(NULL, ATTACH_POINT, 0)) == NULL) { return EXIT_FAILURE; } /* Do your MsgReceive's here now with the chid */ while (1) { rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL); if (rcvid == -1) {/* Error condition, exit */ break; } // if (rcvid == 0) {/* Pulse received */ // switch (msg.hdr.code) { // case _PULSE_CODE_DISCONNECT: /* * A client disconnected all its connections (called * name_close() for each name_open() of our name) or * terminated */ // ConnectDetach(msg.hdr.scoid); // break; // case _PULSE_CODE_UNBLOCK: /* * REPLY blocked client wants to unblock (was hit by * a signal or timed out). It's up to you if you * reply now or later. // */ // break; // default: /* * A pulse sent by one of your processes or a * _PULSE_CODE_COIDDEATH or _PULSE_CODE_THREADDEATH * from the kernel? */ // break; // } // continue; // } /* name_open() sends a connect message, must EOK this */ // if (msg.hdr.type == _IO_CONNECT ) { // MsgReply( rcvid, EOK, NULL, 0 ); // continue; // } /* Some other QNX IO message was received; reject it */ // if (msg.hdr.type > _IO_BASE && msg.hdr.type <= _IO_MAX ) { // MsgError( rcvid, ENOSYS ); // continue; // } /* A message (presumable ours) received, handle */ printf("Server receive text :%s \n", msg.text); //czyszcze rmsg.text przed ponownym urzyciem memset(&rmsg.text[0], 0, sizeof(rmsg.text)); rmsg.from = getpid(); rmsg.typ = 1; rmsg.ile = 0;//tu modyf int i = 0; // for(i = 0; i < strlen(msg.text); i++) // rmsg.text[i] = msg.text[i]; char letter ; for(i = 0; i < strlen(msg.text); i++){ //if(islower(msg.text[i]) ){ letter = toupper(msg.text[i]); // rmsg.text[i] = letter;//844 rmsg.text[i] = letter; //msg.text[i]; //} //} } printf("server will send text : %s\n", rmsg.text); //rmsg.text[1] = 's'; MsgReply(rcvid, EOK, &rmsg, sizeof(rmsg)); } /* Remove the name from the space */ name_detach(attach, 0); return EXIT_SUCCESS; }
int main(int argc, char **argv) { //***************************************************************************** FILE *pfcfg; syncsig_t *psync; sem_t wrk_sem; sem_t *pwrk_sem = &wrk_sem; struct timespec wait_wrk_sem; size_t i, wrk_amount = CLIENT_MAX, client_max = CLIENT_MAX; pthread_t *pworker; name_attach_t* pnat; frame_t frame; iov_t *piov, *pheader; cash_t *pcash; pheader = malloc(sizeof(iov_t)); SETIOV(pheader, &frame, sizeof(frame_t)); char buf[BUFFER_SIZE], cmd[BUFFER_SIZE], param[BUFFER_SIZE]; int rcvid, slave_chid; char srv_name[BUFFER_SIZE] = SRV_NAME; // Slave support slave_t slave; slave.amount = 0; uint32_t *proute; //***************************************************************************** // Configs //log_start(FILE_NAME_LOG, FILE_NAME_ERR); printf("%s\n", MSG_VER_START); wait_wrk_sem.tv_nsec = WRK_SEM_TIMEOUT; wait_wrk_sem.tv_sec = 0; if(argc == 2) { pfcfg = fopen(argv[1], "r"); } else { pfcfg = fopen(FILE_NAME_CFG, "r"); } if(NULL != pfcfg) { while(0 == feof(pfcfg)) { fscanf(pfcfg, "%s %s", cmd, param); if(0 == strcmp(cmd, CFG_PAR_WRKAM)) { wrk_amount = atoi(param); } else if(0 == strcmp(cmd, CFG_PAR_WRKSEMTIME)) { wait_wrk_sem.tv_nsec = atol(param); } else if(0 == strcmp(cmd, CFG_PAR_MASTER)) { slave.amount = atoi(param); } else if(0 == strcmp(cmd, CFG_PAR_SLAVE)) { strcpy(srv_name, SLAVE_NAME); strcat(srv_name, param); } else if(0 == strcmp(cmd, CFG_PAR_CLIENTMAX)) { client_max = atoi(param); } } fclose(pfcfg); } else { perror(MSG_ERR_CFGFILE); } printf("%s\n", MSG_VER_CFG); signal(SIGINT, fsigint); // Net pnat = name_attach(NULL, srv_name, NAME_FLAG_ATTACH_GLOBAL); __ERROR_EXIT(pnat, NULL, "name_attach"); chid = pnat->chid; // Cash pcash = malloc(sizeof(cash_t)*client_max); //memset(pcash, NULL, sizeof(cash_t)*client_max); /*for(size_t i=0; i<client_max; ++i) { pcash[i].status = EMPTY; }*/ proute = malloc(sizeof(uint32_t)*client_max); // Slaves if(slave.amount > 0) { slave.pslave = malloc(sizeof(slave_info_t)*slave.amount); for(int i = 0; i< slave.amount; ++i) { slave.pslave[i].name = malloc(sizeof(char)*BUFFER_SIZE); strcpy(slave.pslave[i].name, SLAVE_NAME); itoa(i, buf, 10); strcat(slave.pslave[i].name, buf); slave.pslave[i].status = DOWN; slave.pslave[i].clientmax = CLIENT_MAX; slave.pslave[i].clientnow = 0; sem_init(&slave.pslave[i].sem, 0, 1); } connectslaves(&slave); slave_chid = ChannelCreate(0); __ERROR_EXIT(slave_chid, -1, "ChannelCreate"); } // Workers __ERROR_EXIT(sem_init(pwrk_sem, 0, 0), -1, "pwrk_sem: sem_init"); psync = malloc(sizeof(syncsig_t)*wrk_amount); pworker = malloc(sizeof(pthread_t)*wrk_amount); wrk_info_t wrk_info; wrk_info.psem = pwrk_sem; wrk_info.chid = pnat->chid; wrk_info.pcash = pcash; wrk_info.pslave = &slave; wrk_info.client_amount = client_max; wrk_info.wrk_amount = wrk_amount; wrk_info.proute = proute; wrk_info.psync = psync; if(slave.amount > 0) { for(size_t i = 0; i<wrk_amount; ++i) { wrk_info.id = i; pthread_create(&pworker[i], NULL, router, &wrk_info); } } else { for(size_t i = 0; i<wrk_amount; ++i) { wrk_info.id = i; pthread_create(&pworker[i], NULL, worker, &wrk_info); } } /* * __ERROR_CHECK(TimerTimeout(CLOCK_REALTIME , _NTO_TIMEOUT_RECEIVE , NULL, &wait_wrk_sem.tv_nsec, NULL), -1, "TimerTimeout"); */ printf("%s\n", MSG_VER_WORK); //***************************************************************************** while(working) { log_update(); sleep(1); } //***************************************************************************** for(size_t i = 0; i<wrk_amount; ++i) { pthread_kill(pworker[i], SIGKILL); } for(size_t i = 0; i<wrk_amount; ++i) { pthread_join(pworker[i], NULL); } if(slave.amount > 0) { for(size_t i = 0; i<slave.amount; ++i) { free(slave.pslave[i].name); } free(slave.pslave); } name_detach(pnat, NULL); free(pworker); free(psync); free(proute); log_update(); //log_stop(); return EXIT_SUCCESS; }