static void connect_to_server(void) { int ret = 0; cci_conn_attribute_t attr = CCI_CONN_ATTR_RO; recv_uri(); again: ret = cci_connect(endpoint, server_uri, &opts, sizeof(opts), attr, NULL, 0, NULL); check_return(endpoint, "cci_connect", ret, 1); while (!connection) { cci_event_t *event = NULL; ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { assert(event->type == CCI_EVENT_CONNECT); connection = event->connect.connection; if (!connection) { cci_return_event(event); fprintf(stderr, "Connecting to %s failed\n", server_uri); attempt *= 2; sleep(attempt); goto again; } cci_return_event(event); } } return; }
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 progress_server(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_RECV: if (event->recv.len != 3) { msg = (void*)event->recv.ptr; transfer_data(*msg); } else { done = 1; } break; case CCI_EVENT_SEND: if (event->send.status != CCI_SUCCESS) { int cnt = (int)((uintptr_t)event->send.status); transfer_data(cnt); } break; default: fprintf(stderr, "ignoring event type %d\n", event->type); } cci_return_event(event); } return; }
static void poll_events(cci_endpoint_t * endpoint, cci_connection_t ** connection, int *done) { int ret; char buffer[8192]; cci_event_t *event; static int i = 0; ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS && event) { switch (event->type) { case CCI_EVENT_SEND: fprintf(stderr, "send %d completed with %d\n", (int)((uintptr_t) event->send.context), event->send.status); assert(event->send.context == (void *)(uintptr_t)i); i++; assert(event->send.connection == *connection); assert(event->send.connection->context == CONNECT_CONTEXT); if (*done == 0) send_done++; else if (*done == 1) *done = 2; break; case CCI_EVENT_RECV:{ int len = event->recv.len; assert(event->recv.connection == *connection); assert(event->recv.connection->context == CONNECT_CONTEXT); memcpy(buffer, event->recv.ptr, len); buffer[len] = '\0'; fprintf(stderr, "received \"%s\"\n", buffer); recv_done++; break; } case CCI_EVENT_CONNECT: *done = 1; assert(event->connect.connection != NULL); assert(event->connect.connection->context == CONNECT_CONTEXT); *connection = event->connect.connection; break; default: fprintf(stderr, "ignoring event type %d\n", event->type); } cci_return_event(event); if (*done == 0 && send_done == iters && recv_done == iters) *done = 1; } }
static void accept_connection(void) { send_uri(); while (!connection) { int ret = 0; cci_event_t *event = NULL; ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { switch (event->type) { case CCI_EVENT_CONNECT_REQUEST: opts = *((options_t *) event->request.data_ptr); cci_accept(event, NULL); break; case CCI_EVENT_ACCEPT: assert(event->accept.status == CCI_SUCCESS); connection = event->accept.connection; ret = posix_memalign((void **)&buffer, 4096, opts.transfer_size); check_return(endpoint, "memalign buffer", ret, 1); memset(buffer, 'a', opts.transfer_size); ret = posix_memalign((void **)&ack, 4096, opts.ack_size); check_return(endpoint, "memalign buffer", ret, 1); memset(buffer, 'b', opts.ack_size); ret = cci_rma_register(endpoint, buffer, opts.transfer_size, CCI_FLAG_WRITE|CCI_FLAG_READ, &local); check_return(endpoint, "cci_rma_register", ret, 1); remote = &opts.rma_handle; break; default: fprintf(stderr, "%s: got %s event\n", __func__, cci_event_type_str(event->type)); } cci_return_event(event); } } return; }
static void progress_client(int iters) { int ret; cci_event_t *event; while (completed < iters || (iters == 0 && !done)) { ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { assert(event); switch (event->type) { case CCI_EVENT_SEND: { int cnt = (int)((uintptr_t)event->send.context); if (event->send.status == CCI_SUCCESS) { inflight--; if (cnt == (int) 0xdeadbeef) done = 1; } else { send_request(cnt); } } break; case CCI_EVENT_RECV: completed++; while (count < iters && inflight < concurrent) { ret = send_request(count); if (!ret) { count++; inflight++; } else { break; } } break; default: fprintf(stderr, "ignoring event type %d\n", event->type); } cci_return_event(event); } } return; }
static void poll_events(cci_endpoint_t * endpoint, cci_connection_t ** connection, int *num_connect) { int ret; cci_event_t *event; char buf[128]; int context = 0; ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS && event) { switch (event->type) { case CCI_EVENT_CONNECT: *num_connect += 1; *connection = event->connect.connection; context = *(int *) event->connect.context; switch (event->connect.status) { case CCI_SUCCESS: sprintf(buf, "CCI_SUCCESS\n"); break; case CCI_ECONNREFUSED: sprintf(buf, "CCI_ECONNREFUSED\n"); break; case CCI_ETIMEDOUT: sprintf(buf, "CCI_ETIMEDOUT\n"); break; default: sprintf(buf, "ERROR (%d)\n", event->connect.status); } printf("CCI_EVENT_CONNECT: %d - %s", context, buf); break; default: fprintf(stderr, "ignoring event type %d\n", event->type); } cci_return_event(event); } }
static void poll_events(cci_endpoint_t * endpoint, cci_connection_t ** connection, int *done) { int ret; char buffer[8192]; cci_event_t *event; ret = cci_get_event(endpoint, &event); if (ret == CCI_SUCCESS) { switch (event->type) { case CCI_EVENT_SEND: /* printf("send %d completed with %s\n", */ /* (int)((uintptr_t) event->send.context), */ /* cci_strerror(endpoint, event->send.status)); */ break; case CCI_EVENT_RECV: memcpy(buffer, event->recv.ptr, event->recv.len); buffer[event->recv.len] = '\0'; // e = cci_get_time(); // fprintf(stderr, "received \"%s\": time: %f \n", buffer, e - s); *done = 1; break; case CCI_EVENT_CONNECT: *done = 1; if (event->connect.status == CCI_SUCCESS) *connection = event->connect.connection; else *connection = NULL; break; default: fprintf(stderr, "ignoring event type %d\n", event->type); } cci_return_event(event); } }
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); } }
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, 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; }
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; 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_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 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; }
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); }
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; }
static void poll_events(cci_endpoint_t * endpoint, cci_connection_t ** connection, int *done, char *id) { int ret; char buffer[MAX_BUFFER_SIZE]; cci_event_t *event; static FILE *fp; static int ft_start = 0; ret = cci_get_event(endpoint, &event); //event 받아옴 if (ret == CCI_SUCCESS && event) { // event가 제대로 받아 와 지면 switch (event->type) { case CCI_EVENT_SEND:{ if(event->send.context==END_CONTEXT)//bye일 경우 *done=1; else assert(event->send.context==SEND_CONTEXT); assert(event->send.connection == *connection); assert(event->send.connection->context == CONNECT_CONTEXT); break; } case CCI_EVENT_RECV:{ int len = event->recv.len; assert(event->recv.connection == *connection); assert(event->recv.connection->context == CONNECT_CONTEXT); memcpy(buffer, event->recv.ptr, len); if(strcasecmp(id,"")==0){ strcpy(id,"client "); memcpy(id+7, buffer, 16); fprintf(stderr, "Welcome to Chatting program! You are %s\n", id); *done = 1; break; } if(strncasecmp((char*)event->recv.ptr, id, 7)==0){//채팅일경우 buffer[len] = '\0'; fprintf(stderr, "%s", buffer); //상대방 대화내용 띄워줌 break; }else{ //file 전송 일 경우 if(!ft_start){//처음온 경우 fprintf(stderr, "file recv...\n"); fp = fopen("test12.bmp", "wb"); if(!fp) fprintf(stderr, "file open failed!\n"); ft_start=1; fwrite(buffer,1, len,fp); }else if(strncasecmp(buffer, "file send completed\0",20)==0){//파일 전송이 끝난 경우 fprintf(stderr, "file recv complete\n"); fclose(fp); ft_start=0; }else if(ft_start){ fwrite(buffer,1, len,fp); } } //end else break; }//end recv case CCI_EVENT_CONNECT: // An outgoing connection request has completed. assert(event->connect.connection != NULL); assert(event->connect.connection->context == CONNECT_CONTEXT); *connection = event->connect.connection; break; default: fprintf(stderr, "ignoring event type %d\n", event->type); } cci_return_event(event); } }