wangzhengquan
2021-01-22 7e1e05df84d57d2d7c3a622d0ece0d4fe7b1fc8c
update
2个文件已添加
1个文件已修改
129 ■■■■■ 已修改文件
CMakeLists.txt 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test_socket/CMakeLists.txt 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test_socket/bus_test.cpp 117 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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)
test_socket/CMakeLists.txt
New file
@@ -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}
                            )
test_socket/bus_test.cpp
New file
@@ -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;
}