From 1d1c02c006dd9561f8d50ffda5b16e29d81997fd Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期五, 17 七月 2020 19:10:51 +0800 Subject: [PATCH] update --- queue/include/shm_socket.h | 2 queue/libshm_queue.a | 0 test2/survey.c | 86 ++++++++++++++++++++++++++++ /dev/null | 48 ---------------- queue/include/mod_socket.h | 2 queue/shm_socket.c | 2 test2/pub_sub.c | 1 queue/mod_socket.c | 11 ++- test2/pub_sub | 0 test2/req_rep | 0 10 files changed, 98 insertions(+), 54 deletions(-) diff --git a/queue/include/mod_socket.h b/queue/include/mod_socket.h index 0ce74fd..79c86c5 100644 --- a/queue/include/mod_socket.h +++ b/queue/include/mod_socket.h @@ -30,7 +30,7 @@ int mod_connect(void * _socket, int port); -int mod_send(void * _socket, void *buf, int size); +int mod_send(void * _socket, const void *buf, const int size); int mod_recv(void * _socket, void **buf, int *size) ; diff --git a/queue/include/shm_socket.h b/queue/include/shm_socket.h index 2147857..789c55f 100644 --- a/queue/include/shm_socket.h +++ b/queue/include/shm_socket.h @@ -84,7 +84,7 @@ int shm_connect(shm_socket_t * socket, int port); -int shm_send(shm_socket_t * socket, void *buf, int size) ; +int shm_send(shm_socket_t * socket, const void *buf, const int size) ; int shm_recv(shm_socket_t * socket, void **buf, int *size) ; diff --git a/queue/libshm_queue.a b/queue/libshm_queue.a index f082fa6..4468b07 100644 --- a/queue/libshm_queue.a +++ b/queue/libshm_queue.a Binary files differ diff --git a/queue/mod_socket.c b/queue/mod_socket.c index 80b5525..ba70580 100644 --- a/queue/mod_socket.c +++ b/queue/mod_socket.c @@ -110,7 +110,7 @@ } -int mod_send(void * _socket, void *buf, int size) { +int mod_send(void * _socket, const void *buf, const int size) { mod_socket_t * socket = (mod_socket_t *) _socket; std::map<int, shm_socket_t* > *clientSocketMap = socket->shm_socket->clientSocketMap; std::map<int, shm_socket_t* >::iterator iter; @@ -122,13 +122,14 @@ rv = shm_send(socket->client_socket, buf, size); SemUtil::inc(socket->slots); break; + case SURVEY: case PUB_SUB: for(iter = clientSocketMap->begin(); iter != clientSocketMap->end(); iter++) { rv = shm_send(iter->second, buf, size); } break; default: - err_exit(0, "涓嶆敮鎸佺殑妯″紡%d", socket->mod); + rv = shm_send(socket->client_socket, buf, size); } return rv; @@ -158,8 +159,12 @@ case PUB_SUB: rv = 0; break; + + case SURVEY: default: - err_exit(0, "涓嶆敮鎸佺殑妯″紡%d", socket->mod); + rv = socket->recvQueue->pop(entry); + *buf = entry.buf; + *size = entry.size; } return rv; diff --git a/queue/shm_socket.c b/queue/shm_socket.c index 27c5b0a..6666db1 100644 --- a/queue/shm_socket.c +++ b/queue/shm_socket.c @@ -306,7 +306,7 @@ -int shm_send(shm_socket_t *socket, void *buf, int size) { +int shm_send(shm_socket_t *socket, const void *buf, const int size) { // hashtable_t *hashtable = mm_get_hashtable(); if(socket->remoteQueue == NULL) { err_msg(errno, "褰撳墠瀹㈡埛绔棤杩炴帴!"); diff --git a/test2/pub_sub b/test2/pub_sub index 19773d9..f8cba8f 100755 --- a/test2/pub_sub +++ b/test2/pub_sub Binary files differ diff --git a/test2/pub_sub.c b/test2/pub_sub.c index cf2b507..78aff69 100644 --- a/test2/pub_sub.c +++ b/test2/pub_sub.c @@ -50,6 +50,7 @@ if (strcmp("client", argv[1]) == 0) client(port); + shm_destroy(); // fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", "server", "client"); return 0; diff --git a/test2/req_rep b/test2/req_rep index e3e7f24..ffc46d9 100755 --- a/test2/req_rep +++ b/test2/req_rep Binary files differ diff --git a/test2/sub b/test2/sub deleted file mode 100755 index ba91707..0000000 --- a/test2/sub +++ /dev/null Binary files differ diff --git a/test2/sub.c b/test2/sub.c deleted file mode 100644 index d90928f..0000000 --- a/test2/sub.c +++ /dev/null @@ -1,48 +0,0 @@ -#include "shm_queue_wrapper.h" - -typedef struct msg_t -{ - int key; - char buf[100]; - -} msg_t; - -void client() { - int key = -1; - size_t qsize = 16; - void * remote_queue = shmqueue_attach( 1); - void * local_queue = shmqueue_create( &key, qsize); - // message_t item; - msg_t msg; - msg.key = key; - - void * rec_msg; - int rec_msg_size; - - sprintf(msg.buf, "鎴戣璁㈤槄浠婃棩澶存潯"); - shmqueue_push(remote_queue, (void *)&msg, sizeof(msg)); - //鍏ラ槦 - while(true) { - - //printf("send: %s\n", msg.buf); - shmqueue_pop(local_queue, &rec_msg, &rec_msg_size); - printf("鏀跺埌璁㈤槄娑堟伅: %s\n", ((msg_t*)rec_msg)->buf); - free(rec_msg); - - - } - - //閿�姣侀槦鍒� - shmqueue_drop(local_queue); - shmqueue_drop(remote_queue); -} - - -int main () { - shm_init(512); - client(); - - //鏁翠釜杩涚▼閫�鍑烘椂闇�瑕佹墽琛岃繖涓柟娉曪紝璇ユ柟娉曢鍏堜細妫�鏌ユ槸鍚﹁繕鏈夊叾浠栬繘绋嬪湪浣跨敤璇ュ叡浜唴瀛橈紝濡傛灉杩樻湁鍏朵粬杩涚▼鍦ㄤ娇鐢ㄥ氨鍙槸detach,濡傛灉娌℃湁鍏朵粬杩涚▼鍦ㄤ娇鐢ㄥ垯閿�姣佹暣鍧楀唴瀛樸�� - shm_destroy(); - return 0; -} \ No newline at end of file diff --git a/test2/survey.c b/test2/survey.c new file mode 100644 index 0000000..d5dac85 --- /dev/null +++ b/test2/survey.c @@ -0,0 +1,86 @@ +#include "mod_socket.h" +#include "shm_mm.h" +#include "usg_common.h" + +#define SERVER "server" +#define CLIENT "client" +#define DATE "DATE" + +char *date(void) { + time_t now = time(&now); + struct tm *info = localtime(&now); + char *text = asctime(info); + text[strlen(text) - 1] = '\0'; // remove '\n' + return (text); +} + +int server(const int port) { + int rv; + + void *socket = mod_open_socket(PUB_SUB); + mod_socket_bind(socket, port); + if ((rv = mod_listen(socket)) != 0) { + printf("mod_listen"); + } + for (;;) { + printf("SERVER: SENDING DATE SURVEY REQUEST\n"); + if ((rv = mod_send(socket, DATE, strlen(DATE) + 1)) != 0) { + printf("mod_send"); + } + + for (;;) { + char *buf = NULL; + int sz; + rv = mod_recv(socket, (void **)&buf, &sz); + + if (rv != 0) { + printf("mod_recv"); + } + printf("SERVER: RECEIVED \"%s\" SURVEY RESPONSE\n", buf); + mod_free(buf, sz); + } + + printf("SERVER: SURVEY COMPLETE\n"); + } +} + +int client(const int port, const char *name) { + int rv; + + void *socket = mod_open_socket(PUB_SUB); + if ((rv = mod_connect(socket, port)) != 0) { + printf("mod_connect"); + } + for (;;) { + char *buf = NULL; + int sz; + if ((rv = mod_recv(socket, (void **)&buf, &sz)) == 0) { + printf("CLIENT (%s): RECEIVED \"%s\" SURVEY REQUEST\n", name, buf); + mod_free(buf, sz); + char *d = date(); + printf("CLIENT (%s): SENDING DATE SURVEY RESPONSE\n", name); + if ((rv = mod_send(socket, d, strlen(d) + 1, 0)) != 0) { + printf("mod_send"); + } + } + } +} + +int main(const int argc, const char **argv) { + shm_init(512); + int port; + if ((argc >= 2) && (strcmp(SERVER, argv[1]) == 0)) { + port = atoi(argv[2]); + (server(port)); + } + else if ((argc >= 3) && (strcmp(CLIENT, argv[1]) == 0)) { + port = atoi(argv[2]); + (client(port, argv[3])); + } else { + fprintf(stderr, "Usage: survey %s|%s <URL> <ARG> ...\n", SERVER, CLIENT); + } + + shm_destroy(); + + return 0; +} \ No newline at end of file -- Gitblit v1.8.0