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; }
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 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, 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 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; }
void server() { int ret, context, num_request = 0, num_accept = 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); fflush(stdout); while (num_request < 3 || num_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_CONNECT_REQUEST: fprintf(stderr, "connect request\n"); num_request++; /* accept the first, reject the second, let the third timeout */ if (num_request == 1) { context = 123; cci_accept(event, (int *) &context); } else if (num_request == 2 && 0) { /* XXX: cci_reject segfaults */ cci_reject(event); } break; case CCI_EVENT_ACCEPT: fprintf(stderr, "completed accept\n"); connection = event->accept.connection; context = *(int *) event->accept.context; printf("CCI_EVENT_ACCEPT: %d\n", context); num_accept++; break; default: fprintf(stderr, "ignoring event type %d\n", event->type); } cci_return_event(event); } printf("Pausing while client times out...\n"); sleep(5); printf("test passed\n"); fflush(stdout); server_cleanup: 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); } free(uri); }