| | |
| | | #include "dgram_mod_socket.h" |
| | | #include "shm_mm.h" |
| | | #include "shm_mm_wraper.h" |
| | | #include "usg_common.h" |
| | | #include "lock_free_queue.h" |
| | | |
| | |
| | | |
| | | }Targ; |
| | | |
| | | LockFreeQueue<task_t, DM_Allocator> task_queue(100); |
| | | |
| | | LockFreeQueue<task_t, DM_Allocator> task_queue(128); |
| | | void *client; |
| | | |
| | | void *worker(void *socket) { |
| | | pthread_detach(pthread_self()); |
| | |
| | | } |
| | | |
| | | void server(int port) { |
| | | void *socket = dgram_mod_open_socket(REQ_REP); |
| | | void *socket = dgram_mod_open_socket(); |
| | | dgram_mod_bind(socket, port); |
| | | initThreadPool(socket); |
| | | |
| | |
| | | dgram_mod_close_socket(socket); |
| | | } |
| | | |
| | | void client(int port) { |
| | | void *socket = dgram_mod_open_socket(REQ_REP); |
| | | void startClient(int port) { |
| | | void *socket = dgram_mod_open_socket(); |
| | | int size; |
| | | void *recvbuf; |
| | | char sendbuf[512]; |
| | | while (true) { |
| | | printf("request: "); |
| | | scanf("%s", sendbuf); |
| | | dgram_mod_sendandrecv(socket, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &size); |
| | | printf("reply: %s\n", (char *)recvbuf); |
| | | free(recvbuf); |
| | | } |
| | | dgram_mod_close_socket(socket); |
| | | } |
| | | |
| | | void client2(int port) { |
| | | void *socket = dgram_mod_open_socket(); |
| | | int size; |
| | | void *recvbuf; |
| | | char sendbuf[512]; |
| | | while (true) { |
| | | sprintf(sendbuf, "hello\n" ); |
| | | dgram_mod_sendandrecv(socket, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &size); |
| | | printf("reply: %s\n", (char *)recvbuf); |
| | | free(recvbuf); |
| | |
| | | char sendbuf[512]; |
| | | int scale = 100000; |
| | | int i; |
| | | void *socket = dgram_mod_open_socket(REQ_REP); |
| | | |
| | | |
| | | char filename[512]; |
| | | sprintf(filename, "test%d.txt", targ->id); |
| | | sprintf(filename, "test%d.tmp", targ->id); |
| | | FILE *fp = NULL; |
| | | fp = fopen(filename, "w+"); |
| | | |
| | |
| | | for (i = 0; i < scale; i++) { |
| | | sprintf(sendbuf, "thread(%d) %d", targ->id, i); |
| | | fprintf(fp, "requst:%s\n", sendbuf); |
| | | dgram_mod_sendandrecv(socket, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &recvsize); |
| | | dgram_mod_sendandrecv(client, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &recvsize); |
| | | fprintf(fp, "reply: %s\n", (char *)recvbuf); |
| | | free(recvbuf); |
| | | } |
| | | fclose(fp); |
| | | dgram_mod_close_socket(socket); |
| | | |
| | | return (void *)i; |
| | | } |
| | | |
| | |
| | | char sendbuf[512]; |
| | | |
| | | struct timeval start; |
| | | |
| | | client = dgram_mod_open_socket(); |
| | | |
| | | gettimeofday(&start, NULL); |
| | | for (i = 0; i < processors; i++) { |
| | | targs[i].port = port; |
| | |
| | | long diffsec = (long) (difftime/1000000); |
| | | long diffmsec = difftime - diffsec*1000000; |
| | | printf("cost: %ld sec: %ld msc\n", diffsec, diffmsec); |
| | | dgram_mod_close_socket(client); |
| | | } |
| | | |
| | | |
| | | int main(int argc, char *argv[]) { |
| | | shm_init(512); |
| | | shm_mm_wrapper_init(512); |
| | | int port; |
| | | if (argc < 3) { |
| | | fprintf(stderr, "Usage: reqrep %s|%s <PORT> ...\n", "server", "client"); |
| | |
| | | port = atoi(argv[2]); |
| | | |
| | | if (strcmp("server", argv[1]) == 0) { |
| | | // int temp = shm_mm_wrapper_alloc_key(); |
| | | // printf("tmp=%d\n", temp); |
| | | server(port); |
| | | } |
| | | |
| | | if (strcmp("client", argv[1]) == 0) |
| | | } else if (strcmp("mclient", argv[1]) == 0) { |
| | | startClients(port); |
| | | } else if (strcmp("client", argv[1]) == 0) { |
| | | startClient(port); |
| | | } else { |
| | | printf("input invalidate arguments\n"); |
| | | } |
| | | |
| | | |
| | | return 0; |