zhangmeng
2024-04-09 2561a007b8d8999a4750046d0cfb3b1ad5af50ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "bus_server_socket.h"
#include "shm_mod_socket.h"
#include "shm_mm_wrapper.h"
#include "usg_common.h"
#include "mm.h"
#include "logger_factory.h"
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "bus_error.h"
 
#include "bh_api.h"
#include "proc_def.h"
 
static ProcInfo proc_desc;
 
int main(int argc, char *argv[]) {
  int ret;
  void *buf_temp = NULL;
  void *errBuf = NULL;
  void *proc_id = NULL;
  void *src = NULL;
  void *data_buf = NULL;
  char topics_buf[200] = { 0x00 };
  int size;
  int id_len;
  int count = 0;
  const int timeout_ms = 3000;
 
  memset(&proc_desc, 0x00, sizeof(ProcInfo));
  strncpy(proc_desc.proc_id, "Hello", strlen("Hello"));
  strncpy(proc_desc.name, "World", strlen("World"));
  strncpy(proc_desc.public_info, "Good", strlen("Good") + 1);
 
  const char *topics_server_specific_reg_buf1 = "Server Specific topics demo1";
  const char *topics_server_specific_reg_buf2 = "Server Specific Hello World";
 
  ret = BHRegister(&proc_desc, (const int)sizeof(ProcInfo), &buf_temp, &size, timeout_ms);
  if (ret == true) {
    printf("the process registered OKay\n");
 
    BHFree(buf_temp, size);
 
  } else {
      BHGetLastError(&errBuf, &size);
 
    printf("the process registered fail with error: %s\n", (char *)errBuf);
 
    BHFree(errBuf, size);
  };
  
  ret = BHRegisterTopics(topics_server_specific_reg_buf1, strlen(topics_server_specific_reg_buf1), &buf_temp, &size, timeout_ms);
  if (ret == true) {
    printf("the process registered topics OKay\n");
 
    BHFree(buf_temp, size);
 
  } else {
    BHGetLastError(&errBuf, &size);
    printf("the process registered1 fail with errorL %s\n", (char *)errBuf);
 
    BHFree(errBuf, size);
  };
  
  ret = BHRegisterTopics(topics_server_specific_reg_buf2, strlen(topics_server_specific_reg_buf2), &buf_temp, &size, timeout_ms);
  if (ret == true) {
    printf("the process registered topics OKay\n");
 
    BHFree(buf_temp, size);
 
  } else {
    BHGetLastError(&errBuf, &size);
    printf("the process registered1 fail with errorL %s\n", (char *)errBuf);
 
    BHFree(errBuf, size);
  };
  
  while(true) {
    ret = BHReadRequest(&proc_id, &id_len, &buf_temp, &size, &src, -1);
    if (ret == true) {
 
      strncpy(topics_buf, (char *)buf_temp, (sizeof(topics_buf) - 1) > size ? size : (sizeof(topics_buf) - 1));
      printf("Get data(%s)", topics_buf);
      
      memset(topics_buf, 0x00, sizeof(topics_buf));
      strncpy(topics_buf, (char *)proc_id, (sizeof(topics_buf) - 1) > id_len ? id_len : (sizeof(topics_buf) - 1));
      printf("proc id(%s)", topics_buf);
      
      BHFree(buf_temp, size);
      BHFree(proc_id, id_len);
      
      memset(topics_buf, 0x00, sizeof(topics_buf));
      sprintf(topics_buf, "Get data count: %d", ++count);
      ret = BHSendReply(src, topics_buf, strlen(topics_buf));
      if (ret == true) {
        
        printf("the process send reply OKay\n");
        
      } else {
        
        BHGetLastError(&errBuf, &size);
        printf("the process send reply fail with errorL %s\n", (char *)errBuf);
 
        BHFree(errBuf, size);
      };
 
      BHFree(src, size);
 
    } else {
      BHGetLastError(&errBuf, &size);
      printf("BHReadRequest fail with error(%s)\n", (char *)errBuf);
 
      BHFree(errBuf, size);
    };
  }
  
  return 0;
 
}