OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ZMQClient.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/ZMQClient.cc
3  *
4  * Copyright (C) 2008-2010 OpenCog Foundation
5  * All Rights Reserved
6  *
7  * Written by Erwin Joosten
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License v3 as
11  * published by the Free Software Foundation and including the exceptions
12  * at http://opencog.org/wiki/Licenses
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program; if not, write to:
21  * Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #include "ZMQClient.h"
26 #include <lib/zmq/zmq.hpp>
27 #include <opencog/atomspace/ZMQMessages.pb.h>
28 
29 using namespace opencog;
30 
31 ZMQClient::ZMQClient(string networkAddress)
32 {
33  zmqContext= new zmq::context_t(1);
34  zmqClientSocket = new zmq::socket_t(*zmqContext, ZMQ_REQ);
35  zmqClientSocket->connect (networkAddress.c_str());
36 }
37 
39 {
40  delete zmqContext;
41  delete zmqClientSocket;
42 };
43 
45  ZMQReplyMessage& replyMessage)
46 {
47  // Send request to server
48  string strRequest = requestMessage.SerializeAsString();
49  zmq::message_t request(strRequest.size());
50  memcpy((void *) request.data (), strRequest.c_str(),
51  strRequest.size()); //TODO use copyless init from data
52  zmqClientSocket->send(request);
53 
54  // Wait for reply
55  zmq::message_t reply;
56  zmqClientSocket->recv(&reply);
57  replyMessage.ParseFromArray(reply.data(), reply.size());
58 }
59 
61 {
63  ZMQReplyMessage rep;
64 
66  req.set_handle(h.value());
67  SendMessage(req, rep);
69  return AtomPtr(atom);
70 };
71 
72 
73 
void SendMessage(ZMQRequestMessage &requestMessage, ZMQReplyMessage &replyMessage)
Definition: ZMQClient.cc:44
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
ZMQClient(string networkAddres="tcp://127.0.0.1:5555")
Definition: ZMQClient.cc:31
const ::ZMQAtomMessage & atom() const
zmq::context_t * zmqContext
Definition: ZMQClient.h:45
UUID value(void) const
Definition: Handle.h:85
void set_function(ZMQFunctionType value)
static TruthValue * deserialize(const ZMQSingleTruthValueMessage &singleTruthValueMessage)
AtomPtr getAtom(Handle &h)
Definition: ZMQClient.cc:60
zmq::socket_t * zmqClientSocket
Definition: ZMQClient.h:44
void set_handle(::google::protobuf::uint64 value)