Skip to content

Commit 2f32b55

Browse files
committed
rework after PR review
1 parent ea68d1d commit 2f32b55

3 files changed

Lines changed: 66 additions & 51 deletions

File tree

src/protobuf.cppzmq/client/main.cpp

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,25 @@ int main()
1111

1212
// construct a REQ (request) socket and connect to interface
1313
zmq::socket_t socket{ context, zmq::socket_type::req };
14-
socket.connect("tcp://localhost:5555");
14+
socket.connect("tcp://127.0.0.1:5555");
1515

1616
// set up some static data to send
1717
const std::string senderName{ "Client" };
18-
int32_t pingCount = 0;
19-
int32_t pongCount = 0;
2018

21-
for (;;) {
22-
// send the request message
23-
own::proto::Event event;
24-
event.set_sender(senderName);
25-
event.set_action(own::proto::Event::PONG);
26-
event.set_ping_count(pingCount);
27-
event.set_pong_count(pongCount);
28-
29-
std::string data;
30-
event.SerializeToString(&data);
19+
zmq::message_t request{};
20+
// send the request message
21+
auto [sendEvent, serializedData] = Common::prepareMessage(senderName, request);
3122

23+
for (;;) {
3224
// send to the server
33-
socket.send(zmq::buffer(data), zmq::send_flags::none);
25+
socket.send(zmq::buffer(serializedData), zmq::send_flags::none);
3426

3527
// wait for reply from server
36-
zmq::message_t reply{};
37-
socket.recv(reply, zmq::recv_flags::none);
38-
39-
own::proto::Event receivedEvent;
40-
receivedEvent.ParseFromArray(reply.data(), static_cast<int>(reply.size()));
41-
pingCount = receivedEvent.ping_count();
42-
pongCount = receivedEvent.pong_count() + 1;
43-
44-
spdlog::info("Received from: {} Action: {} Ping count: {} Pong count: {}",
45-
receivedEvent.sender(),
46-
own::proto::Event_PingPong_Name(receivedEvent.action()),
47-
receivedEvent.ping_count(),
48-
receivedEvent.pong_count());
28+
socket.recv(request, zmq::recv_flags::none);
29+
const auto [receivedEvent, _] = Common::prepareMessage(senderName, request);
30+
serializedData = Common::incrementPingPong(sendEvent, own::proto::Event::PONG);
31+
sendEvent = receivedEvent;
32+
Common::logProtoEvent(receivedEvent);
4933

5034
if (receivedEvent.pong_count() == Common::maxCount) { break; }
5135
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
1+
#include <spdlog/spdlog.h>
2+
#include <zmq.hpp>
3+
14
namespace Common {
25
constexpr auto maxCount = 10;
6+
7+
std::tuple<own::proto::Event, std::string> prepareMessage(const std::string &senderName, const zmq::message_t &request)
8+
{
9+
own::proto::Event receivedEvent{};
10+
std::string data;
11+
receivedEvent.ParseFromArray(request.data(), static_cast<int>(request.size()));
12+
13+
own::proto::Event event{};
14+
event.set_sender(senderName);
15+
event.SerializeToString(&data);
16+
return std::make_tuple(receivedEvent, data);
317
}
18+
19+
[[nodiscard]]std::string incrementPingPong(own::proto::Event& event, own::proto::Event_PingPong eventType)
20+
{
21+
switch (eventType) {
22+
case own::proto::Event::PING: {
23+
event.set_action(own::proto::Event::PING);
24+
event.set_ping_count(event.ping_count() + 1);
25+
event.set_pong_count(event.pong_count());
26+
}
27+
case own::proto::Event::PONG: {
28+
event.set_action(own::proto::Event::PONG);
29+
event.set_ping_count(event.ping_count());
30+
event.set_pong_count(event.pong_count() + 1);
31+
break;
32+
}
33+
default: {
34+
break;
35+
}
36+
}
37+
std::string data;
38+
event.SerializeToString(&data);
39+
return data;
40+
}
41+
42+
void logProtoEvent(const own::proto::Event &receivedEvent)
43+
{
44+
spdlog::info("Received from: {} - Action: {} - Ping count: {} - Pong count: {}",
45+
receivedEvent.sender(),
46+
own::proto::Event_PingPong_Name(receivedEvent.action()),
47+
receivedEvent.ping_count(),
48+
receivedEvent.pong_count());
49+
}
50+
}// namespace Common

src/protobuf.cppzmq/server/main.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include "event.pb.h"
22
#include <chrono>
33
#include <include/common.h>
4-
#include <spdlog/spdlog.h>
54
#include <thread>
6-
#include <zmq.hpp>
75

86
int main()
97
{
@@ -19,33 +17,19 @@ int main()
1917
// prepare some static data for responses
2018
const std::string senderName{ "Server" };
2119

20+
zmq::message_t request{};
2221
for (;;) {
23-
zmq::message_t request;
24-
2522
// receive a request from client
2623
socket.recv(request, zmq::recv_flags::none);
27-
own::proto::Event receivedEvent;
28-
receivedEvent.ParseFromArray(request.data(), static_cast<int>(request.size()));
29-
3024
// simulate work
3125
std::this_thread::sleep_for(500ms);
32-
own::proto::Event event;
33-
event.set_sender(senderName);
34-
event.set_action(own::proto::Event::PING);
35-
event.set_ping_count(receivedEvent.ping_count() + 1);
36-
event.set_pong_count(receivedEvent.pong_count());
37-
38-
std::string data;
39-
event.SerializeToString(&data);
40-
41-
// send the reply to the client
42-
socket.send(zmq::buffer(data), zmq::send_flags::none);
43-
44-
spdlog::info("Received from: {} Action: {} Ping count: {} Pong count: {}",
45-
receivedEvent.sender(),
46-
own::proto::Event_PingPong_Name(receivedEvent.action()),
47-
receivedEvent.ping_count(),
48-
receivedEvent.pong_count());
26+
27+
auto [receivedEvent, serializedData] = Common::prepareMessage(senderName, request);
28+
serializedData = Common::incrementPingPong(receivedEvent, own::proto::Event::PING);
29+
Common::logProtoEvent(receivedEvent);
30+
31+
// send the reply to the client
32+
socket.send(zmq::buffer(serializedData), zmq::send_flags::none);
4933

5034
if (receivedEvent.ping_count() == Common::maxCount) { break; }
5135
}

0 commit comments

Comments
 (0)