|
| 1 | +#include "event.pb.h" |
| 2 | +#include <chrono> |
| 3 | +#include <include/common.h> |
| 4 | +#include <spdlog/spdlog.h> |
| 5 | +#include <thread> |
| 6 | +#include <zmq.hpp> |
| 7 | + |
| 8 | +int main() |
| 9 | +{ |
| 10 | + using namespace std::chrono_literals; |
| 11 | + |
| 12 | + // initialize the zmq context with a single IO thread |
| 13 | + zmq::context_t context{ 1 }; |
| 14 | + |
| 15 | + // construct a REP (reply) socket and bind to interface |
| 16 | + zmq::socket_t socket{ context, zmq::socket_type::rep }; |
| 17 | + socket.bind("tcp://*:5555"); |
| 18 | + |
| 19 | + // prepare some static data for responses |
| 20 | + const std::string senderName{ "Server" }; |
| 21 | + |
| 22 | + for (;;) { |
| 23 | + zmq::message_t request; |
| 24 | + |
| 25 | + // receive a request from client |
| 26 | + socket.recv(request, zmq::recv_flags::none); |
| 27 | + own::proto::Event receivedEvent; |
| 28 | + receivedEvent.ParseFromArray(request.data(), static_cast<int>(request.size())); |
| 29 | + |
| 30 | + // simulate work |
| 31 | + 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()); |
| 49 | + |
| 50 | + if (receivedEvent.ping_count() == Common::maxCount) { break; } |
| 51 | + } |
| 52 | + |
| 53 | + return 0; |
| 54 | +} |
0 commit comments