OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ZMQServer.h
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/ZMQServer.h
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 #ifndef _OPENCOG_ZMQ_SERVER_H
26 #define _OPENCOG_ZMQ_SERVER_H
27 
28 #include <boost/thread.hpp>
29 
30 #include "AtomSpace.h"
31 #include "Handle.h"
32 #include "types.h"
33 #include <lib/zmq/zmq.hpp>
34 #include <opencog/atomspace/ZMQMessages.pb.h>
35 #include <string>
36 
37 using namespace std;
38 
39 /*
40 TODO: remove text from this file, as an exact copy is in wiki
41 http://wiki.opencog.org/w/ZeroMQ
42 
43 Setup:
44  Install zeroMQ (should already be installed)
45  http://www.zeromq.org/intro:get-the-software
46  eclipse: add zmq to libraries
47  Install protocol buffers (should already be installed)
48  http://code.google.com/p/protobuf/downloads/list
49  ./configure
50  make
51  sudo make install
52  eclipse: add protobuf to libraries
53  Compile proto file (if changed)
54  protoc --cpp_out=. AtomSpaceMessages.proto
55 
56 How to compile:
57  Add new files and libs to opencog/atomspace/CMakeLists.txt
58  add ZMQMessages.pb.cc, ZMQServer.cc, ZMQClient.cc, ProtocolBufferSerializer.cc
59  to ADD_LIBRARY (atomspace SHARED
60  add protobuf to SET(ATOMSPACE_LINK_LIBS
61  add ZMQMessages.pb.h, ZMQServer.h, ZMQClient.h, ProtocolBufferSerializer.h
62  to INSTALL (FILES
63  run cmake .. in bin folder
64  Add -DZMQ_EXPERIMENT to CXX_DEFINES in the flags.make file in the following folders:
65  /home/erwin/ochack/bin/opencog/atomspace/CMakeFiles/atomspace.dir
66  /home/erwin/ochack/bin/opencog/server/CMakeFiles/cogserver.dir
67  /home/erwin/ochack/bin/opencog/server/CMakeFiles/server.dir
68 
69 How to use:
70  Link with AtomSpace
71  Create a ZMQClient instance and call its methods
72  ZMQClient* zmqClient = new ZMQClient(); //defaults to localhost
73  zmqClient->getAtom(h);
74  If the cogserver is running on a different PC
75  ZMQClient* zmqClient = new ZMQClient("tcp://168.0.0.21:5555");
76 
77 How it works:
78  ZeroMQ allows you to connect a client process to the atomspace managed by
79  the server (normally the cogserver). Use cases include deploying an agent
80  on a separate server, easier debugging of agents, connecting components
81  that are written in different languages (any language that supports
82  protocolbuffers (see http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns)
83  can use a simple message based wrapper to talk to the server. Of course
84  implementing the full atomspace OO interface takes a lot more work. )
85  and connecting tools that need high performance access to the atomspace
86  (e.g. the atomspace visualizer).
87  You can connect multiple clients to one server. The clients can be anywhere
88  in the world as long as the server is reachable via TCP/IP.
89 
90  Server:
91  if server is enabled run zmqloop
92  zmqloop
93  check for RequestMessage
94  switch(function)
95  case getAtom:
96  call getAtom with arguments from RequestMessage
97  store return value in ReplyMessage
98  case getName:
99  etc...
100  send ReplyMessage
101  Client:
102  async queue works the same as usual but if client is enabled call server instead of Run
103  copy ASRequest arguments and function number to RequestMessage
104  send RequestMessage to server
105  wait for ReplyMessage
106  copy ReplyMessage to ASRequest
107 
108 Problem solving:
109  It doesn't matter if you start the server or client first but if the server
110  crashes or is restarted you have to restart the client(s) as well.
111  If you get error "Address already in use" then another instance of the
112  server is already running
113 
114 TODO:
115  -febcorpus.scm doesn't load
116  -clean exit of atomspaceasync without using sleep or a busy loop
117  send ctrl+C signal to the thread and break when NULL message received
118  char *reply = zstr_recv (client);
119  if (!reply)
120  if(exitSeverloop)
121  logger.info "Serverloop exitting"
122  break;
123  else
124  logger.error "Serverloop quit unexpectedly"
125  -what if dowork throws an exception on the server? catch exceptions, put them in reply message and rethrow at client
126  -try to handle communications exceptions locally
127  */
128 
129 
130 class AtomSpaceAsyncUTest;
131 
132 namespace opencog {
137 class ZMQServer {
138  zmq::context_t* zmqContext;
139  zmq::socket_t *zmqClientSocket;
140  boost::thread zmqServerThread;
142 
143  void zmqLoop(string networkAddress);
144 
145 public:
146  ZMQServer(AtomSpace* atomSpace1,
147  string networkAddress = "tcp://*:5555"); //"ipc:///tmp/AtomSpaceZMQ.ipc"
148  ~ZMQServer();
149 };
150 
152 } // namespace opencog
153 
154 #endif // _OPENCOG_ZMQ_SERVER_H
zmq::socket_t * zmqClientSocket
Definition: ZMQServer.h:139
zmq::context_t * zmqContext
Definition: ZMQServer.h:138
AtomSpace * atomSpace
Definition: ZMQServer.h:141
boost::thread zmqServerThread
Definition: ZMQServer.h:140