OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ZMQServer.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/ZMQServer.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 "ZMQServer.h"
26 
27 using namespace opencog;
28 
29 ZMQServer::ZMQServer(AtomSpace* atomSpace1, string networkAdress)
30 {
31  zmqContext= new zmq::context_t(1);
32  atomSpace = atomSpace1;
33  zmqServerThread = boost::thread(boost::bind(&ZMQServer::zmqLoop, this, networkAdress));
34 }
35 
37 {
41 };
42 
43 void ZMQServer::zmqLoop(string networkAddress)
44 {
45  zmq::socket_t zmqServerSocket (*zmqContext, ZMQ_REP);
46  zmqServerSocket.bind (networkAddress.c_str());
47 
48  while (true)
49  {
50  // Wait for next request from client
51  zmq::message_t request;
52  zmqServerSocket.recv (&request);
53  //TODO check for exit signal
54  //TODO check for errors, log and retry?
55  ZMQRequestMessage requestMessage;
56  requestMessage.ParseFromArray(request.data(), request.size());
57 
58  ZMQReplyMessage replyMessage;
59  switch(requestMessage.function())
60  {
61  case ZMQgetAtom:
62  {
63  AtomPtr atom = atomSpace->cloneAtom(
64  Handle(requestMessage.handle()));
66  break;
67  }
68  //TODO add other functions
69  default:
70  assert(!"Invalid ZMQ function");
71  }
72 
73  // Send reply back to client
74  string strReply = replyMessage.SerializeAsString();
75  zmq::message_t reply (strReply.size());
76  memcpy ((void *) reply.data (), strReply.c_str(), strReply.size()); //TODO in place init
77  zmqServerSocket.send (reply);
78  }
79 }
80 
inline::ZMQAtomMessage * mutable_atom()
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
ZMQServer(AtomSpace *atomSpace1, string networkAddress="tcp://*:5555")
Definition: ZMQServer.cc:29
zmq::context_t * zmqContext
Definition: ZMQServer.h:138
void zmqLoop(string networkAddress)
Definition: ZMQServer.cc:43
static void serialize(Atom &atom, ZMQAtomMessage *atomMessage)
AtomSpace * atomSpace
Definition: ZMQServer.h:141
inline::google::protobuf::uint64 handle() const
ZMQFunctionType function() const
boost::thread zmqServerThread
Definition: ZMQServer.h:140