From 7e1e05df84d57d2d7c3a622d0ece0d4fe7b1fc8c Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期五, 22 一月 2021 18:03:13 +0800 Subject: [PATCH] update --- test_socket/CMakeLists.txt | 11 +++ test_socket/bus_test.cpp | 117 +++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 1 3 files changed, 129 insertions(+), 0 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 00acf78..6406fa6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,4 +21,5 @@ add_subdirectory(${PROJECT_SOURCE_DIR}/src) add_subdirectory(${PROJECT_SOURCE_DIR}/test) +add_subdirectory(${PROJECT_SOURCE_DIR}/test_socket) add_subdirectory(${PROJECT_SOURCE_DIR}/test_net_socket) \ No newline at end of file diff --git a/test_socket/CMakeLists.txt b/test_socket/CMakeLists.txt new file mode 100644 index 0000000..a3f3fa5 --- /dev/null +++ b/test_socket/CMakeLists.txt @@ -0,0 +1,11 @@ +# add the executable +add_executable(bus_test bus_test.cpp) +target_link_libraries(bus_test PRIVATE shm_queue ${EXTRA_LIBS} ) +target_include_directories(bus_test PRIVATE + "${PROJECT_BINARY_DIR}" + ${EXTRA_INCLUDES} + ) + + + + diff --git a/test_socket/bus_test.cpp b/test_socket/bus_test.cpp new file mode 100644 index 0000000..b815476 --- /dev/null +++ b/test_socket/bus_test.cpp @@ -0,0 +1,117 @@ +#include "bus_server_socket.h" +#include "shm_mod_socket.h" +#include "shm_mm_wrapper.h" +#include "usg_common.h" +#include "mm.h" + +BusServerSocket * server_socket; +void sigint_handler(int sig) { + + exit(0); +} + +void server(int key) { + server_socket = new BusServerSocket(); + + server_socket->bind( key); + + server_socket->start(); +} + + +void *run_recv(void *skptr) { + pthread_detach(pthread_self()); + void *recvbuf; + int size; + int key; + ShmModSocket *sk = (ShmModSocket *)skptr; + while ( true) { + sk->recvfrom( &recvbuf, &size, &key); + printf("鏀跺埌璁㈤槄娑堟伅:%s\n", recvbuf); + free(recvbuf); + } + +} + +void client(int key) { + ShmModSocket *sk = new ShmModSocket(); + + pthread_t tid; + pthread_create(&tid, NULL, run_recv, (void *)sk); + int size; + + char action[512]; + char topic[512]; + char content[512]; + long i = 0; + while (true) { + //printf("Usage: pub <topic> [content] or sub <topic>\n"); + printf("Can I help you? sub, pub, desub or quit\n"); + scanf("%s",action); + + if(strcmp(action, "sub") == 0) { + printf("Please input topic!\n"); + scanf("%s", topic); + if (sk->sub(topic, strlen(topic), key) == 0) { + printf("%d Sub success!\n", sk->get_key()); + } else { + printf("Sub failture!\n"); + exit(0); + } + + } else if(strcmp(action, "desub") == 0) { + printf("Please input topic!\n"); + scanf("%s", topic); + if (sk->desub(topic, strlen(topic), key) == 0) { + printf("%d Desub success!\n", sk->get_key()); + } else { + printf("Desub failture!\n"); + exit(0); + } + + } else if(strcmp(action, "pub") == 0) { + // printf("%s %s %s\n", action, topic, content); + printf("Please input topic and content\n"); + scanf("%s %s", topic, content); + if(sk->pub(topic, strlen(topic)+1, content, strlen(content)+1, key) == 0){ + printf("%d Pub success!\n", sk->get_key()); + } else { + printf("Pub failture!\n"); + } + + } else if(strcmp(action, "quit") == 0) { + printf("(%d) quit\n", sk->get_key()); + delete sk; + break; + } else { + printf("error input argument\n"); + continue; + } + + } + +} + + + +int main(int argc, char *argv[]) { + shm_mm_wrapper_init(512); + int key; + if (argc < 3) { + fprintf(stderr, "Usage: %s %s|%s <key> ...\n", argv[0], "server", "client"); + return 1; + } + + key = atoi(argv[2]); + + if (strcmp("server", argv[1]) == 0) { + server(key); + + } else if (strcmp("client", argv[1]) == 0) { + client(key); + } + + + + return 0; +} \ No newline at end of file -- Gitblit v1.8.0