Home › Forum › SOFA › Programming with SOFA › Send component data via zmq socket
Tagged: 64_bits, component, Linux_ubuntu, SOFA_1706, ZMQ
- This topic has 1 reply, 1 voice, and was last updated 6 years, 9 months ago by
bgarcial.
-
AuthorPosts
-
6 December 2017 at 01:09 #10212
bgarcial
BlockedI have my
ZMQServerComponent.h
file in which I declare, theData<double> myparam;
variable:#include <sofa/core/behavior/BaseController.h> #include <zmq.hpp> namespace sofa { namespace component { namespace controller { class ZMQServerComponent : public sofa::core::behavior::BaseController { public: SOFA_CLASS(ZMQServerComponent, sofa::core::behavior::BaseController); Data<double> myparam; ZMQServerComponent (); virtual ~ZMQServerComponent (); void receiveRequests(); void sendReplyToClient(); void init(); }; } } }
In my ZMQServerComponent.cpp I have the goal of send the
Data<double> myparam;
via zmq socket with the objective of send through zeroMQ some data from SOFA.
This I perform in mysendReplyToClient()
function …Then, according to previously mentioned I have the following component:
#include "ZMQServerComponent.h" #include <sofa/core/ObjectFactory.h> #include <zmq.hpp> #include <iostream> using std::cout; using std::endl; using std::string; namespace sofa { namespace component { namespace controller { zmq::context_t context (1); zmq::socket_t socket (context, ZMQ_REP); ZMQServerComponent::ZMQServerComponent() : myparam(initData(&myparam, 0.42, "myparam", "ZeroMq version plugin. ")) {} void ZMQServerComponent::receiveRequests() { cout << "Creating socket zeroMQ ..."<<endl; socket.bind("tcp://*:5555"); zmq::message_t request; // Wait for next request from client socket.recv(&request); string incomingMessage = std::string(static_cast<char *>(request.data()), request.size()); cout << "Incoming message from client" << incomingMessage << endl; // Do some work sleep(1); } // I send data in response to client void ZMQServerComponent::sendReplyToClient() { zmq::message_t reply (30); // Sending the response to client via zmq - Does not works memcpy (reply.data(), "Hello Client, my version is: ", myparam.getValue()); // Show the myparam value. This works cout << "myparam value is:" << myparam.getValue() << endl; //memcpy (reply.data(), "Hello Client", myparam.getValue()); socket.send(reply); } void ZMQServerComponent::init(){ ZMQServerComponent z; z.receiveRequests(); z.sendReplyToClient(); } ZMQServerComponent::~ZMQServerComponent() {} SOFA_DECL_CLASS(ZMQServerComponent) int ZMQServerComponentClass = sofa::core::RegisterObject("This component create a Socket.").add<ZMQServerComponent>(); } } }
In my client, which is a python script, I have:
import zmq context = zmq.Context() # Socket to talk to server print("Connecting to ZMQ server…") socket = context.socket(zmq.REQ) socket.connect("tcp://localhost:5555") socket.send(b" Hello ZMQ Server, What is your version") # Get the reply. message = socket.recv() print("Received reply %s" % message)
When I send the data from client to Server, In my C++ Server I get:
bgarcial@elpug : ~/workspace/sofa/build/v17.06 [0] % ./bin/runSofa [ERROR] [PluginManager] Plugin loading failed (/home/bgarcial/workspace/sofa/build/v17.06/lib/libZeroMq.so): function initExternalModule() not found Creating socket zeroMQ ... Incoming message from client Hello ZMQ Server, What is your version myparam value is:0.42
And in the client I get:
[2] % python client.py Connecting to ZMQ server… Received reply b'\xdc\xd7\x00\xf0\xc3\x7f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' (cnvss_tests) bgarcial@elpug ‹ master ●● › :
Apparently the server response does not arrive or arrive a empty string …
I have some doubts, if I should perform some cast or message conversion (is a
double
data type from server) in my python client or form C++ Server this message does not sending. I think this latest option, may be …In the server side, I can show the
myparam
value but this is are not send to client …Ho to can I response to client with the SOFA component parameter value?
7 December 2017 at 17:36 #10217bgarcial
BlockedI’ve changed the python client to c++ client of this way:
This c++ client is an external application out from SOFA#include <zmq.hpp> #include <string> #include <iostream> #include <time.h> #include <sys/time.h> using std::cout; using std::cin; using std::endl; using std::string; int main () { // Prepare our context and socket std::string message="Toes Server"; zmq::context_t context (1); zmq::socket_t socket (context, ZMQ_REQ); // Llamamos el proceso al cual le queremos medir el tiempo cout << "Conecct to ZMQ Server" << endl; socket.connect ("tcp://localhost:5555"); zmq::message_t request (message.length()); memcpy (request.data (), message.c_str(), message.length()); //std::cout << "Sending " << request.data() << "…" << std::endl; socket.send (request); zmq::message_t reply; socket.recv (&reply); string replyMessage = string(static_cast<char *>(reply.data()), reply.size()); cout << "Received message from server: " + replyMessage << " " << endl; return 0; }
I execute the client after the server and I receive the following output:
bgarcial@elpug ‹ master ●● › : ~/CLionProjects/zeroMQ_C++_Client-Python_Server [0] % ./client_get-time-of-day.out Conecct to ZMQ Server Received message from server: ��y����y����n?� bgarcial@elpug ‹ master ●● › : ~/CLionProjects/zeroMQ_C++_Client-Python_Server
The response from ZMQ server is an strange string or binary. I think that my response is not arrive on the client side or this should be converted, by I unknown how to do it..
-
AuthorPosts
- You must be logged in to reply to this topic.