int readreq(int con, char *recv) { if( Rio_readn(con, recv, 4) != 4) { #ifdef DEBUG printf("server receive bytes < 4 bytes\n"); #endif return 1; } int len = atoi(recv); Rio_readn(con, recv + 4, len); #ifdef DEBUG printf("server receive %d bytes\n%s\n" , strlen(recv), recv); #endif runlog("", 0, "Recv=[%s]\n", recv); return 0; }
void *func(void *arg) { int n = (int) arg; char rbuf[5]; const char *msg = "test"; int clientfd = Open_clientfd("127.0.0.1", 12400); int i; for (i = 0; i < n; ++i) { Rio_writen(clientfd, msg, 5); Rio_readn(clientfd, rbuf, 5); } Close(clientfd); return NULL; }
void serve_static(int fd, char *filename, int filesize) { char filetype[MAXLINE], buf[MAXBUF]; /* Send response headers to client */ get_filetype(filename, filetype); snprintf(buf, sizeof(buf), "HTTP/1.0 200 OK\r\n"); snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "Server: Tiny Web Server\r\n"); snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "Content-length: %d\r\n", filesize); snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "Content-type: %s\r\n\r\n", filetype); Rio_writen(fd, buf, strlen(buf)); /* * Send response body to client * * If the file is big enough, we read many times, and each time we * double increase buffer. */ int srcfd = Open(filename, O_RDONLY, 0); char *body = NULL; const int min_size = (1<<10); /* 1KB */ int max_size = (unsigned)INT_MIN >> 1; /* 16bit: 16KB, 32bit: 1GB */ size_t size = min_size; for (;;) { if (size != max_size) { char *old_body = body; body = realloc(old_body, size); if (body == NULL) { if (size == min_size) { free(old_body); perror("realloc"); break; } else { /* size > min_size */ max_size = size >> 1; size = max_size; body = old_body; } } } int n = Rio_readn(srcfd, body, size); if (n > 0) /* read something */ Rio_writen(fd, body, n); if (n != size) /* EOF or read all the content */ break; if (size != max_size) size <<= 1; /* increase buffer, read more next time */ }
int main(int argc, char **argv) { int clientfd, port; char *host, buf[MAX_LINE]; if (argc == 2 || argc == 3) { host = argv[1]; port = (argc == 2) ? DEFAULT_PORT : atoi(argv[2]); } else { fprintf(stderr, "usage: %s <host>\n", argv[0]); exit(0); } clientfd = Open_clientfd(host, port); printf("Bank client v.%d connected to %s on port %d\n", VERSION, host, port); msg_t *request = new_msg(); msg_t *response = new_msg(); printf("> "); fflush(stdout); while (Fgets(buf, MAX_LINE, stdin) != NULL) { if (parse_buf(buf, request)) { Rio_writen(clientfd, (void *) request, sizeof(msg_t)); // Send bits to server Rio_readn(clientfd, (void *) response, sizeof(msg_t)); // Get bits back from server print_response(response); } clear_msg(request); clear_msg(response); printf("> "); fflush(stdout); } Close(clientfd); free(request); free(response); exit(0); }
void send_response_body(int fd, char *filename) { /* * Send response body to client * * If the file is big enough, we read many times, and each time we * double increase buffer. */ int srcfd = Open(filename, O_RDONLY, 0); char *body = NULL; const int min_size = (1<<10); /* 1KB */ int max_size = (unsigned)INT_MIN >> 1; /* 16bit: 16KB, 32bit: 1GB */ size_t size = min_size; for (;;) { if (size != max_size) { char *old_body = body; body = realloc(old_body, size); if (body == NULL) { if (size == min_size) { free(old_body); perror("realloc"); break; } else { /* size > min_size */ max_size = size >> 1; size = max_size; body = old_body; } } } int n = Rio_readn(srcfd, body, size); if (n > 0) /* read something */ Rio_writen(fd, body, n); if (n != size) /* EOF or read all the content */ break; if (size != max_size) size <<= 1; /* increase buffer, read more next time */ }
char *read_until(int fd, char *pattern) { int buf_size = 2, result_size = 8192; char buf[buf_size]; char *result = malloc(result_size); // printf("S-read_until:\n"); int rc; int next = 0; bzero(result, result_size); while ( next < result_size - 1) { bzero(buf, buf_size); rc = Rio_readn(fd, buf, buf_size - 1); if (rc > 0) { strncat(result, buf, result_size - next - 1); next += rc; if ( next >= strlen(pattern) && strmatch(result+next, pattern) ) { break; } } } // printf("S-read_until returns\n"); return result; }
int doreq(Recv *R, Send *S, void *dbcon) { struct water winfo = { "", 0.0, 0, Calloc(MONTHNUM,sizeof(struct bill) )} ; //水费账户/ struct bank binfo = { "", 0.0, Calloc(1,sizeof(struct pay))}; //银行账户 struct user person = { "", "", &winfo, &binfo }; //用户信息 char *pbrecv; char buf[ HEADLEN_30 +1]; sscanf(R->buffer, RECVHEAD, &(R->len), &(R->code), &(R->filelen)); //填充报文头结构 pbrecv = R->buffer + 30; switch(R->code) { case UQUERY: S->len = SLEN7000; sscanf(pbrecv, RFORMAT7000, person.pwater->account ); //获取用户编号 do7000(S, &person, R, dbcon); break; case SIGN: sscanf(pbrecv, RFORMAT7001, person.pwater->account, person.name, person.addr, person.pbank->account ); do7001(S, &person, R, dbcon); break; case MODIFY: sscanf(pbrecv, RFORMAT7002, person.pwater->account, person.name, person.addr, person.pbank->account ); do7002(S, &person, R, dbcon); break; case DELETE: sscanf(pbrecv, RFORMAT7003, person.pwater->account, person.name, person.addr, person.pbank->account ); do7003(S, &person, R, dbcon); break; case BQUERY: sscanf(pbrecv, RFORMAT7004 , person.pwater->account); do7004(S, &person, R, dbcon); S->len = HEADLEN_26 + BODYLEN7004 + person.pwater->months*30; // 30 = 2(月份) + 12(金额) + 12(滞纳金) break; case BPAY: sscanf(pbrecv, RFORMAT7005, person.pwater->account, person.name, person.addr, &(person.pbank->table->money), person.pbank->table->order, person.pbank->table->date); do7005(S, &person, R, dbcon); break; case BREDO: sscanf(pbrecv, RFORMAT7006, person.pwater->account, person.name, person.addr, &person.pbank->table->money, person.pbank->table->order, person.pbank->table->date); do7006(S, &person, R, dbcon); break; case BDAILY: { char date[9] = {""}; int count = 0; float sum = 0; FILE *fp; char file[30] = {""}; char usrbuf[110] = {""}; //110为一条记录的长度,包含\n + \0 int num = R->filelen / 109; sscanf(pbrecv, RFORMAT7007, date, &count, &sum); do7007(S, date, count, sum, R, dbcon); //接受银行详细对账账单,并写入文本 sprintf(file,"./7-%s.txt", GetSysDate(1)); if((fp = fopen(file, "w")) == NULL) { S->result = ERRORFILE; break; } while(num > 0 ) { num--; //printf("server receive %d bytes %s\n",n,usrbuf); //Rio_writen(fileno(fp), usrbuf, Rio_readn(conn, usrbuf, sizeof(usrbuf))); Rio_readn(R->conn, usrbuf, sizeof(usrbuf)-1); fputs(usrbuf,fp); } fclose(fp); } break; case BMONTHQ: do7008(S, R, dbcon); break; case BMONTHP: { char line[115]; int res = 0; //00代表银行扣款成功,99代码扣款失败 //读取每一行记录,如扣款成功,则将欠款表记录移至缴费表 while(Rio_readn(R->conn, line, sizeof(line))) { sscanf(line, "%12s%60s%20s%12f%8s%2d", person.pwater->account, person.name, person.pbank->account, &person.pbank->table->money, person.pbank->table->date, &res); do7009(&person, res, dbcon); memset(line, 0 , sizeof(line)); } } break; default: app_error("No this Code ,Please check"); break; } //sprintf会在结尾+NULL, 故使用memcpy将报文头拷贝至Buffer中 //PS sprintf函数不安全,可能发生缓冲区溢出,建议选用snprintf sprintf(buf,SENDHEAD, S->len , R->code, S->result, S->filelen); memcpy(S->buffer, buf, HEADLEN_30); Free(person.pbank->table); Free(person.pwater->table); }
void* request (void* data){ pthread_detach(pthread_self()); int serverfd=*(int*)data; int clientfd; char headerBuff[MAXHEADERSIZE]; char RequestHeader[MAXHEADERSIZE]; char hostname[MAXHOSTSIZE]; char Buffer[MAXBUFFERSIZE]; char* urlname; char* cache_buff; int cache_size = 0; int error; int byte; int getbyte; int cachehit = 0; Signal(SIGPIPE, SIG_IGN); free(data); rio_t serverrio_t; // Rio_readinitb(&serverrio_t,serverfd); /*set the structure, and set port to 80*/ printf("to Start read %u\n",pthread_self()); byte=Rio_readn(serverfd,headerBuff,sizeof(headerBuff)); // byte=read(serverfd,headerBuff,sizeof(headerBuff)); printf("finish read byte:%d %u\n",byte,pthread_self()); if(byte<=0){ close(serverfd); pthread_exit(NULL); } printf("%s\n",headerBuff); fflush(stdout); /*Check if the webpage is cached. if cache hit, no need to connect to server*/ if(takeurlname(headerBuff,&urlname) == 0){ perror("Cannot find url name\n"); close(serverfd); pthread_exit(NULL); } printf("Try to get cached data: %u\n",pthread_self()); /* Try to get cached data, must check no thread writing the cache, * and use FILO architecture to lock when there threads reading cache*/ /* P(&mutex); readcnt++; if(readcnt == 1) P(&w); V(&mutex); if( Get_cachedata(urlname,serverfd) > 0){ cachehit = 1; } P(&mutex); readcnt--; if(readcnt == 0) V(&w); V(&mutex); if(cachehit){ close(serverfd); pthread_exit(NULL); } */ printf("No cached data try connect: %u\n",pthread_self()); /* Webpage not cached, retrieve information from web server */ if( (clientfd=socket(AF_INET,SOCK_STREAM,0))<0){ perror("socket"); close(serverfd); pthread_exit(NULL); } // Rio_readinitb(&clientrio_t,clientfd); /* Set the request to the server */ if( settherequest(headerBuff,RequestHeader,hostname) == 0){ perror("Set Request Error\n"); close(serverfd); pthread_exit(NULL); } printf("%s\n",RequestHeader); fflush(stdout); /*get server's address info*/ struct addrinfo *hostinfo; if( (error=getaddrinfo(hostname,"http",NULL,&hostinfo)) >0){ perror("hostinfo"); close(serverfd); pthread_exit(NULL); } /* connect to the server*/ if( (connect(clientfd,hostinfo->ai_addr,hostinfo->ai_addrlen))<0){ perror("connect"); close(serverfd); return NULL; } printf("Ready to transmitt data: %u\n",pthread_self()); /*pass the request from browser to server*/ Rio_writen(clientfd,RequestHeader,byte); /*read the information from web server and send it to browser*/ cache_buff = (char*) malloc(MAX_OBJECT_SIZE*sizeof(char)); bzero(cache_buff,MAX_OBJECT_SIZE); while(1){ // getbyte=read(clientfd,Buffer,sizeof(Buffer)); getbyte=Rio_readn(clientfd,Buffer,sizeof(Buffer)); if(getbyte<=0) break; if(cache_size + getbyte < MAX_OBJECT_SIZE){ memcpy(cache_buff + cache_size,Buffer,getbyte); } cache_size += getbyte; Rio_writen(serverfd,Buffer,getbyte); } printf("Finish transmitt data: %u\n",pthread_self()); /* if size fit in cache, write into cache, else free the content*/ /* if(cache_size < MAX_OBJECT_SIZE){ char* new_cache_buff = realloc(cache_buff,cache_size); free(cache_buff); if(new_cache_buff == NULL){ perror("Realloc fail\n"); close(clientfd); close(serverfd); pthread_exit(NULL); } P(&w); Create_cache(urlname,new_cache_buff,cache_size); Cache_checker(); V(&w); } else{ free(cache_buff); } */ printf("Finish cached data: %u\n",pthread_self()); close(clientfd); close(serverfd); pthread_exit(NULL); }
int main(int argc, char *argv[]) { int listenfd, connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; int acc_count = 0; //if (strcmp(argv[2], "compute-0-29.local") == 0) //{ printf("%s listening\n", argv[2]); listenfd = Open_listenfd ("15618"); //} sleep(5); //int send_val = 5; printf("Hostname is %s\n", argv[2]); printf("Int got as %d\n", atoi(argv[1])); if (strcmp(argv[2], "compute-0-29.local") == 0) { while (1) { clientlen = sizeof (clientaddr); // accept connections connfd = Accept (listenfd, (SA *) & clientaddr, &clientlen); Getnameinfo ((SA *) & clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf ("Accepted connection from (%s, %s). Connfd is %d\n", hostname, port, connfd); //newfd = (int *) malloc (sizeof (int)); //newfd = connfd; // go serve this client! // pthread_create (&tid, NULL, doit, newfd); acc_count++; double send_double = 232.23; int retval = Rio_writen (connfd, (void *)&send_double, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", connfd); unix_error ("Rio_writen error"); } retval = Rio_writen (connfd, (void *)&send_double, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", connfd); unix_error ("Rio_writen error"); } int len = Rio_readn (connfd, (void *)&send_double, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and receive_val as %lf\n", argv[2], len, send_double); len = Rio_readn (connfd, (void *)&send_double, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and receive_val as %lf\n", argv[2], len, send_double); if (acc_count == 3) { printf("Accepted 3 connections.\n"); break; } } } else { int serverfd = Open_clientfd ("10.22.1.241", "15618"); printf("In host %s, serverfd is %d\n", argv[2], serverfd); double buf; int len = Rio_readn (serverfd, (void *)&buf, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and buf as %lf\n", argv[2], len, buf); len = Rio_readn (serverfd, (void *)&buf, sizeof(double)); if (len < 0) { unix_error ("Rio_readlineb error"); } printf("Host %s got len as %d and buf as %lf\n", argv[2], len, buf); buf = 99.104; int retval = Rio_writen (serverfd, (void *)&buf, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", serverfd); unix_error ("Rio_writen error"); } retval = Rio_writen (serverfd, (void *)&buf, sizeof(double)); if (retval < 0) { printf("Rio_writen to %d encountered a problem.\n", serverfd); unix_error ("Rio_writen error"); } } return 0; }
void *accept_commands(char *hostname, int connfd) { int opcode; while (1) { printf("Node %s waiting for a command\n", hostname); Rio_readn (connfd, (void *)&opcode, sizeof(int)); switch (opcode) { case COMPUTE_LOG_LIKELIHOOD: { printf("Node %s will start computing log likelihood\n", hostname); double ll = 0.0; /* for(int i = worker_id; i < numchunks; i+=total_workers){ std::string ipfile = prefix_input_file_name + std::to_string(i) + std::string(".txt"); std::string labfile = prefix_label_file_name + std::to_string(i) + std::string(".txt"); read_trainingdata_and_copy_to_GPU(ipfile, labfile); ll += compute_log_likelihood(); } */ pthread_t reader_thread; done_reading = false; pthread_create(&reader_thread, NULL, background_reader, NULL); int cur_pointer = 0; for (int i = worker_id; i < numchunks; i += total_workers) { while (1) // wait while thread is reading data { if (done_reading == true) { break; } } printf("cur_pointer in log likelihood is %d\n", cur_pointer); X_host = X_host_buffers[cur_pointer]; labels_host = labels_host_buffers[cur_pointer]; cur_pointer = cur_pointer + 1; cur_pointer %= 2; copy_training_data_to_GPU(X_host, labels_host); done_reading = false; ll += compute_log_likelihood(); } printf("Waiting for join!\n"); pthread_join(reader_thread, NULL); Rio_writen (connfd, (void *)&ll, sizeof(double)); break; } case COMPUTE_GRADIENT_LOG_HYPERPARAMS: { printf("Node %s will start computing gradient log hyperparams\n", hostname); double grads[3] = {0.0}; double temp[3]; /* for(int i = worker_id; i < numchunks; i+=total_workers){ std::string ipfile = prefix_input_file_name + std::to_string(i) + std::string(".txt"); std::string labfile = prefix_label_file_name + std::to_string(i) + std::string(".txt"); read_trainingdata_and_copy_to_GPU(ipfile, labfile); compute_gradient_log_hyperparams(temp); for(int j = 0 ; j < 3; j++){ grads[j] += temp[j]; } } */ pthread_t reader_thread; done_reading = false; pthread_create(&reader_thread, NULL, background_reader, NULL); int cur_pointer = 0; for (int i = worker_id; i < numchunks; i += total_workers) { while (!done_reading); // wait while thread is reading data printf("cur_pointer in log likelihood is %d\n", cur_pointer); X_host = X_host_buffers[cur_pointer]; labels_host = labels_host_buffers[cur_pointer]; cur_pointer = cur_pointer + 1; cur_pointer %= 2; copy_training_data_to_GPU(X_host, labels_host); done_reading = false; compute_gradient_log_hyperparams(temp); for(int j = 0; j < 3; j++){ grads[j] += temp[j]; } } pthread_join(reader_thread, NULL); //compute_gradient_log_hyperparams(grads); Rio_writen (connfd, (void *)&grads[0], sizeof(double)); Rio_writen (connfd, (void *)&grads[1], sizeof(double)); Rio_writen (connfd, (void *)&grads[2], sizeof(double)); break; } case GET_LOGHYPERPARAMS: { printf("Node %s will start returning log hyperparams\n", hostname); double *log_hyperparams = get_loghyperparam(); // send the same value thrice Rio_writen (connfd, (void *)&log_hyperparams[0], sizeof(double)); Rio_writen (connfd, (void *)&log_hyperparams[1], sizeof(double)); Rio_writen (connfd, (void *)&log_hyperparams[2], sizeof(double)); break; } case SET_LOGHYPERPARAMS: { printf("Node %s expecing log hyperparams to be set\n", hostname); double new_log_hyperparams[3]; Rio_readn (connfd, (void *)&new_log_hyperparams[0], sizeof(double)); Rio_readn (connfd, (void *)&new_log_hyperparams[1], sizeof(double)); Rio_readn (connfd, (void *)&new_log_hyperparams[2], sizeof(double)); Eigen::VectorXd new_eigen(3); new_eigen[0] = new_log_hyperparams[0]; new_eigen[1] = new_log_hyperparams[1]; new_eigen[2] = new_log_hyperparams[2]; printf("------------------\n"); printf("slave set the value of HP as\n"); for(int i = 0; i < 3;i++){ printf("%lf\n", new_eigen[i]); } printf("\n"); printf("------------------\n"); set_loghyper_eigen(new_eigen); break; } case DONE: { printf("Node %s got DONE message. Return!\n", hostname); return NULL; } } } }
int main(int argc, char *argv[]) { int listenfd, connfd; char hostname[MAXLINE], port[MAXLINE]; socklen_t clientlen; struct sockaddr_storage clientaddr; char *common_port = "15618"; listenfd = Open_listenfd (common_port); sleep(5); total_workers = atoi(argv[3]); numchunks = atoi(argv[4]); numtrain = atoi(argv[5]); dimensions = atoi(argv[6]); prefix_input_file_name = std::string(argv[7]); prefix_label_file_name = std::string(argv[8]); printf("Hostname %s is listening on port %s with listenfd = %d\n", argv[1], common_port, listenfd); printf("Node is %s and Master is %s. Number of workers is %d\n", argv[1], argv[2], total_workers); printf("Number of shards (chunks) = %d\n", numchunks); printf("Number of traning points for each expert = %d, with D = %d\n", numtrain, dimensions); printf("Input file prefix: %s, Label file prefix = %s\n", prefix_input_file_name.c_str(), prefix_label_file_name.c_str()); if (strcmp(argv[1], argv[2]) == 0) { for (int i = 0; i < total_workers - 1; i++) { clientlen = sizeof (clientaddr); // accept connections connfd = Accept (listenfd, (SA *) & clientaddr, &clientlen); Getnameinfo ((SA *) & clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, 0); printf ("Accepted connection from (%s, %s). Connfd is %d\n", hostname, port, connfd); worker_conn_fds.push_back(connfd); int new_worker_id = i + 1; Rio_writen (connfd, (void *)&new_worker_id, sizeof(int)); } } else { connfd = Open_clientfd (argv[2], common_port); printf("Host %s connected to master, connfd is %d\n", argv[1], connfd); Rio_readn (connfd, (void *)&worker_id, sizeof(int)); printf("Host %s got worker id as %d\n", argv[1], worker_id); } if (strcmp(argv[1], argv[2]) == 0) { printf("Master calling cg_solve()\n"); setup(numtrain, dimensions); BCM_log_hyperparams = new double[3]; Eigen::VectorXd initval(3); for(int i = 0 ; i < 3; i++){ initval[i] = 2.0; } set_loghyper_eigen_multinode(initval); double startime = CycleTimer::currentSeconds(); cg_solve(argv[1]); double endtime = CycleTimer::currentSeconds(); printf("TOTAL training time = %lf\n", endtime - startime); destruct_cublas_cusoler(); // testing_phase(numtrain,numtrain); } else { printf("Worker skipping cg_solve(), instead calling accept_commands\n"); setup(numtrain, dimensions); accept_commands(argv[1], connfd); } return 0; }