2015-02-19 13:59:22 +00:00
|
|
|
#include <iostream>
|
2015-02-22 19:29:32 +00:00
|
|
|
#include <string>
|
2015-04-09 14:23:47 +00:00
|
|
|
#include <memory>
|
2015-02-19 13:59:22 +00:00
|
|
|
|
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 MyRpcChannel : public google::protobuf::RpcChannel
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::shared_ptr<zmq::context_t> m_zmqContext;
|
|
|
|
std::shared_ptr<zmq::socket_t> m_zmqSocket;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MyRpcChannel(const std::string &connectString)
|
|
|
|
{
|
|
|
|
m_zmqContext = std::make_shared<zmq::context_t>(1);
|
|
|
|
m_zmqSocket = std::make_shared<zmq::socket_t>(*m_zmqContext, ZMQ_REQ);
|
|
|
|
m_zmqSocket->connect(connectString.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CallMethod(
|
|
|
|
const google::protobuf::MethodDescriptor* method,
|
|
|
|
google::protobuf::RpcController* controller,
|
|
|
|
const google::protobuf::Message* request,
|
|
|
|
google::protobuf::Message* response,
|
|
|
|
google::protobuf::Closure* done) override
|
|
|
|
{
|
|
|
|
std::cout << "sending message type " << request->GetDescriptor()->full_name() << std::endl;
|
|
|
|
}
|
|
|
|
};
|
2015-02-22 19:18:51 +00:00
|
|
|
|
2015-02-19 13:59:22 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
std::cout << "Client running" << std::endl;
|
|
|
|
|
2015-02-22 20:35:09 +00:00
|
|
|
zmq::context_t context(1);
|
|
|
|
zmq::socket_t socket(context, ZMQ_REQ);
|
|
|
|
socket.connect("tcp://localhost:5555");
|
|
|
|
|
2015-04-09 14:23:47 +00:00
|
|
|
MyRpcChannel channel("tcp://localhost:5555");
|
|
|
|
Messages::PingService::Stub pingService(&channel);
|
|
|
|
|
|
|
|
Messages::PingRequest pingRequest;
|
|
|
|
pingRequest.set_text("Ping");
|
|
|
|
|
|
|
|
Messages::PingReply pingReply;
|
|
|
|
|
|
|
|
pingService.Ping(nullptr, &pingRequest, &pingReply, nullptr);
|
|
|
|
pingService.Ping2(nullptr, &pingRequest, &pingReply, nullptr);
|
|
|
|
|
2015-02-22 20:35:09 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2015-04-09 14:23:47 +00:00
|
|
|
Messages::PingRequest requestMessage;
|
2015-02-22 20:35:09 +00:00
|
|
|
requestMessage.set_text("Hello Server");
|
|
|
|
|
|
|
|
zmq::message_t request(requestMessage.ByteSize());
|
|
|
|
requestMessage.SerializeToArray(request.data(), request.size());
|
|
|
|
socket.send(request);
|
2015-02-22 19:29:32 +00:00
|
|
|
|
2015-02-22 20:35:09 +00:00
|
|
|
zmq::message_t reply;
|
|
|
|
socket.recv(&reply);
|
2015-02-22 19:29:32 +00:00
|
|
|
|
2015-04-09 14:23:47 +00:00
|
|
|
Messages::PingReply replyMessage;
|
2015-02-22 20:35:09 +00:00
|
|
|
replyMessage.ParseFromArray(reply.data(), reply.size());
|
2015-02-22 19:29:32 +00:00
|
|
|
|
2015-02-22 20:35:09 +00:00
|
|
|
std::cout << "Received reply: " << replyMessage.text() << std::endl;
|
2015-02-22 19:29:32 +00:00
|
|
|
|
2015-02-22 20:35:09 +00:00
|
|
|
Sleep(1000);
|
|
|
|
}
|
2015-02-22 19:29:32 +00:00
|
|
|
|
2015-02-19 13:59:22 +00:00
|
|
|
return 0;
|
2015-04-09 14:23:47 +00:00
|
|
|
}
|
|
|
|
|