void *file_send(void *param){ char buf[MAX_BUFFER_SIZE]; long file_size=0, cur_size=0, read_size=0; int ret; FILE* file; t_data *argv=(t_data*)param; memset(buf, 0x00, MAX_BUFFER_SIZE); fprintf(stderr, "file send start!\n"); file = fopen("test.bmp", "rb"); if(!file) fprintf(stderr, "file is not opened\n"); fseek(file, 0, SEEK_END); file_size = ftell(file); /*file size check*/ fseek(file, 0, SEEK_SET); while(cur_size != file_size) { read_size = fread(buf, 1, argv->connection->max_send_size, file); cur_size = cur_size+read_size;//현재까지 읽은 크기 fprintf(stderr, "%ld%%\n",(cur_size*100)/file_size ); ret = cci_send(argv->connection, buf, read_size, SEND_CONTEXT, argv->flag); //읽은만큼 보내줌 if (ret) fprintf(stderr, "file send failed\n"); } ret = cci_send(argv->connection, "file send completed\0", 20, SEND_CONTEXT, argv->flag); //파일 종료 신호 보냄 if (ret) fprintf(stderr, "send failed\n"); fprintf(stderr,"file size is %ldbytes. file send completed\n",file_size); fclose(file); }
static inline void poll_events (void) { int ret; cci_event_t *event; ret = cci_get_event (endpoint, &event); if (ret == CCI_SUCCESS) { assert (event); switch (event->type) { case CCI_EVENT_CONNECT_REQUEST: opts = *((options_t *) event->request.data_ptr); ret = cci_reject (event); check_return (ctl_ep, "reject", ret, 1); break; case CCI_EVENT_CONNECT: if (!is_server) { connection = event->accept.connection; if (event->connect.status == CCI_ECONNREFUSED) { printf ("Connection refused\n"); ret = cci_send (ctl_conn, "bye", 5, (void*)0xdeadbeef, ctl_opts.flags); check_return(ctl_ep, "cci_send", ret, 0); done = 1; } } break; default: fprintf (stderr, "ignoring event type %d\n", event->type); } } return; }
void *send_msg(void* param){//채팅 보내는 스레드, 받는거는 그냥 main 스레드가 하게 했음 char data[MAX_BUFFER_SIZE], output[MAX_BUFFER_SIZE]; char* temp_str = NULL; int ret, end_client = 0; uint32_t len; t_data *argv=(t_data*)param; char *id = argv->id; pthread_t file_thread; memset(data,0x00,MAX_BUFFER_SIZE); while(!end_client){ memcpy(output, id, strlen(argv->id)); //client # temp_str=fgets(data, MAX_BUFFER_SIZE, stdin); //문자열 입력 받기 strcat(output, ":"); //client #:~~~이거 붙여줌 strcat(output, data); len=strlen(output); if(strncasecmp(data,"bye\n",4)==0){ //종료를 원할 경우 fprintf(stderr,"exit program...\n"); end_client=1; ret = cci_send(argv->connection, output, len, END_CONTEXT, argv->flag); if (ret) fprintf(stderr, "bye error\n"); continue; } ret = cci_send(argv->connection, output, len ,SEND_CONTEXT, argv->flag); //문자열 서버로 보내줌 if (ret) fprintf(stderr, "send failed\n"); if(strncasecmp(data,"file\n",5)==0) //file 전송 원할 경우 pthread_create(&file_thread, NULL, file_send, param); memset(output,0,MAX_BUFFER_SIZE); } }
static int send_request(int cnt) { int ret = 0; assert(!is_server); /* Send the count in the request */ *msg = cnt; ret = cci_send(connection, request, opts.req_size, (void*)((uintptr_t)cnt), 0); check_return(endpoint, "cci_send", ret, 0); return ret; }
static void do_server(void) { int ret; while (!ready) { cci_event_t *event; if (blocking) { FD_ZERO(&rfds); FD_SET(fd, &rfds); ret = select(nfds, &rfds, NULL, NULL, NULL); if (!ret) return; } ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { switch (event->type) { case CCI_EVENT_CONNECT_REQUEST: if (accept) { opts = *((options_t *) event->request. data_ptr); ret = cci_accept(event, NULL); check_return(endpoint, "cci_accept", ret, 1); } else { ret = cci_reject(event); check_return(endpoint, "cci_reject", ret, 1); } break; case CCI_EVENT_ACCEPT: { int len; ready = 1; connection = event->accept.connection; if (opts.method == MSGS) len = connection->max_send_size; else len = opts.max_rma_size; ret = posix_memalign((void **)&buffer, 4096, len); check_return(endpoint, "memalign buffer", ret, 1); memset(buffer, 'a', len); if (opts.method != MSGS) { ret = cci_rma_register(endpoint, buffer, opts. max_rma_size, opts.method == RMA_WRITE ? CCI_FLAG_WRITE : CCI_FLAG_READ, &server_rma_handle); check_return(endpoint, "cci_rma_register", ret, 1); memcpy(&opts.rma_handle, server_rma_handle, sizeof(*server_rma_handle)); } ret = cci_send(connection, &opts, sizeof(opts), NULL, 0); check_return(endpoint, "cci_send", ret, 1); break; } default: fprintf(stderr, "%s: ignoring unexpected event %d\n", __func__, event->type); break; } ret = cci_return_event(event); if (ret) fprintf(stderr, "cci_return_event() failed with %s\n", cci_strerror(endpoint, ret)); } } while (!done) poll_events(); if (opts.method != MSGS) { ret = cci_rma_deregister(endpoint, server_rma_handle); check_return(endpoint, "cci_rma_deregister", ret, 1); } printf("server done\n"); sleep(1); return; }
static void do_client(void) { int ret; uint32_t min = 0, max; struct timeval start, end; char *func; char *header = "Done"; /* initiate connect */ ret = cci_connect(endpoint, server_uri, &opts, sizeof(opts), attr, NULL, 0, NULL); check_return(endpoint, "cci_connect", ret, 1); /* poll for connect completion */ while (!connect_done) poll_events(); if (!connection) { fprintf(stderr, "no connection\n"); return; } while (!ready) poll_events(); if (opts.method == MSGS) { func = "cci_send"; max = connection->max_send_size; } else { func = "cci_rma"; max = opts.max_rma_size; } ret = posix_memalign((void **)&buffer, 4096, max); check_return(endpoint, "memalign buffer", ret, 1); memset(buffer, 'b', max); if (opts.method != MSGS) { int flags = 0; /* for the client, we want the opposite of the opts.method. * when testing RMA WRITE, we only need READ access. * when testing RMA READ, we need WRITE access. */ if (opts.method == RMA_WRITE) flags = CCI_FLAG_READ; else if (opts.method == RMA_READ) flags = CCI_FLAG_WRITE; ret = cci_rma_register(endpoint, buffer, max, flags, &local_rma_handle); check_return(endpoint, "cci_rma_register", ret, 1); fprintf(stderr, "local_rma_handle is %p\n", (void*)local_rma_handle); min = 1; if (opts.method == RMA_WRITE) opts.flags |= CCI_FLAG_WRITE; else opts.flags |= CCI_FLAG_READ; } if (remote_completion) { rmt_comp_msg = header; rmt_comp_len = 4; } if (opts.method == MSGS) printf("Bytes\t\tLatency (one-way)\tThroughput\n"); else printf("Bytes\t\tLatency (round-trip)\tThroughput\n"); /* begin communication with server */ for (current_size = min; current_size <= max;) { double lat = 0.0; double bw = 0.0; if (opts.method == MSGS) ret = cci_send(connection, buffer, current_size, NULL, opts.flags); else ret = cci_rma(connection, rmt_comp_msg, rmt_comp_len, local_rma_handle, 0, &opts.rma_handle, 0, current_size, (void *)1, opts.flags); check_return(endpoint, func, ret, 1); while (count < warmup) poll_events(); gettimeofday(&start, NULL); while (count < warmup + iters) poll_events(); gettimeofday(&end, NULL); if (opts.method == MSGS) lat = usecs(start, end) / (double)iters / 2.0; else lat = usecs(start, end) / (double)iters; bw = (double)current_size / lat; printf("%8d\t%8.2f us\t\t%8.2f MB/s\n", current_size, lat, bw); count = 0; if (current_size == 0) current_size++; else current_size *= 2; if (current_size >= 64 * 1024) { if (iters >= 32) iters /= 2; if (warmup >= 4) warmup /= 2; } } ret = cci_send(connection, "bye", 3, (void *)0xdeadbeef, opts.flags); check_return(endpoint, "cci_send", ret, 0); while (!done) poll_events(); if (opts.method != MSGS) { ret = cci_rma_deregister(endpoint, local_rma_handle); check_return(endpoint, "cci_rma_deregister", ret, 1); } printf("client done\n"); sleep(1); return; }
static void poll_events(void) { int ret; cci_event_t *event; if (blocking) { FD_ZERO(&rfds); FD_SET(fd, &rfds); ret = select(nfds, &rfds, NULL, NULL, NULL); if (!ret) return; } ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { assert(event); switch (event->type) { case CCI_EVENT_SEND: assert(event->send.status == CCI_SUCCESS); if (opts.method != MSGS) { if (!is_server && event->send.context == (void *)1) { count++; if (count < warmup + iters) { ret = cci_rma(connection, rmt_comp_msg, rmt_comp_len, local_rma_handle, 0, &opts.rma_handle, 0, current_size, (void *)1, opts.flags); check_return(endpoint, "cci_rma", ret, 1); } } } if (!is_server && event->send.context == (void *)0xdeadbeef) done = 1; break; case CCI_EVENT_RECV: { if (!is_server && opts.method != MSGS && event->recv.ptr == (void *)1) { count++; if (count < warmup + iters) { ret = cci_rma(connection, rmt_comp_msg, rmt_comp_len, local_rma_handle, 0, &opts.rma_handle, 0, current_size, (void *)1, opts.flags); check_return(endpoint, "cci_rma", ret, 1); } } if (!ready) { ready = 1; if (opts.method != MSGS && !is_server) { /* get server_rma_handle */ opts = *((options_t *) event->recv. ptr); } } else if (is_server && event->recv.len == 3) { done = 1; break; } else if (opts.method == MSGS) { if (is_server) { count++; if (event->recv.len > current_size) { current_size = event->recv.len; count = 1; } } else { if (event->recv.len == current_size) count++; } if (is_server || count < warmup + iters) { ret = cci_send(connection, buffer, current_size, NULL, opts.flags); if (ret) fprintf(stderr, "%s: %s: send returned %s\n", __func__, is_server ? "server" : "client", cci_strerror (endpoint, ret)); check_return(endpoint, "cci_send", ret, 1); } } break; } case CCI_EVENT_CONNECT: if (!is_server) { connect_done = 1; connection = event->connect.connection; } break; default: fprintf(stderr, "ignoring event type %d\n", event->type); } cci_return_event(event); } return; }
static void do_server(void) { int ret = 0; hdr_t *h = NULL; while (!ready) { cci_event_t *event; if (blocking) { FD_ZERO(&rfds); FD_SET(fd, &rfds); ret = select(nfds, &rfds, NULL, NULL, NULL); if (!ret) return; } ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { switch (event->type) { case CCI_EVENT_CONNECT_REQUEST: { int which = 0; h = (void*)event->request.data_ptr; if (h->generic.type == MSG_CONN_REQ) { opts = h->request.opts; which = TEST; } else { which = CONTROL; } ret = cci_accept(event, (void*)((uintptr_t)which)); check_return(endpoint, "cci_accept", ret, 1); break; } case CCI_EVENT_ACCEPT: { if ((uintptr_t)event->accept.context == (uintptr_t)CONTROL) { control = event->accept.connection; } else { int len; test = event->accept.connection; len = opts.reg_len; ret = posix_memalign((void **)&buffer, 4096, len); check_return(endpoint, "memalign buffer", ret, 1); init_buffer(0); print_buffer(buffer, opts.reg_len); ret = cci_rma_register(endpoint, buffer, opts.reg_len, opts.method == RMA_WRITE ? CCI_FLAG_WRITE : CCI_FLAG_READ, &local_rma_handle); check_return(endpoint, "cci_rma_register", ret, 1); } if (test && control) { hdr_t msg; ready = 1; msg.reply.type = MSG_CONN_REPLY; msg.reply.handle = *local_rma_handle; ret = cci_send(test, &msg, sizeof(msg.reply), NULL, 0); check_return(endpoint, "cci_send", ret, 1); } break; } default: fprintf(stderr, "%s: ignoring unexpected event %s\n", __func__, cci_event_type_str(event->type)); break; } ret = cci_return_event(event); if (ret) fprintf(stderr, "cci_return_event() failed with %s\n", cci_strerror(endpoint, ret)); } } while (!done) poll_events(); ret = cci_rma_deregister(endpoint, local_rma_handle); check_return(endpoint, "cci_rma_deregister", ret, 1); printf("server done\n"); sleep(1); return; }
static void do_client(void) { int ret; uint32_t min = 1; /* initiate connect */ msg.request.type = MSG_CONTROL; ret = cci_connect(endpoint, server_uri, &msg, sizeof(msg.generic), attr, (void*)(uintptr_t)CONTROL, 0, NULL); check_return(endpoint, "cci_connect", ret, 1); msg.request.type = MSG_CONN_REQ; msg.request.opts = opts; ret = cci_connect(endpoint, server_uri, &msg, sizeof(msg.request), attr, (void*)(uintptr_t)TEST, 0, NULL); check_return(endpoint, "cci_connect", ret, 1); /* poll for connect completion */ while (!connect_done) poll_events(); if (!test) { fprintf(stderr, "no connection\n"); return; } while (!ready) poll_events(); ret = posix_memalign((void **)&buffer, 4096, opts.reg_len); check_return(endpoint, "memalign buffer", ret, 1); memset(buffer, 0xaa, opts.reg_len); init_buffer(1); print_buffer(buffer, (int) opts.reg_len); /* for the client, we do not need remote access flags */ ret = cci_rma_register(endpoint, buffer, opts.reg_len, 0, &local_rma_handle); check_return(endpoint, "cci_rma_register", ret, 1); if (opts.method == RMA_WRITE) opts.flags = CCI_FLAG_WRITE; else opts.flags = CCI_FLAG_READ; /* begin communication with server */ for (current_size = min; current_size <= length;) { void *ptr = (void*)((uintptr_t)buffer + local_offset); msg.check.type = MSG_RMA_CHK; msg.check.offset = remote_offset; msg.check.len = current_size; /* Compute the CRC only on a valid buffer */ if (current_size + local_offset <= opts.reg_len) msg.check.crc = crc32(0, ptr, current_size); else msg.check.crc = 0; msg_len = sizeof(msg.check); print_buffer(ptr, current_size); fprintf(stderr, "Testing length %9u ... ", current_size); ret = cci_rma(test, &msg, msg_len, local_rma_handle, local_offset, &remote_rma_handle, remote_offset, current_size, NULL, opts.flags); check_return(endpoint, "cci_rma", ret, 1); while (count < iters) poll_events(); if (test) fprintf(stderr, "success.\n"); else goto out; count = 0; current_size *= 2; if (current_size >= 64 * 1024) { if (iters >= 32) iters /= 2; } } out: ret = cci_send(control, "bye", 3, (void *)0xdeadbeef, 0); check_return(endpoint, "cci_send", ret, 0); while (!done) poll_events(); ret = cci_rma_deregister(endpoint, local_rma_handle); check_return(endpoint, "cci_rma_deregister", ret, 1); printf("client done\n"); sleep(1); return; }
static void poll_events(void) { int ret; cci_event_t *event; if (blocking) { FD_ZERO(&rfds); FD_SET(fd, &rfds); ret = select(nfds, &rfds, NULL, NULL, NULL); if (!ret) return; } ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { assert(event); switch (event->type) { case CCI_EVENT_SEND: if (event->send.status != CCI_SUCCESS) { fprintf(stderr, "RMA failed with %s.\n", cci_strerror(endpoint, event->send.status)); cci_disconnect(test); test = NULL; done = 1; } if (is_server) break; /* Client */ if (event->send.context == (void *)0xdeadbeef) { done = 1; break; } break; case CCI_EVENT_RECV: if (is_client) { hdr_t *h = (void*)event->recv.ptr; if (!ready) { ready = 1; memcpy((void*)&remote_rma_handle, &h->reply.handle, sizeof(remote_rma_handle)); } else { /* RMA status msg */ if (opts.method == RMA_WRITE) { if (h->status.crc != msg.check.crc) { fprintf(stderr, "Server reported " "CRC failed.\n" "Local CRC 0x%x != " "remote CRC 0x%x.\n" "count=%d current_size=%u\n", msg.check.crc, h->status.crc, count, current_size); } } else { uint32_t crc = 0; void *ptr = (void*)((uintptr_t)buffer + local_offset); /* Compute the CRC only on a valid buffer */ if (current_size + local_offset <= opts.reg_len) crc = crc32(0, ptr, current_size); else crc = 0; if (crc != h->status.crc) { fprintf(stderr, "Server reported " "CRC failed.\n" "Local CRC 0x%x != " "remote CRC 0x%x.\n" "count=%d current_size=%u\n", crc, h->status.crc, count, current_size); } } /* RMA completed */ count++; if (count < iters) { ret = cci_rma(test, &msg, msg_len, local_rma_handle, local_offset, &remote_rma_handle, remote_offset, current_size, NULL, opts.flags); check_return(endpoint, "cci_rma", ret, 1); } } } else { hdr_t *h = (void*)event->recv.ptr; /* is_server */ if (event->recv.len == 3) { done = 1; } else { uint32_t crc = 0; void *ptr = (void*)((uintptr_t)buffer + h->check.offset); /* RMA check request */ if ((h->check.len + h->check.offset) <= opts.reg_len) crc = crc32(0, ptr, h->check.len); else crc = 0; msg.status.type = MSG_RMA_STATUS; msg.status.crc = crc; if (opts.method == RMA_WRITE) { fprintf(stderr, "server: client crc=0x%x " "server crc=0x%x\n", h->check.crc, crc); } print_buffer(ptr, h->check.len); ret = cci_send(test, &msg, sizeof(msg.status), NULL, CCI_FLAG_SILENT); check_return(endpoint, "cci_send", ret, 1); } } break; case CCI_EVENT_CONNECT: if (event->connect.status != CCI_SUCCESS) { fprintf(stderr, "Connection rejected.\n"); exit(0); } if ((uintptr_t)event->connect.context == (uintptr_t)CONTROL) { control = event->connect.connection; } else { test = event->connect.connection; } if (control && test) connect_done = 1; break; case CCI_EVENT_CONNECT_REQUEST: fprintf(stderr, "Peer is reconnecting? Rejecting.\n"); cci_reject(event); break; default: fprintf(stderr, "ignoring event type %s\n", cci_event_type_str(event->type)); } cci_return_event(event); } return; }
int main(int argc, char *argv[]) { int ret; uint32_t caps = 0; char *uri = NULL; cci_endpoint_t *endpoint = NULL; cci_os_handle_t ep_fd; cci_connection_t *connection = NULL; /* init */ ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, &ep_fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); while (1) { cci_event_t *event; ret = cci_get_event(endpoint, &event); if (ret != CCI_SUCCESS) { if (ret != CCI_EAGAIN) { fprintf(stderr, "cci_get_event() returned %s", cci_strerror(endpoint, ret)); } continue; } /* fprintf(stderr, "ret: %d, event: %d (RECV:%d, SEND:%d, CONNECT_REQ:%d, ACCEPT:%d)\n", */ /* ret, event->type, CCI_EVENT_RECV, CCI_EVENT_SEND, CCI_EVENT_CONNECT_REQUEST, */ /* CCI_EVENT_ACCEPT); */ fprintf(stderr, "Event: %s, ret:%d len: %lu\n", cci_event_type_str(event->type), ret, event->recv.len); switch (event->type) { case CCI_EVENT_RECV: { // fprintf(stderr, "=====: %p %p %d\n", buffer, event->recv.ptr, event->recv.len); // memcpy(buffer, // event->recv.ptr, event->recv.len); // fprintf(stderr, "=====\n"); // buffer[event->recv.len] = 0; // printf("recv'd \n"); /* echo the message to the client */ ret = cci_send(connection, event->recv.ptr, event->recv.len, NULL, 0); if (ret != CCI_SUCCESS) fprintf(stderr, "send returned %s\n", cci_strerror(endpoint, ret)); break; } case CCI_EVENT_SEND: // printf("completed send\n"); break; case CCI_EVENT_CONNECT_REQUEST: { int accept = 1; if (accept) { ret = cci_accept(event, NULL); if (ret != CCI_SUCCESS) { fprintf(stderr, "cci_accept() returned %s", cci_strerror(endpoint, ret)); } } else { cci_reject(event); } } break; case CCI_EVENT_ACCEPT: connection = event->accept.connection; if (!buffer) { buffer = calloc(1, connection->max_send_size + 1); fprintf(stderr, "allocated buffer: %p , size %d", buffer, connection->max_send_size + 1); /* check for buffer ... */ } else { fprintf(stderr, "Not allocated\n"); } break; default: fprintf(stderr, "unexpected event %d", event->type); break; } cci_return_event(event); } /* clean up */ cci_destroy_endpoint(endpoint); /* add cci_finalize() here */ return 0; }
int main(int argc, char *argv[]) { int ret, number_of_connections=0; int client_end[MAX_CONNECTION_SIZE]={0,}; uint32_t caps = 0; char *uri = NULL; long file_size[MAX_CONNECTION_SIZE]={0,}; cci_endpoint_t *endpoint = NULL; cci_os_handle_t *ep_fd = NULL; cci_connection_t *connection[MAX_CONNECTION_SIZE] = {NULL,}; ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, ep_fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); while (1) { int accept = 1; cci_event_t *event; ret = cci_get_event(endpoint, &event); if (ret != 0) { if (ret != CCI_EAGAIN) fprintf(stderr, "cci_get_event() returned %s\n", cci_strerror(endpoint, ret)); continue; } switch (event->type) { case CCI_EVENT_RECV:{ char buf[MAX_BUFFER_SIZE]; char *number, *data; char exit_msg[32]; long read_size=0; int len = event->recv.len; int i=0, j=0; int id; memset(buf, 0, MAX_BUFFER_SIZE); memcpy(buf, event->recv.ptr, len); if(strncasecmp((char*)event->recv.ptr,"client", 6)==0){ /* 문자인 경우 */ fprintf(stderr, "%s", buf); strtok(buf," :"); /* 필요한 정보 파싱 (누구한테, 첫번째 문자)*/ number=strtok(NULL," :"); data=strtok(NULL," :"); id=atoi(number); //누구한테서 왔는지 memcpy(buf, event->recv.ptr, len); if(event->recv.connection==connection[id-1]){ if(strncasecmp(data,"bye\n", 4)==0){ //종료를 원할 경우 fprintf(stderr, "Client%d want to termainate this program.\n", id); client_end[id-1]=1; }else if(strncasecmp(data,"file\n", 5)==0) //file 전송을 원할 경우 fprintf(stderr, "Client %d send a file...\n", id); for(j=0; j<number_of_connections; j++){ if(j !=id-1 && !client_end[j]) //받는놈은 종료되지 않은놈이고 보내는놈이 아님 ret = cci_send(connection[j], buf, len, SEND_CONTEXT, 0); //보냄 if(client_end[id-1]){ //보내는놈이 종료를 원한 경우 sprintf(exit_msg,"client%d exits..\n", id); ret = cci_send(connection[j], exit_msg, strlen(exit_msg), SEND_CONTEXT, 0); //종료메세지 보냄 } } //end for j } }else{ /* 파일인 경우 */ for(i=0; i<number_of_connections; i++){ //어떤 connection에서 왔는지 check if(event->recv.connection == connection[i]){ if(strncasecmp((char*)event->recv.ptr,"file send completed\0", 20)==0){ //file 전송종료신호면 그만 fprintf(stderr,"%s. file size is %ldbytes.\n",(char*)event->recv.ptr, file_size[i]); memcpy(buf,event->recv.ptr, len); //종료신호 고대로 클라에게 read_size=len; file_size[i]=0; }else{ read_size=event->recv.len; //읽은 크기 file_size[i]+=read_size; //file size check fprintf(stderr, "%ld\n", file_size[i]); memcpy(buf, event->recv.ptr, read_size); //읽은거 buf에 써주기 }//end file trasport for(j=0; j<number_of_connections; j++){ /* broadcast */ if(j !=i && !client_end[j]){ ret = cci_send(connection[j], buf, read_size, SEND_CONTEXT, 0); if(ret) fprintf(stderr, "file send failed!\n"); } }//end for j break; }//end if }//end for i } break; }//end recv case case CCI_EVENT_SEND: assert(event->send.context == SEND_CONTEXT); assert(event->send.connection->context == ACCEPT_CONTEXT); fprintf(stderr, "completed send\n"); break; case CCI_EVENT_CONNECT_REQUEST: if (accept) { cci_accept(event, ACCEPT_CONTEXT); }else { cci_reject(event); } break; case CCI_EVENT_ACCEPT:{ char number[MAX_CONNECTION_SIZE]; assert(event->accept.connection != NULL); assert(event->accept.connection->context == ACCEPT_CONTEXT); connection[number_of_connections] = event->accept.connection; fprintf(stderr, "completed accept\n"); sprintf(number,"%d",number_of_connections+1); ret = cci_send(connection[number_of_connections], number, strlen(number), SEND_CONTEXT, 0); //몇번째 클라이언트 인지 알려줌 number_of_connections++; break; } default: printf("event type %d\n", event->type); break; } //end switch cci_return_event(event); } /* clean up */ cci_destroy_endpoint(endpoint); cci_finalize(); free(uri); return 0; }
int main(int argc, char *argv[]) { int ret, done = 0; uint32_t caps = 0; char *uri = NULL; cci_endpoint_t *endpoint = NULL; cci_os_handle_t *ep_fd = NULL; cci_connection_t *connection = NULL; ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, ep_fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); while (!done) { int accept = 1; cci_event_t *event; ret = cci_get_event(endpoint, &event); if (ret != 0) { if (ret != CCI_EAGAIN) fprintf(stderr, "cci_get_event() returned %s\n", cci_strerror(endpoint, ret)); continue; } switch (event->type) { case CCI_EVENT_RECV:{ char buf[8192]; char *data = "data:"; int offset = 0; int len = event->recv.len; assert(event->recv.connection == connection); assert(event->recv.connection->context == ACCEPT_CONTEXT); if (len == 3) { done = 1; continue; } memset(buf, 0, 8192); offset = strlen(data); memcpy(buf, data, offset); memcpy(buf + offset, event->recv.ptr, len); offset += len; fprintf(stderr, "recv'd \"%s\"\n", buf); ret = cci_send(connection, buf, offset, SEND_CONTEXT, 0); if (ret) fprintf(stderr, "send returned %s\n", cci_strerror(endpoint, ret)); break; } case CCI_EVENT_SEND: fprintf(stderr, "completed send\n"); assert(event->send.context == SEND_CONTEXT); assert(event->send.connection == connection); assert(event->send.connection->context == ACCEPT_CONTEXT); break; case CCI_EVENT_CONNECT_REQUEST: /* inspect conn_req_t and decide to accept or reject */ if (accept) { /* associate this connect request with this endpoint */ cci_accept(event, ACCEPT_CONTEXT); } else { cci_reject(event); } break; case CCI_EVENT_ACCEPT: fprintf(stderr, "completed accept\n"); assert(event->accept.connection != NULL); assert(event->accept.connection->context == ACCEPT_CONTEXT); connection = event->accept.connection; break; default: printf("event type %d\n", event->type); break; } cci_return_event(event); } /* clean up */ cci_destroy_endpoint(endpoint); cci_finalize(); free(uri); return 0; }
int main(int argc, char *argv[]) { int done = 0, ret, i = 0, c; uint32_t caps = 0; char *server_uri = NULL; /* ip://1.2.3.4 */ char *uri = NULL; cci_os_handle_t fd; cci_endpoint_t *endpoint = NULL; cci_connection_t *connection = NULL; uint32_t timeout_us = 30 * 1000000; /* microseconds */ proc_name = argv[0]; while ((c = getopt(argc, argv, "h:")) != -1) { switch (c) { case 'h': server_uri = strdup(optarg); break; default: usage(); } } /* init */ ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() returned %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, &fd); if (ret) { fprintf(stderr, "cci_create_endpoint() returned %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } printf("Opened %s\n", uri); /* set endpoint tx timeout */ cci_set_opt(endpoint, CCI_OPT_ENDPT_SEND_TIMEOUT, &timeout_us); if (ret) { fprintf(stderr, "cci_set_opt() returned %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } /* initiate connect */ ret = cci_connect(endpoint, server_uri, "Hello World!", 12, CCI_CONN_ATTR_UU, NULL, 0, NULL); if (ret) { fprintf(stderr, "cci_connect() returned %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } /* poll for connect completion */ while (!done) poll_events(endpoint, &connection, &done); if (!connection) { fprintf(stderr, "no connection\n"); exit(EXIT_FAILURE); } /* begin communication with server */ char *data; int max = connection->max_send_size; data = (char*)malloc(max); memset(data, 1, sizeof(data)); int len = 128; int repeat = 1000; while (len <= max) { s = cci_get_time(); for (i = 0; i < repeat; i++) { /* ret = cci_send(connection, data, (uint32_t) strlen(data), */ /* (void *)(uintptr_t) i, 0); */ ret = cci_send(connection, data, len, (void *)(uintptr_t) i, 0); if (ret > 0) { fprintf(stderr, "send %d returned %s\n", i, cci_strerror(endpoint, ret)); exit(1); } done = 0; while (!done) poll_events(endpoint, &connection, &done); } e = cci_get_time(); fprintf(stderr, "%d %f %f\n", len, (e - s) / repeat, (len / (e - s)) * repeat); len = len << 1; sleep(1); } /* clean up */ ret = cci_disconnect(connection); if (ret) { fprintf(stderr, "cci_disconnect() returned %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } ret = cci_destroy_endpoint(endpoint); if (ret) { fprintf(stderr, "cci_destroy_endpoint() returned %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* add cci_finalize() here */ return 0; }
static inline void poll_ctl_events (void) { cci_event_t *event; int ret; ret = cci_get_event (ctl_ep, &event); if (ret == CCI_SUCCESS) { switch (event->type) { case CCI_EVENT_CONNECT_REQUEST: ctl_opts = *((options_t *) event->request.data_ptr); ret = cci_accept (event, NULL); check_return (ctl_ep, "cci_accept", ret, 1); break; case CCI_EVENT_ACCEPT: ctl_conn = event->accept.connection; break; case CCI_EVENT_RECV: if (is_server && strncmp (event->recv.ptr, "start", 5) == 0) { printf ("Connection accepted, creating endpoint for testing...\n"); ret = cci_create_endpoint(NULL, 0, &endpoint, NULL); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &test_uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_send (ctl_conn, test_uri, strlen (test_uri), (void*)0xdeadfeeb, ctl_opts.flags); check_return(ctl_ep, "cci_send", ret, 0); printf("Opened %s\n", test_uri); } if (is_server && strncmp (event->recv.ptr, "bye", 3) == 0) { done = 1; } if (!is_server) { test_uri = strdup (event->recv.ptr); printf ("Opening a connection to %s\n", test_uri); ret = cci_create_endpoint(NULL, 0, &endpoint, NULL); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_get_opt(endpoint, CCI_OPT_ENDPT_URI, &uri); if (ret) { fprintf(stderr, "cci_get_opt() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } ret = cci_connect (endpoint, test_uri, &opts, sizeof (opts), attr, NULL, 0, NULL); check_return (endpoint, "cci_connect", ret, 1); } break; case CCI_EVENT_CONNECT: if (!is_server) { ctl_conn = event->connect.connection; printf ("Control connection established, simulating a conn_reject now...\n"); /* Send the start message */ ret = cci_send (ctl_conn, "start", 5, (void*)0xdeadbeed, ctl_opts.flags); check_return(ctl_ep, "cci_send", ret, 0); } break; case CCI_EVENT_SEND: break; default: fprintf (stderr, "Ignoring event type %d\n", event->type); } cci_return_event (event); } }
static void do_client(void) { int ret, i = 0; struct timeval start, end; double lat = 0.0; double bw = 0.0; ret = posix_memalign((void **)&request, 4096, opts.req_size); check_return(endpoint, "memalign buffer", ret, 1); msg = (int*) request; ret = posix_memalign((void **)&buffer, 4096, opts.transfer_size); check_return(endpoint, "memalign buffer", ret, 1); memset(buffer, 'b', opts.transfer_size); ret = cci_rma_register(endpoint, buffer, opts.transfer_size, opts.rma_flags, &local); check_return(endpoint, "cci_rma_register", ret, 1); memcpy(&opts.rma_handle, local, sizeof(*local)); connect_to_server(); if (connection->max_send_size < opts.req_size) opts.req_size = connection->max_send_size; if (!suppress) printf("Bytes\t\tLatency (per rpc)\tThroughput (per rpc)\n"); /* begin communication with server */ ret = send_request(count); check_return(endpoint, "send first request", ret, 1); if (!ret) { count++; inflight++; } progress_client(opts.warmup); count = 0; completed = 0; gettimeofday(&start, NULL); for (i = 0; i < concurrent; i++) { ret = send_request(count); if (!ret) { count++; inflight++; } check_return(endpoint, "send first request", ret, 0); } progress_client(opts.iters); gettimeofday(&end, NULL); lat = usecs(start, end) / (double)opts.iters; bw = (double)opts.transfer_size / lat; printf("%8d\t%8.2f us\t\t%8.2f MB/s\n", opts.transfer_size, lat, bw); ret = cci_send(connection, "bye", 3, (void *)0xdeadbeef, 0); check_return(endpoint, "cci_send", ret, 1); progress_client(0); ret = cci_rma_deregister(endpoint, local); check_return(endpoint, "cci_rma_deregister", ret, 1); if (!suppress) printf("client done\n"); return; }
int main(int argc, char *argv[]) { int done = 0, ret, i = 0, c; uint32_t caps = 0; char *server_uri = NULL; cci_os_handle_t *fd = NULL; cci_endpoint_t *endpoint = NULL; cci_connection_t *connection = NULL; uint32_t timeout = 30 * 1000000; while ((c = getopt(argc, argv, "h:c:b")) != -1) { switch (c) { case 'h': server_uri = strdup(optarg); break; case 'c': if (strncasecmp ("ru", optarg, 2) == 0) attr = CCI_CONN_ATTR_RU; else if (strncasecmp ("ro", optarg, 2) == 0) attr = CCI_CONN_ATTR_RO; else if (strncasecmp ("uu", optarg, 2) == 0) attr = CCI_CONN_ATTR_UU; break; case 'b': flags |= CCI_FLAG_BLOCKING; break; default: fprintf(stderr, "usage: %s -h <server_uri> [-c <type>]\n", argv[0]); fprintf(stderr, "\t-c\tConnection type (UU, RU, or RO) " "set by client; RO by default\n"); exit(EXIT_FAILURE); } } if (!server_uri) { fprintf(stderr, "usage: %s -h <server_uri> [-c <type>]\n", argv[0]); fprintf(stderr, "\t-c\tConnection type (UU, RU, or RO) " "set by client; RO by default\n"); exit(EXIT_FAILURE); } ret = cci_init(CCI_ABI_VERSION, 0, &caps); if (ret) { fprintf(stderr, "cci_init() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* create an endpoint */ ret = cci_create_endpoint(NULL, 0, &endpoint, fd); if (ret) { fprintf(stderr, "cci_create_endpoint() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } /* set conn tx timeout */ cci_set_opt(endpoint, CCI_OPT_ENDPT_SEND_TIMEOUT, &timeout); if (ret) { fprintf(stderr, "cci_set_opt() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } /* initiate connect */ ret = cci_connect(endpoint, server_uri, "Hello World!", 12, attr, CONNECT_CONTEXT, 0, NULL); if (ret) { fprintf(stderr, "cci_connect() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } /* poll for connect completion */ while (!done) poll_events(endpoint, &connection, &done); if (!connection) exit(0); done = 0; /* begin communication with server */ for (i = 0; i < iters; i++) { char data[128]; memset(data, 0, sizeof(data)); sprintf(data, "%4d", i); sprintf(data + 4, "Hello World!"); ret = cci_send(connection, data, (uint32_t) strlen(data) + 4, (void *)(uintptr_t) i, flags); if (ret) fprintf(stderr, "send %d failed with %s\n", i, cci_strerror(endpoint, ret)); if (flags & CCI_FLAG_BLOCKING) fprintf(stderr, "send %d completed with %d\n", i, ret); } if (flags == CCI_FLAG_BLOCKING) send_done = iters; while (!done) poll_events(endpoint, &connection, &done); ret = cci_send(connection, "bye", 3, (void *)(uintptr_t) iters, flags); if (ret) fprintf(stderr, "sending \"bye\" failed with %s\n", cci_strerror(endpoint, ret)); if (flags & CCI_FLAG_BLOCKING) done = 2; while (done != 2) poll_events(endpoint, &connection, &done); /* clean up */ ret = cci_destroy_endpoint(endpoint); if (ret) { fprintf(stderr, "cci_destroy_endpoint() failed with %s\n", cci_strerror(endpoint, ret)); exit(EXIT_FAILURE); } ret = cci_finalize(); if (ret) { fprintf(stderr, "cci_finalize() failed with %s\n", cci_strerror(NULL, ret)); exit(EXIT_FAILURE); } return 0; }