int ext_send_data_msg(int rx_chan, bool debug, u1_t cmd, u1_t *bytes, int nbytes) { conn_t *conn = ext_users[rx_chan].conn; if (debug) printf("ext_send_data_msg: RX%d-%p cmd %d nbytes %d\n", rx_chan, conn, cmd, nbytes); if (!conn || !conn->mc) return -1; send_data_msg(conn, SM_NO_DEBUG, cmd, bytes, nbytes); return 0; }
// Loop for communication with last sensor void last_loop() { print_info("Last loop"); while(1) { if(mode == RECONF) { sleep(1); continue; } print_info("Waiting for data from last..."); unsigned char buf[MAX_DATA]; unsigned last_addr_len = sizeof(last_addr); int len = recvfrom(sock_last, buf, MAX_DATA, 0, (struct sockaddr*)&last_addr, &last_addr_len); if(len < 0) { mode = DOUBLE_LIST; resolving_error = 1; print_warning("Didn't receive data message from last sensor"); print_success("Mode changed to double-list"); if(send_error_msg(FIRST) < 0 || receive_ack_and_finit(FIRST) < 0) { close_first = 1; } if(send_error_msg(LAST) < 0 || receive_ack_and_finit(LAST) < 0) { resolving_error = 0; return; } resolving_error = 0; } else { union msg received_msg; int msg_type = unpack_msg(buf, &received_msg); switch(msg_type) { case INIT_MSG: take_init_msg(received_msg.init); break; case DATA_MSG: take_data_msg(received_msg.data); break; default: print_warning("Received unknown bytes"); } cleanup_msg(&received_msg); } usleep(SERVER_TIMEOUT*1000); if(mode == DOUBLE_LIST) { print_info("Sending data to last sensor..."); send_data_msg(LAST); } } }
// Loop for communication with first sensor void* first_loop(void* arg) { print_success("Loop thread started"); send_init_msg(); while(1) { if(close_first) return NULL; if(mode == RECONF) { sleep(1); continue; } if(resolving_error) { usleep(SERVER_TIMEOUT*1000); continue; } send_data_msg(FIRST); if(mode == DOUBLE_LIST) { print_info("Waiting for data from first..."); unsigned char buf[MAX_DATA]; unsigned first_addr_len = sizeof(first_addr); int len = recvfrom(sock_first, buf, MAX_DATA, 0, (struct sockaddr*)&first_addr, &first_addr_len); if(len < 0) { print_error("Second problem with network(not received msg from first sensor), terminating...."); exit(-1); } union msg received_msg; int msg_type = unpack_msg(buf, &received_msg); if(msg_type == DATA_MSG) take_data_msg(received_msg.data); else print_warning("Received unknown bytes"); } usleep(SERVER_TIMEOUT*1000); } return NULL; }
/* * put_file */ void put_file( char *fname) { FILE *fp=NULL; int fd, nTotal=0, nBytesRead=0, retval, aborted=0; char *abortstr = "ABORrn", ch; /* void (*OldHandler)(int); */ if( !fname || ! (*fname)) { printf("No file specified.n"); return; } if(! (fp=fopen(fname,(bMode==ASCII) ? "rt" : "rb"))) { perror("file open"); return; } if( get_listen_socket() < 0) { fclose(fp); return; } /* * send command to server & read reply */ sprintf(tmp_buffer,"STOR %srn",fname); if(!send_ctrl_msg(tmp_buffer,strlen(tmp_buffer))) { fclose(fp); return; } int m=get_host_reply(); if(m==550) return; /* * accept server connection */ if( accept_connection() <= 0) { fclose(fp); return; } /* * now send file */ fd = fileno(fp); printf("Type q and hit return to abortrn"); while( (nBytesRead=read(fd,tmp_buffer,1024)) > 0) { send_data_msg(tmp_buffer,nBytesRead); nTotal+=nBytesRead; printf("%s : %d sentr",fname,nTotal); if( check_input() ) { ch = getchar(); if( ch != 'n') { while( getchar() != 'n') ; /* read 'til new line */ } if( ch == 'q') aborted = 1; } /* * send an abort command to server if we aborted. */ if( aborted ) { printf("rnAbort: Waiting for server to finish."); send_ctrl_msg(abortstr,strlen(abortstr)); break; } } /*(void)signal(SIGINT,OldHandler); */ printf("rn"); /* * close data connection */ close_data_connection(hDataSocket); close(fd); get_host_reply(); }