2015-02-19 13:59:22 +00:00
|
|
|
#include <iostream>
|
2015-02-22 20:35:09 +00:00
|
|
|
#include <string>
|
2015-04-09 14:23:47 +00:00
|
|
|
#include <memory>
|
2015-02-22 20:35:09 +00:00
|
|
|
|
|
|
|
#include <zmq.hpp>
|
|
|
|
|
2015-04-09 14:23:47 +00:00
|
|
|
#include "proto/Messages.pb.h"
|
|
|
|
|
|
|
|
class MyPingService : public Messages::PingService
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Ping(
|
|
|
|
::google::protobuf::RpcController* controller,
|
|
|
|
const ::Messages::PingRequest* request,
|
|
|
|
::Messages::PingReply* response,
|
|
|
|
::google::protobuf::Closure* done) override
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ping2(
|
|
|
|
::google::protobuf::RpcController* controller,
|
|
|
|
const ::Messages::PingRequest* request,
|
|
|
|
::Messages::PingReply* response,
|
|
|
|
::google::protobuf::Closure* done) override
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MyRpcServer
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::shared_ptr<zmq::context_t> m_zmqContext;
|
|
|
|
std::shared_ptr<zmq::socket_t> m_zmqSocket;
|
|
|
|
|
|
|
|
MyPingService m_pingService;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MyRpcServer(const std::string &bindString)
|
|
|
|
{
|
|
|
|
m_zmqContext = std::make_shared<zmq::context_t>(1);
|
|
|
|
m_zmqSocket = std::make_shared<zmq::socket_t>(*m_zmqContext, ZMQ_REQ);
|
|
|
|
m_zmqSocket->bind(bindString.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Run()
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-02-19 13:59:22 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
std::cout << "Server running" << std::endl;
|
|
|
|
|
2015-04-09 14:23:47 +00:00
|
|
|
MyRpcServer server("tcp://*:5555");
|
|
|
|
server.Run();
|
|
|
|
|
2015-02-22 20:35:09 +00:00
|
|
|
zmq::context_t context(1);
|
|
|
|
zmq::socket_t socket(context, ZMQ_REP);
|
|
|
|
socket.bind("tcp://*:5555");
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
zmq::message_t request;
|
|
|
|
socket.recv(&request);
|
|
|
|
|
2015-04-09 14:23:47 +00:00
|
|
|
Messages::PingRequest requestMessage;
|
2015-02-22 20:35:09 +00:00
|
|
|
requestMessage.ParseFromArray(request.data(), request.size());
|
|
|
|
|
|
|
|
std::cout << "Received request: " << requestMessage.text() << std::endl;
|
|
|
|
|
2015-04-09 14:23:47 +00:00
|
|
|
Messages::PingReply replyMessage;
|
2015-02-22 20:35:09 +00:00
|
|
|
replyMessage.set_text("Hello Client");
|
|
|
|
|
|
|
|
zmq::message_t reply(replyMessage.ByteSize());
|
|
|
|
replyMessage.SerializeToArray(reply.data(), reply.size());
|
|
|
|
socket.send(reply);
|
|
|
|
}
|
|
|
|
|
2015-02-19 13:59:22 +00:00
|
|
|
return 0;
|
2015-04-09 14:23:47 +00:00
|
|
|
}
|
|
|
|
|