int pce_log (const char * volatile buf, ...) { char d_buffer_2[PCE_PSTR_MAX]; if (!(get_msg_type ((char*) buf) & STDLOG_LVL)) { return 0; } va_list al; va_start(al, buf); if (NULL != fd_log_pce) { struct tm tmd; struct tm *tm = get_localtime (&tmd); snprintf (d_buffer_2, PCE_PSTR_MAX, "[%.2u/%.2u/%.2u %.2u:%.2u:%.2u] [%d] %s", tm->tm_mday, tm->tm_mon + 1, (tm->tm_year + 1900) % 100, tm->tm_hour, tm->tm_min, tm->tm_sec, getpid (), buf); char wl_buffer[PCE_PSTR_MAX]; vsnprintf (wl_buffer, PCE_PSTR_MAX, d_buffer_2, al); w_log_p (wl_buffer); } va_end(al); return 0; }
static int on_socket_readable(void *socket, int *stdin_fd, zmq_msg_t *stdin_msg, size_t *stdin_msg_pos) { while (1) { if (*stdin_msg_pos) return 0; if (zmq_msg_init(stdin_msg) == -1) { TRACE("zmq_msg_init() failed"); return -1; } if (zmq_msg_recv(stdin_msg, socket, ZMQ_DONTWAIT) == -1) { if (errno == EAGAIN) return 0; TRACE_ERRNO("zmq_msg_recv() failed"); return -1; } size_t msg_size = zmq_msg_size(stdin_msg); if (msg_size == 0) { TRACE("empty message received"); return -1; } char *msg_data = zmq_msg_data(stdin_msg); TRACE("received message, type=%i, size=%zu", get_msg_type(stdin_msg), msg_size); if (msg_data[0] == msg_type_stdin) { *stdin_msg_pos = 1; if (fd_write(stdin_fd, stdin_msg, stdin_msg_pos) == -1) return -1; } else { TRACE("message with unexpected type received"); return -1; } } }
//------------------------------------------------------------------------------ int get_msg_data_size(char *msg) { switch(get_msg_type(msg)) { #ifdef SIMULATION case WEBCAM: return ((MSG_INT1_t*)msg)->value; break; case WEBCAM_QUERY: return sizeof(MSG_INT1_t) - 2*sizeof(int); break; case MSG_INFO: return sizeof(picInfo_t) - 2*sizeof(int); break; #endif case BEGIN: case JACK: case EMPTY_MSG: return 0; break; case QUERY: case ACK: case BELT: case VPOS: case HPOS: case CLAMP: case PRINTF: return sizeof(MSG_INT1_t) - 2*sizeof(int); break; case MOTORS: case CODER: return sizeof(MSG_INT2_t) - 2*sizeof(int); break; case DIST: case COUL: return sizeof(int)*((MSG_INTn_t*)msg)->n; break; default: fprintf(stderr,"<comm.c> get_msg_data_size() : Unknown message type. %d\n",get_msg_type(msg)); fflush(stdout); break; } return 0; }
int send_to_client(ChatClient *cli, const char *msg, MsgType msg_type) { ChatPacket *pkt = packet_new(SERV_NAME, cli->name); pkt->nmsg = 1; pkt->msg[0] = strdup(msg); pkt->time = gettime(); pkt->type = get_msg_type(msg_type); cli->pktsnd = pkt; return client_flush(cli); }
// Arguments: // vslabc <infile> <outfile> <ip> int main(int argc, char **argv) { uint8_t gp[2], **out_data, iReturn, *ip_ptr; uint16_t in_data[BLOCKLEN], resBlockID, blockID = 0; uint32_t senderIP, length, i; bool newDataWanted = true; FILE *fpIn, *fpOut; msg msg; FID msg_type; out_data = malloc(sizeof(uint8_t*)); // introduce yourself printf("VSLab client, build %s %s\n\n", __DATE__, __TIME__); // check command line parameters if (argc < 4) { printf("Missing arguments!\n"); printf("Usage: main.c infile outfile server-ip\n"); return -1; } // open in-filepointer fpIn=fopen(argv[1],"rb"); if(!fpIn) { printf("Unable to open file!"); return -1; } // open out-filepointer fpOut = fopen(argv[2], "w"); // init socket init_client(1 /*clientID*/, 1 /*priority*/, 0 /*broadcast address*/); /********** SETTING GP ***************************************************/ // read in generator polynom length = fread(&gp,sizeof(uint8_t),2,fpIn); if(length != 2) { printf("Client: Reading in gp was not possible\n"); return ERROR; } while(1) { // send it out iReturn = send_gp_req((gp[0] << 8) + gp[1], inet_network(argv[3])); if(iReturn != NO_ERROR) { printf("Client: GP %#x sent out failed: %d\n", (gp[0] << 8) + gp[1], iReturn); sleep(1); continue; } else { printf("Client: GP %#x sent out to %s\n", (gp[0] << 8) + gp[1], argv[3]); } // receive gp-set-response tol_start_timeout(TOL_TIMEOUT_SECS); iReturn = recv_msg(&msg, &senderIP); tol_stop_timeout(); if(tol_is_timed_out()) { tol_reset_timeout(); printf("Client: Timeout // Server @ %s unreachable // Quit\n",argv[3]); return -1; //printf("Client: Timeout --> Send GP-Request again\n"); //continue; } msg_type = get_msg_type(&msg); if(iReturn != NO_ERROR || msg_type != GP_RSP) { if(iReturn == ERR_INVALIDVERSION) { printf("Client: Server is working with different protocol version: %d\n\n", iReturn); return -1; } else if(((error*)(msg.data))->errCode == ERR_NOSUCHFUNCTION) { printf("Client: Server is not familiar with function: %d\n\n", iReturn); return -1; } printf("Client: No gp response received: %d\n\n", iReturn); sleep(1); continue; } else { ip_ptr = (uint8_t*)&senderIP; printf("Client: Received gp response from %d.%d.%d.%d\n\n", ip_ptr[3],ip_ptr[2],ip_ptr[1],ip_ptr[0]); free_msg(&msg); break; } } /********** SCRAMBLING DATA **********************************************/ while(1) { // read in data if(newDataWanted) { length = fread(&in_data,sizeof(uint16_t),BLOCKLEN,fpIn); if(length < 1) break; for(i=0; i<length; i++) { in_data[i] = htons(in_data[i]); // correcting byteorder } printf("Client: Read in data: "); if(BLOCKLEN < 20) for(i=0; i<length; i++) printf("%#x ", in_data[i]); else printf("..."); printf("\n"); newDataWanted = false; } // send it out iReturn = send_dec_req(blockID, in_data, length, inet_network(argv[3])); if(iReturn != NO_ERROR) { printf("Client: Sending out data was not successful: %d\n", iReturn); sleep(0.5); continue; } else { printf("Client: Sending out data worked\n"); } // receive scrambled data tol_start_timeout(TOL_TIMEOUT_SECS); iReturn = recv_msg(&msg, &senderIP); tol_stop_timeout(); if (tol_is_timed_out()) { tol_reset_timeout(); printf("Client: Timeout --> Send data again (Block-ID %d)\n", blockID); continue; } msg_type = get_msg_type(&msg); if(iReturn != NO_ERROR || msg_type != DECRYPT_RSP) { printf("Client: No data response received: %d\n", iReturn); sleep(1); continue; } else { ip_ptr = (uint8_t*)&senderIP; printf("Client: Received data response from %d.%d.%d.%d\n", ip_ptr[3],ip_ptr[2],ip_ptr[1],ip_ptr[0]); } // print & write out result iReturn = extract_dec_rsp(&msg, &resBlockID, out_data, &length); if(iReturn != NO_ERROR) { printf("Client: An error occured during extracting the data packet: %d\n\n", iReturn); free_msg(&msg); free_data(*out_data); sleep(1); continue; } else if(resBlockID != blockID) { printf("Client: Received blockID %d, expected blockID %d\n\n", resBlockID, blockID); free_msg(&msg); free_data(*out_data); sleep(1); continue; } else { printf(" with length %d and block id %d\n", length, blockID); (*out_data)[length] = '\0'; fwrite(*out_data, 1, length, fpOut); if(BLOCKLEN < 20) printf("Client: Received data: %s \n", *out_data); printf("\n"); blockID++; newDataWanted = true; free_data(*out_data); free_msg(&msg); } } /********** UNLOCK SERVER ************************************************/ // unlock server iReturn = send_unlock_req(inet_network(argv[3])); if(iReturn != NO_ERROR) { printf("Client: Sending unlock request was not successful: %d\n", iReturn); } else { printf("Client: Sending unlock request worked\n"); } // receive unlock response tol_start_timeout(TOL_TIMEOUT_SECS); iReturn = recv_msg(&msg, &senderIP); tol_stop_timeout(); if (tol_is_timed_out()) { tol_reset_timeout(); printf("Client: Timeout --> No Unlock Response received\n"); } else { msg_type = get_msg_type(&msg); if(iReturn != NO_ERROR || msg_type != UNLOCK_RSP) { printf("Client: No unlock response received: %d\n\n", iReturn); } else { ip_ptr = (uint8_t*)&senderIP; printf("Client: Received unlock response from %d.%d.%d.%d\n\n", ip_ptr[3],ip_ptr[2],ip_ptr[1],ip_ptr[0]); } } /*****************************************************************************/ // deinit socket deinit_client(); // close filepointer fclose(fpIn); fclose(fpOut); printf("Client: Scrambling %s is done!\n", argv[1]); return 0; }
/** * @brief 分析客户端输入的数据 * * @param cli 客户端数据结构 * @param input 输入的数据 * * @return */ int client_parse_input(ChatClient *cli, char *input) { int ret = 0; int file_flag = 0; input = str_strip(input); //去掉字符串首尾的空格 if(input == NULL) { return -1; } char *to = NULL; char *msg = NULL; ServerCmd cmd = CMD_LAST; packet_free(cli->pktsnd); //清空发送数据包 cli->pktsnd = NULL; if(strncmp("to ", input, 3) == 0) //比较前三个字符 { input += 3; if(line_parse(input, ':', &to, &msg) == 0) //以:为分隔符,将字符串分成两段 { cli->pktsnd = packet_new(cli->name, to); } } else if(strncmp("file to ", input, 8) == 0) { input += 8; if(line_parse(input, ':', &to, &msg) == 0) //以:为分隔符,将字符串分成两段 { cli->pktsnd = packet_new(cli->name, to); } file_flag = 1; } else if(strcmp(input, "whoison") == 0) { cli->pktsnd = packet_new(cli->name, SERV_NAME); cmd = CMD_WHOISON; msg = (char *) g_cmd[cmd]; } else if(strcmp(input, "showuser") == 0) { cli->pktsnd = packet_new(cli->name, SERV_NAME); cmd = CMD_SHOWUSER; msg = (char *) g_cmd[cmd]; } else if(strcmp(input, "help") == 0) { printf("\rto usr: msg --------- send msg to 'usr'\n"); printf("file to usr: filepath --------- send file to 'usr'\n"); printf("whoison --------- check server who is online\n"); printf("howuser --------- check all users who are registered\n"); printf("logout/bye/exit --------- logout and exit client\n"); return 0; } else if(strcmp(input, "logout") == 0 || strcmp(input, "exit") == 0 || strcmp(input, "bye") == 0) { cli->pktsnd = packet_new(cli->name, SERV_NAME); cmd = CMD_LOGOUT; msg = (char *) g_cmd[cmd]; } if(file_flag) { cli->pktsnd->type = get_msg_type(MSG_FILE_SEND); //设置发送的是文件类型 packet_add_msg(cli->pktsnd, msg); FILE *fp = fopen(msg, "r"); char buffer[MAXLEN]; if (fp == NULL) { printf("File: %s Not Found!\n", msg); } else { bzero(buffer, MAXLEN); while(fgets(buffer, MAXLEN, fp) != NULL) { packet_add_msg(cli->pktsnd, buffer); bzero(buffer, MAXLEN); } fclose(fp); printf("File:\t%s Transfer Finished!\n", msg); } ret = client_flush(cli); } if(msg && cli->pktsnd) { cli->pktsnd->type = get_msg_type(MSG_TEXT_SEND); //设置发送的是文字类型 packet_add_msg(cli->pktsnd, msg); ret = client_flush(cli); if(cmd==CMD_LOGOUT) { exit(0); } } return ret; }
//the meat of the client. performs all sending of data to the server, as well as calling methods //to build the various request types. void client_operations(int sockfd, host* host, hfs_entry* files, char* auth_token, char* username, int num_files) { int seq_num = 0; hfs_entry* current_file = &files[0]; int current_file_index = 1; while(current_file) { //get length of file in bytes FILE* fp = fopen(current_file->abs_path, "r"); fseek(fp, 0L, SEEK_END); if(!fp) { syslog(LOG_ERR, "Error reading file %s, could not include in request.", current_file->abs_path); fclose(fp); continue; } uint32_t f_size = ftell(fp); fseek(fp, 0L, SEEK_SET); fclose(fp); msg* control_msg = build_control_init_request(seq_num, strlen(current_file->rel_path), f_size, current_file->crc32, auth_token, current_file->rel_path); //something is wrong with network order here. // control_msg->length = htonl(control_msg->length); char* msg_type_str = get_msg_type(control_msg->buffer[0]); syslog(LOG_DEBUG, "Sending %s msg. Sequence # %d. Data size: %d", msg_type_str, control_msg->buffer[1], control_msg->length); free(msg_type_str); send_msg(sockfd, control_msg, host); int errcode = wait_for_ack(control_msg, sockfd, seq_num, host); //make sure no error! if(errcode) { syslog(LOG_EMERG, "Received error code from HFTP server. Likely bad auth token."); errx(EXIT_FAILURE, "Received error code from HFTP server. Likely bad auth token."); } // free(control_msg->buffer); free(control_msg); int index = 0, prev_index = 0, interval_counter = 0; //while file is still being sent while(index < f_size+1) { seq_num = !seq_num; //data request - index is out param to say where in the file has already been processed msg* data_msg = data_from_file(current_file->abs_path, f_size, seq_num, &index); msg_type_str = get_msg_type(data_msg->buffer[0]); //not necessarily whole file; check with index < f_size+1 syslog(LOG_DEBUG, "Sending %s msg. Sequence # %d. Data size: %d", msg_type_str, data_msg->buffer[1], data_msg->length); free(msg_type_str); send_msg(sockfd, data_msg, host); //need error check here? wait_for_ack(data_msg, sockfd, seq_num, host); // free(data_msg->buffer); free(data_msg); interval_counter += index - prev_index; prev_index = index; if(interval_counter >= OUTPUT_INTERVAL) { interval_counter = 0; if(f_size != 0) { syslog(LOG_INFO, "Transferring file %s, %d of %d. %d bytes successfully transferred of %d, %.1lf%% complete.", current_file->rel_path, current_file_index, num_files, index, f_size, ((double)index/(double)f_size)*100); } } } syslog(LOG_INFO, "Finished transferring %s, %d of %d.", current_file->rel_path, current_file_index, num_files); seq_num = !seq_num; current_file = current_file->next; current_file_index++; } msg_control* term_msg_control = (msg_control*)calloc(1, sizeof(msg_control)); term_msg_control->type = TYPE_CONTROL_TERM; term_msg_control->seq = seq_num; term_msg_control->length = sizeof(term_msg_control->type + term_msg_control->seq); msg* term_msg = (msg*)term_msg_control; send_msg(sockfd, term_msg, host); wait_for_ack(term_msg, sockfd, seq_num, host); free(term_msg_control); }
//------------------------------------------------------------------------------ void* pic1_handle_msg(void *) { if(!WANT_PIC1) while(true) usleep(100000); char *_msg; while(true) { if(connect_usb(PIC1)<0) { fprintf(stderr,"Connection failed PIC1, retrying...\n"); sleep(1); continue; } if(callbackOnReconnectPIC1) callbackOnReconnectPIC1(); fprintf(stderr,"Listen to PIC1...\n"); fflush(stdout); while((_msg=read_usb(PIC1))) { if(_msg==NULL) break; //printf("Recv PIC1: %d\n",get_msg_type(_msg)); switch(get_msg_type(_msg)) { case EMPTY_MSG: #ifdef SIMULATION fprintf(stderr,"<picInterface.cpp> PIC1: Connexion réinitialisée.\n"); fflush(stdout); if(callbackRecvReset) callbackRecvReset(); #endif break; case CODER: #ifdef SIMULATION picSendInfo(); #endif if(callbackRecvCoder) { MSG_INT2_t *msg = (MSG_INT2_t *)_msg; #ifdef SIMULATION callbackRecvCoder(msg->value1, msg->value2); #else callbackRecvCoder(msg->value2, -msg->value1); #endif } break; case PRINTF: { MSG_INT1_t *msg = (MSG_INT1_t *)_msg; printf("from pic %d: %d\n",1,msg->value); } break; default: fprintf(stderr,"<picInterface.cpp> PIC1: Unknown message type %d.\n",get_msg_type(_msg)); fflush(stdout); break; } delete _msg; } fprintf(stderr,"Connection closed PIC1...\n"); fflush(stdout); usleep(10000); break; } return NULL; }
//------------------------------------------------------------------------------ void* pic2_handle_msg(void *) { if(!WANT_PIC2) while(true) usleep(100000); char *_msg; while(true) { if(connect_usb(PIC2)<0) { fprintf(stderr,"Connection failed PIC2, retrying...\n"); sleep(1); continue; } picBegin(); if(callbackOnReconnectPIC2) callbackOnReconnectPIC2(); fprintf(stderr,"Listen to PIC2...\n"); fflush(stdout); while((_msg=read_usb(PIC2))) { if(_msg==NULL) break; //printf("Recv PIC2: %d\n",get_msg_type(_msg)); switch(get_msg_type(_msg)) { case EMPTY_MSG: #ifdef SIMULATION fprintf(stderr,"<picInterface.cpp> PIC2: Connexion réinitialisée.\n"); fflush(stdout); if(callbackRecvReset) callbackRecvReset(); #endif break; #ifdef SIMULATION case WEBCAM: { MSG_INT1_t *msg = (MSG_INT1_t *)_msg; int size = msg->value; int *ptr = &msg->value; ptr++; *((char**)webcam_data) = new char[size]; memcpy(*((char**)webcam_data),ptr,size); pthread_mutex_unlock(mut_webcam); mut_webcam = NULL; } break; #endif case BEGIN: break; case JACK: { callbackOnJack(); #ifdef SIMULATION int buf = JACK; if(write_usb(PIC2, &buf, sizeof(int))<0) break; #else unsigned char buf = JACK; if(write_usb(PIC2, &buf, sizeof(buf))<0) break; #endif } break; case PRINTF: { MSG_INT1_t *msg = (MSG_INT1_t *)_msg; printf("from pic %d: %d\n",2,msg->value); } break; case VPOS: if(mut_vpos) { if(true /*msg->msg_id == msg_id_vpos*/) { msg_id_vpos = 0; pthread_mutex_unlock(mut_vpos); mut_vpos = NULL; } } else fprintf(stderr,"<picInterface.cpp> PIC2: Unmatched ACK VPOS.\n"); fflush(stdout); break; case HPOS: if(mut_hpos) { if(true /*msg->msg_id == msg_id_hpos*/) { msg_id_hpos = 0; pthread_mutex_unlock(mut_hpos); mut_hpos = NULL; } } else fprintf(stderr,"<picInterface.cpp> PIC2: Unmatched ACK HPOS.\n"); fflush(stdout); break; case CLAMP: /*if(mut_clamp) { if(msg->msg_id == msg_id_clamp) { msg_id_clamp = 0; pthread_mutex_unlock(mut_clamp); mut_clamp = NULL; } } else printf("<picInterface.cpp> PIC2: Unmatched ACK CLAMP.\n"); fflush(stdout); */ break; case DIST: if(callbackRecvDistCaptors) { MSG_INTn_t *msg = (MSG_INTn_t *)_msg; float *tab; if(msg->n == 0) tab = NULL; else tab = new float[msg->n]; int *values = &msg->n; values++; for(int i=0; i<msg->n; i++) { tab[i] = COEFF_A_DIST * pow(COEFF_B_DIST, ((double)(values[i]>>6))); if(tab[i]<0.) tab[i] = 0.; } #ifdef SIMULATION float tmp = tab[1]; values[1] = tab[2]; values[2] = tmp; #endif callbackRecvDistCaptors(msg->n, tab); if(tab != NULL) delete tab; } break; case COUL: if(callbackRecvCoulCaptors) { MSG_INTn_t *msg = (MSG_INTn_t *)_msg; float *tab = new float[msg->n]; int *values = &msg->n; values++; for(int i=0; i<msg->n; i++) tab[i] = ((float)values[i]); callbackRecvCoulCaptors(msg->n, tab); delete tab; } break; default: fprintf(stderr,"<picInterface.cpp> PIC2: Unknown message type %d.\n",get_msg_type(_msg)); fflush(stdout); break; } delete _msg; } fprintf(stderr,"Connection closed PIC2...\n"); fflush(stdout); usleep(10000); break; } return NULL; }