From d26327b3cde043a9470dcd7fea8e704ea517fdae Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 01 四月 2021 19:26:57 +0800
Subject: [PATCH] add req/rep center;

---
 utest/utest.cpp |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)

diff --git a/utest/utest.cpp b/utest/utest.cpp
index b95e646..54c6d6f 100644
--- a/utest/utest.cpp
+++ b/utest/utest.cpp
@@ -1,6 +1,8 @@
 #include "defs.h"
 #include "pubsub.h"
 #include "pubsub_center.h"
+#include "reqrep.h"
+#include "reqrep_center.h"
 #include "socket.h"
 #include "util.h"
 #include <atomic>
@@ -150,6 +152,62 @@
 	bus.Stop();
 }
 
+BOOST_AUTO_TEST_CASE(ReqRepTest)
+{
+	const std::string shm_name("ShmReqRep");
+	ShmRemover auto_remove(shm_name);
+	SharedMemory shm(shm_name, 1024 * 1024 * 50);
+
+	auto Avail = [&]() { return shm.get_free_memory(); };
+	auto init_avail = Avail();
+	int *flag = shm.find_or_construct<int>("flag")(123);
+	printf("flag = %d\n", *flag);
+	++*flag;
+
+	ReqRepCenter center(shm);
+	center.Start(2);
+	std::atomic<bool> run(true);
+
+	auto Client = [&](const std::string &topic, const int nreq) {
+		SocketRequest client(shm);
+		std::string reply;
+		boost::timer::auto_cpu_timer timer;
+		for (int i = 0; i < nreq; ++i) {
+			if (!client.SyncRequest(topic, "data " + std::to_string(i), reply, 1000)) {
+				printf("client request failed\n");
+			}
+		}
+		printf("request %s %d done ", topic.c_str(), nreq);
+	};
+	auto Server = [&](const std::string &name, const std::vector<std::string> &topics) {
+		SocketReply server(shm);
+		ProcInfo info;
+		info.set_name(name);
+		info.set_info(name);
+		if (!server.Register(info, topics, 100)) {
+			printf("register failed\n");
+		}
+		auto onData = [](const std::string &topic, const std::string &data, std::string &reply) {
+			reply = topic + ':' + data;
+			return true;
+		};
+		server.StartWorker(onData);
+		while (run) {
+			std::this_thread::yield();
+		}
+	};
+	ThreadManager clients, servers;
+	std::vector<std::string> topics = {"topic1", "topic2"};
+	servers.Launch(Server, "server", topics);
+	std::this_thread::sleep_for(100ms);
+	for (auto &t : topics) {
+		clients.Launch(Client, t, 1000 * 100);
+	}
+	clients.WaitAll();
+	run = false;
+	servers.WaitAll();
+}
+
 inline int MyMin(int a, int b)
 {
 	printf("MyMin\n");

--
Gitblit v1.8.0