int main( int argc, char **argv) { int rcvid; struct my_msg msg; name_attach_t *attach; /* attach the name the client will use to find us */ /* our channel will be in the attach structure */ if ( (attach = name_attach( NULL, MY_SERV, 0 )) == NULL) // register a name in the namespace and create a channel { printf("server: failed to attach name, errno: %d\n", errno ); exit(1); } /* wait for the message from the client */ rcvid = MsgReceive( attach->chid, &msg, sizeof( msg ), NULL ); if ( msg.type == MSG_GIVE_PULSE ) { /* wait until it is time to notify the client */ sleep(2); /* deliver notification to client that client requested */ MsgDeliverEvent( rcvid, &msg.event ); printf("server: delivered event\n"); } else { printf("server: unexpected message\n"); } return 0; }
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(); } }
/** * 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); } }
int main(int argc, char* argv[]) { int ping_coid; int status; //status return value used for ConnectAttach and MsgSend struct _pulse *mypulse; struct _msg_info info; mss_t msg; name_attach_t *attach; setvbuf(stdout, NULL, _IOLBF, 0); if ((attach = name_attach(NULL, PONG, 0)) == NULL) { printf("server:failed to attach name, errno %d\n", errno ); exit(1); } if ((ping_coid = name_open(PING, 0)) == -1) { printf("failed to find ping, errno %d\n", errno ); exit(1); } status = MsgSendPulse(ping_coid, PULSE_PRIORITY, PULSE_CODE, 1); if (-1 == status) { perror("MsgSendPulse"); exit(EXIT_FAILURE); } while (1) { int rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), &info); if (rcvid == -1) { perror("MsgReceive"); break; } else if (rcvid == 0) { mypulse = (struct _pulse *) &msg; printf("Pulse code: %d, pulse value %d\n", mypulse->code, mypulse->value.sival_int); sleep(1); status = MsgSendPulse(ping_coid, PULSE_PRIORITY, PULSE_CODE, mypulse->value.sival_int + 1); } else { printf("Unexpcted message"); } } return EXIT_SUCCESS; }
int main(void) { int rcvid; msg_buf_t msg; int status; int checksum; name_attach_t* attach; setvbuf (stdout, NULL, _IOLBF, 0); //set IO to stdout to be line buffered attach = name_attach(NULL, SERVER_NAME, 0);//PUT CODE HERE to attach a name if(NULL == attach) { //was there an error creating the channel? perror("name_attach()"); //look up the errno code and print exit(EXIT_FAILURE); } while(1) { rcvid = MsgReceive(attach->chid, &msg, sizeof msg, NULL);//PUT CODE HERE to receive msg from client, store the receive id in rcvid if(rcvid == -1) { //was there an error receiving msg? perror("MsgReceive"); //look up errno code and print continue; //try receiving another msg } else if(rcvid == 0) { printf("received pulse, code = %d\n", msg.pulse.code); continue; } printf("received msg: %s\n", msg.cksum.string_to_cksum); checksum = calculate_checksum(msg.cksum.string_to_cksum); status = MsgReply(rcvid, EOK, &checksum, sizeof checksum);//PUT CODE HERE TO reply to client with checksum, store the return status in status if(-1 == status) { perror("MsgReply"); } } return 0; }
int main(int argc, char *argv[]) { uint32_t l; int id, id2; my_data_t msg; int rcvid; name_attach_t *name; printf("Welcome Onda\n"); if (ThreadCtl(_NTO_TCTL_IO, 0) < 0) { perror(NULL); return -1; } name = name_attach(NULL, "onda", NAME_FLAG_ATTACH_GLOBAL); if (name == NULL) { perror("Error0\n"); return -1; } /* attach GPIO interrupt */ id = InterruptAttach (33, gpio_isr_handler, NULL, 0, _NTO_INTR_FLAGS_PROCESS); /* attach timer interrupt */ id2 = InterruptAttach (45, timer_isr_handler, NULL, 0, _NTO_INTR_FLAGS_PROCESS); gpio5 = mmap_device_io(OMAP3530_GPIO_SIZE, OMAP3530_GPIO5_BASE); if (gpio5 == MAP_DEVICE_FAILED) { perror(NULL); return -1; } //gpt3 = mmap_device_io(OMAP3530_GPT_SIZE, OMAP3530_GPT3_BASE); gpt9 = mmap_device_io(OMAP3530_GPT_SIZE, OMAP3530_GPT9_BASE); if (gpt9 == MAP_DEVICE_FAILED) { perror(NULL); return -1; } sys = mmap_device_io(OMAP3530_SYSCTL_SIZE, OMAP3530_SYSCTL_BASE); if (sys == MAP_DEVICE_FAILED) { perror(NULL); return -1; } /* selecting mode 4 function - GPIO 139 * selecting pullup and mode 4 function - GPIO 138 */ #define SYS_CONF ((4 << 16) | ((1 << 8) | (1<<3) | 4)) #define SYS_MASK ~(0x10F010F) l = (in32(sys + 0x168) & SYS_MASK) | SYS_CONF; //l = (in32(sys + 0x168) & ~(7<<16) ) | (4 << 16); //out32(sys + 0x168, ((1<<3 | 4) << 16) | (1<<3) | 4); out32(sys + 0x168, l); /* setting mode 2 - PWM */ l = (in32(sys + 0x174) & ~7 ) | 2; out32(sys + 0x174, l); /* setting the PIN 138 to input * setting the PIN 139 to output */ l = (in32(gpio5 + OMAP2420_GPIO_OE) & ~(1 << 11)) | 1 << 10; out32(gpio5 + OMAP2420_GPIO_OE, l); /* enabling interrupt on both levels on GPIO 139 */ out32(gpio5 + OMAP2420_GPIO_RISINGDETECT, l << 10); out32(gpio5 + OMAP2420_GPIO_FALLINGDETECT, l << 10); out32(gpio5 + OMAP2420_GPIO_SETIRQENABLE1, l << 10); /* make sure timer has stop */ out32(gpt9 + OMAP3530_GPT_TCLR, 0); /* enabling the interrupt */ out32(gpt9 + OMAP3530_GPT_TIER, 2); //comentar se PWM /* configuring PWM */ out32(gpt9 + OMAP3530_GPT_TCLR, (1<<12) | (1<<10) | (1<<7)); //-- PWM out32(gpio5 + OMAP2420_GPIO_SETDATAOUT, (1 << 11)); printf("Esperando requisições\n"); while (1) { rcvid = MsgReceive(name->chid, &msg, sizeof(msg), NULL); if (rcvid == -1) {/* Error condition, exit */ break; } /* 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; } switch (msg.hdr.subtype) { case 0x55: MsgReply(rcvid, EOK, &pincount, sizeof(pincount)); break; case 0x65: MsgReply(rcvid, EOK, &interval, sizeof(interval)); break; case 0x66: interval = msg.data; MsgReply(rcvid, EOK, &interval, sizeof(interval)); break; default: MsgReply(rcvid, EOK, NULL, 0); } } out32(gpt9 + OMAP3530_GPT_TCLR, 0); out32(gpt9 + OMAP3530_GPT_TIER, 0); InterruptDetach (id); InterruptDetach (id2); printf("Fim\n"); return EXIT_SUCCESS; }
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[]) { name_attach_t *att; int rcvid; struct _msg_info msg_info; struct sigevent sigev; int self_coid; int achid; struct _asyncmsg_get_header *agh, *agh1; /* register my name so client can find me */ att = name_attach(NULL,RECV_NAME, 0 ); if (NULL == att ) { perror(PROGNAME "name_attach()"); exit(EXIT_FAILURE); } /* create a connection to the synchronous channel created by the * name_attach() call. Will use this to specify where the pulses * flagging async data available should be delivered. */ self_coid = ConnectAttach( 0, 0, att->chid, _NTO_SIDE_CHANNEL, 0 ); if( -1 == self_coid ) { perror(PROGNAME "ConnectAttach"); exit( EXIT_FAILURE ); } /* and fill in the event structure to describe a priority 10 pulse * to be delivered to my synchronous channel */ SIGEV_PULSE_INIT( &sigev, self_coid, 10, PULSE_ASYNCH_EVENT, 0 ); /* create an asynchrounous channel with automatic buffering * it will not block an asyncmsg_get() call if there are no messages available * it will receive up to 10 messages of up to 1024 bytes each at once * It will get a pulse notification when the queue of available messages goes * from empty to non-empty */ achid = asyncmsg_channel_create( _NTO_CHF_ASYNC_NONBLOCK, 0666, 1024, 10, &sigev, NULL ); if( -1 == achid ) { perror( "asyncmsg_channel_create"); exit( EXIT_FAILURE ); } while(1) { rcvid = MsgReceive( att->chid, &recv_buf, sizeof (recv_buf), &msg_info ); if( -1 == rcvid ) { perror(PROGNAME "MsgReceive failed"); continue; } if ( 0 == rcvid ) { /* we received a pulse */ printf("got a pulse\n"); switch( recv_buf.pulse.code ) { /* system disconnect pulse */ case _PULSE_CODE_DISCONNECT: ConnectDetach( recv_buf.pulse.scoid ); printf(PROGNAME "disconnect from a client %X\n", recv_buf.pulse.scoid); break; /* our pulse - we've got one or more messages */ case PULSE_ASYNCH_EVENT: /* get one or more messages from our channel */ agh = asyncmsg_get( achid ); if (NULL == agh ) { perror("went to get a message, but nothing was there"); } else { /* the async receive header is, actually, a linked list of headers * if multiple messages have been received at once, so we need to * walk the list, looking at each header and message in turn */ while( agh ) { printf("the message came from %d in %d parts\n", agh->info.pid, agh->parts); printf("the data is '%s'\n", (char *)(agh->iov->iov_base)); agh1 = agh; agh = agh1->next; /* free this message */ asyncmsg_free( agh1 ); } } break; default: printf(PROGNAME "unexpect pulse code: %d\n", recv_buf.pulse.code ); break; } continue; } /* not an error, not a pulse, therefor a message */ if ( recv_buf.type == _IO_CONNECT ) { /* _IO_CONNECT because someone did a name_open() to us, must EOK it.*/ MsgReply( rcvid, EOK, NULL, 0 ); continue; } if ( recv_buf.type > _IO_BASE && recv_buf.type <= _IO_MAX ) { /* unexpected system message,probably qconn, error it */ MsgError( rcvid, ENOSYS ); continue; } switch( recv_buf.type ) { /* here our client asked for our asynchronous channel id * reply with it */ case GET_ACHID: printf("got request for my achid\n"); MsgReply(rcvid, 0, &achid, sizeof(achid )); break; default: /* some other unexpect message */ printf(PROGNAME "unexpect message type: %d\n", recv_buf.type); MsgError(rcvid, ENOSYS ); break; } } }
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; }
int main(void) { int rcvid; name_attach_t* attach; msg_buf_t msg; int status; int checksum; struct _msg_info msg_info; listNode_t* list_ptr = NULL; setvbuf (stdout, NULL, _IOLBF, 0); attach = name_attach(NULL, "cksum", 0); if(attach == NULL) { //was there an error creating the channel? perror("name_attach"); //look up the errno code and print exit(EXIT_FAILURE); } while(1) { printf("Waiting for a message...\n"); rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), &msg_info); //PUT CODE HERE to receive msg from client if(rcvid == -1) { //was there an error receiving msg? perror("MsgReceive"); //look up errno code and print break; //try receiving another msg } else if(rcvid > 0) { //msg switch(msg.cksum.msg_type) { case _IO_CONNECT: //name_open() within the client may send this MsgReply(rcvid, EOK, NULL, 0); break; case CKSUM_MSG_TYPE: printf("Message received, client scoid = %d\n", msg_info.scoid); list_ptr = add_client_to_list(list_ptr, msg_info.scoid); print_list(list_ptr); checksum = calculate_checksum(msg.cksum.string_to_cksum); status = MsgReply(rcvid, EOK, &checksum, sizeof(checksum) ); if(-1 == status) { perror("MsgReply"); } break; default: MsgReply(rcvid, ENOSYS, NULL, 0); break; } } else if(rcvid == 0) { //message switch(msg.pulse.code) { case _PULSE_CODE_DISCONNECT: printf("received disconnect pulse from client, scoid = %d\n", msg.pulse.scoid); // list_ptr = remove_client_from_list(list_ptr, msg.pulse.scoid); // if(retp == list_ptr) { // printf("can't find client %d in list\n", msg.pulse.scoid); // } // ConnectDetach(msg.pulse.scoid); print_list(list_ptr); break; default: printf("unknown pulse received, code = %d\n", msg.pulse.code); } } else { printf("received unexpected msg with rcvid < -1 !!!\n"); } } return 0; }