Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @brief Standard system include files and namespaces
- */
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sys/sysinfo.h>
- #include <unistd.h>
- using namespace std;
- /**
- * @brief Project libraries headers and namespaces
- */
- #include <google/protobuf/message_lite.h>
- #include <zmq.hpp>
- /**
- * @brief Project include files and namespaces
- */
- #include "rpi_protocol.pb.h"
- static void DispatchMessage(RPiProtocol::Message rpi_msg)
- {
- std::cout << "type= " << rpi_msg.type() << std::endl;
- std::cout << "command= " << rpi_msg.command() << std::endl;
- std::cout << "version= " << rpi_msg.version() << std::endl;
- if (rpi_msg.has_correlator())
- {
- std::cout << "correlator= " << rpi_msg.correlator() << std::endl;
- }
- // Handle RPi Protocol request
- if (rpi_msg.type() == RPiProtocol::Message::REQUEST)
- {
- cout << "REQUEST received." << endl;
- switch (rpi_msg.command())
- {
- case RPiProtocol::Message::GET_SYS_INFO:
- cout << "GET_SYS_INFO received." << endl;
- break;
- default:
- cout << "Command not supported." << endl;
- break;
- }
- }
- // Handle RPi Protocol response
- else if (rpi_msg.type() == RPiProtocol::Message::RESPONSE)
- {
- cout << "RESPONSE received." << endl;
- }
- // Unsupported RPi Protocol message type received
- else
- {
- cout << "Unsupported message type received." << endl;
- }
- }
- static void HandleMessage(zmq::message_t &message)
- {
- RPiProtocol::Message rpi_msg;
- std::string rpi_msg_str;
- std::cout << "New message received" << std::endl;
- // Convert ZMQ message to string.
- rpi_msg_str = std::string(static_cast<char*>(message.data()), message.size());
- // Deserialize RPi Protocol message from string.
- rpi_msg.ParseFromString(rpi_msg_str);
- // Dispatch received RPi Protocol message
- DispatchMessage(rpi_msg);
- }
- int main(int argc, char *argv[])
- {
- // Verify that the version of the library that we linked against is
- // compatible with the version of the headers we compiled against.
- GOOGLE_PROTOBUF_VERIFY_VERSION;
- // Prepare our context and socket.
- zmq::context_t context(1);
- zmq::socket_t socket(context, ZMQ_REP);
- socket.bind("tcp://*:5555");
- // Server thread.
- while (true) {
- zmq::message_t request;
- // Wait for next request from client.
- socket.recv(&request);
- // Handle ZMQ message.
- HandleMessage(request);
- #if 1
- // Test Code.
- // Try to send some 'demo' response back
- RPiProtocol::Message response;
- std::string response_string;
- response.set_type(RPiProtocol::Message::RESPONSE);
- response.set_command(RPiProtocol::Message::GET_SYS_INFO);
- response.set_version(0);
- // Serialize ZMQ message to string.
- if (response.SerializeToString(&response_string))
- {
- // Debug prints.
- printf("%#010x\n", response_string.c_str());
- cout << "Response string length= " << response_string.length() << endl;
- // Send response message back to the client.
- zmq::message_t reply(response_string.length());
- memcpy((void *)reply.data(), &response_string, response_string.length());
- socket.send(reply);
- }
- #else
- // Do some 'work'
- sleep(1);
- // Send reply back to client
- zmq::message_t reply(5);
- memcpy((void *)reply.data(), "World", 5);
- socket.send(reply);
- #endif
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement