zhangmeng
2022-12-14 a49287079cb5a97ef65818b70529c9d3bbdd99fa
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
#include <vector>
#include <thread>
#include <chrono>
#include <atomic>
using namespace std;
 
#include "src/bn_api.h"
 
static void test_rr(){
 
  thread([]{
    string base_cont("test_req_rep==");
 
    atomic<uint64_t> index{0};
    vector<thread> v_t;
    for (int i = 0; i < 621; i++){
      v_t.emplace_back([&base_cont, i, &index]{
 
        while (true) {
          // printf("start request\n");
          // auto s = chrono::steady_clock::now();
          auto msg("[Thread("+to_string(i)+")]->"+base_cont+to_string(index++));
          TestRequest(0, msg.c_str(), msg.length());
          this_thread::sleep_for(chrono::milliseconds(10));
          // auto e = chrono::steady_clock::now();
          // printf("======>>thread %d TestRequest time %ld ms\n", i, chrono::duration_cast<chrono::milliseconds>(e-s).count());
        }
      });
    }
 
    while (true) {
      // printf("start request\n");
      // auto s = chrono::steady_clock::now();
      auto msg(base_cont+to_string(index++));
      TestRequest(0, msg.c_str(), msg.length());
      this_thread::sleep_for(chrono::milliseconds(10));
      // auto e = chrono::steady_clock::now();
      // printf("TestRequest time %ld ms\n", chrono::duration_cast<chrono::milliseconds>(e-s).count());
    }
  }).detach();
 
    while(true){
      TestReply(0, -1);
    }
 
}
 
static void test_ps(){
 
  const string t("topics_");
  vector<string> topics;
  for(int i = 0; i < 3; i++){
    topics.emplace_back(t + to_string(i+1));
  }
 
  string base_cont("test_pub_sub==");
  // while (base_cont.size() < 12662) {
  //   base_cont += base_cont;
  // }
  thread([&]{
    this_thread::sleep_for(chrono::seconds(3));
    while (true) {
      for(auto && i : topics){
        auto msg = base_cont + "test_ps pub message "+i+"-->msg";
        TestPub(i.c_str(), i.length(), msg.c_str(), msg.length());
        this_thread::sleep_for(chrono::milliseconds{126});
      }
    }
  }).detach();
 
  for(auto && i : topics){
    TestSub(i.c_str(), i.length(), 0, 0);
  }
  // this_thread::sleep_for(chrono::seconds(3));
  while (true) {
    char *msg;
    int msg_len;
    TestSub(NULL, 0, (void**)&msg, &msg_len);
    this_thread::sleep_for(chrono::seconds{1});
  }
}
 
vector<thread> v_t;
template<class F>
void run_test(F&& f){
  v_t.emplace_back([f]{
    f();
  });
}
 
 
#include "bhome_msg.pb.h"
#include "bhome_msg_api.pb.h"
using namespace bhome_msg;
 
int main(int argc, char const *argv[])
{
  // run_test([&]{test_rr();});
 
  // test_rr();
  // test_ps();
  // return 0;
 
  int reply = 1;
 
  string id("hello-reply");
  if (argc > 1) {
    printf("this is request\n");
    id = "hello-request";
    reply = 0;
  }else{
    printf("this is reply\n");
  }
 
  ProcInfo pi;
  pi.set_proc_id(id);
  pi.set_name("works");
 
  string out;
  pi.SerializeToString(&out);
 
  void* rep;
  int repl = 0;
  BHRegister(out.data(), out.size(), &rep, &repl, 500);
 
  if (reply){
    while (true) {
      void* pid;
      int pidl;
      void* req;
      int reql;
      void* src;
 
      if (BHReadRequest(&pid, &pidl, &req, &reql, &src, 500)){
 
        bhome_msg::MsgRequestTopic msg;
        msg.ParseFromArray(req, reql);
        printf("recv request %d msg data %s\n", reql, msg.data().c_str());
 
        bhome_msg::MsgRequestTopicReply rep;
        rep.set_data(msg.data() + "-reply");
        string srep;
        rep.SerializeToString(&srep);
 
        // auto s = chrono::steady_clock::now();
        int ret = BHSendReply(src, srep.data(), srep.size());
        // auto e = chrono::steady_clock::now();
        // printf("reply time %lu ms\n", chrono::duration_cast<chrono::milliseconds>(e-s).count());
 
      }else{
        // usleep(50000);
        // printf("BHReadRequest no data\n");
      }
    }
  }else{
    printf("start request %d\n", atoi(argv[1]));
    bhome_msg::BHAddress addr;
    addr.set_ip("192.168.20.108");
    addr.set_port(atoi(argv[1]));
 
    string node;
    addr.SerializeToString(&node);
 
    void* pid;
    int pidl;
    void* rep;
    int repl;
    unsigned idx = 0;
    while(1){
 
      bhome_msg::MsgRequestTopic msg;
      msg.set_topic("hello-reply");
      msg.set_data("hell-world-" + to_string(getpid()) + "-" + to_string(idx++));
      string smsg;
      msg.SerializeToString(&smsg);
 
      // auto s = chrono::steady_clock::now();
      int ret = BHRequest(node.data(), node.size(), smsg.data(), smsg.size(),
        &pid, &pidl, &rep, &repl, 5000);
      // auto e = chrono::steady_clock::now();
      // printf("request time %lu ms\n", chrono::duration_cast<chrono::milliseconds>(e-s).count());
 
      bhome_msg::MsgRequestTopicReply prep;
      prep.ParseFromArray(rep, repl);
 
      printf("pid %d BHRequest rep data %s size %d ret %d\n", getpid(), prep.data().c_str(),
        repl, ret);
 
    }
  }
 
  return 0;
}