#include <XmlRpcServer.h>
Inheritance diagram for XmlRpc::XmlRpcServer:
Public Member Functions | |
XmlRpcServer () | |
Create a server object. | |
virtual | ~XmlRpcServer () |
Destructor. | |
void | enableIntrospection (bool enabled=true) |
Specify whether introspection is enabled or not. Default is not enabled. | |
void | addMethod (XmlRpcServerMethod *method) |
Add a command to the RPC server. | |
void | removeMethod (XmlRpcServerMethod *method) |
Remove a command from the RPC server. | |
void | removeMethod (const std::string &methodName) |
Remove a command from the RPC server by name. | |
XmlRpcServerMethod * | findMethod (const std::string &name) const |
Look up a method by name. | |
bool | bindAndListen (int port, int backlog=5) |
void | work (double msTime) |
Process client requests for the specified time. | |
void | exit () |
Temporarily stop processing client requests and exit the work() method. | |
void | shutdown () |
Close all connections with clients and the socket file descriptor. | |
void | listMethods (XmlRpcValue &result) |
Introspection support. | |
virtual unsigned | handleEvent (unsigned eventType) |
Handle client connection requests. | |
virtual void | removeConnection (XmlRpcServerConnection *) |
Remove a connection from the dispatcher. | |
int | getfd () const |
Return the file descriptor being monitored. | |
void | setfd (int fd) |
Specify the file descriptor to monitor. | |
bool | getKeepOpen () const |
Return whether the file descriptor should be kept open if it is no longer monitored. | |
void | setKeepOpen (bool b=true) |
Specify whether the file descriptor should be kept open if it is no longer monitored. | |
virtual void | close () |
Close the owned fd. If deleteOnClose was specified at construction, the object is deleted. | |
Protected Types | |
typedef std::map< std::string, XmlRpcServerMethod * > | MethodMap |
Protected Member Functions | |
virtual void | acceptConnection () |
Accept a client connection request. | |
virtual XmlRpcServerConnection * | createConnection (int socket) |
Create a new connection object for processing requests from a specific client. | |
Protected Attributes | |
bool | _introspectionEnabled |
XmlRpcDispatch | _disp |
MethodMap | _methods |
XmlRpcServerMethod * | _listMethods |
XmlRpcServerMethod * | _methodHelp |
Definition at line 33 of file XmlRpcServer.h.
typedef std::map< std::string, XmlRpcServerMethod* > XmlRpc::XmlRpcServer::MethodMap [protected] |
Definition at line 94 of file XmlRpcServer.h.
XmlRpcServer::XmlRpcServer | ( | ) |
Create a server object.
Definition at line 13 of file XmlRpcServer.cpp.
References _introspectionEnabled, _listMethods, and _methodHelp.
00014 { 00015 _introspectionEnabled = false; 00016 _listMethods = 0; 00017 _methodHelp = 0; 00018 }
XmlRpcServer::~XmlRpcServer | ( | ) | [virtual] |
Destructor.
Definition at line 21 of file XmlRpcServer.cpp.
References _listMethods, _methodHelp, _methods, and shutdown().
00022 { 00023 this->shutdown(); 00024 _methods.clear(); 00025 delete _listMethods; 00026 delete _methodHelp; 00027 }
void XmlRpcServer::acceptConnection | ( | ) | [protected, virtual] |
Accept a client connection request.
Definition at line 145 of file XmlRpcServer.cpp.
References _disp, XmlRpc::XmlRpcSocket::accept(), XmlRpc::XmlRpcDispatch::addSource(), EvtCyclic3::c_str(), XmlRpc::XmlRpcSocket::close(), ers::error, XmlRpc::XmlRpcSocket::getErrorMsg(), XmlRpc::XmlRpcUtil::log(), XmlRpc::XmlRpcDispatch::ReadableEvent, s, and XmlRpc::XmlRpcSocket::setNonBlocking().
Referenced by handleEvent().
00146 { 00147 int s = XmlRpcSocket::accept(this->getfd()); 00148 XmlRpcUtil::log(2, "XmlRpcServer::acceptConnection: socket %d", s); 00149 if (s < 0) 00150 { 00151 //this->close(); 00152 XmlRpcUtil::error("XmlRpcServer::acceptConnection: Could not accept connection (%s).", XmlRpcSocket::getErrorMsg().c_str()); 00153 } 00154 else if ( ! XmlRpcSocket::setNonBlocking(s)) 00155 { 00156 XmlRpcSocket::close(s); 00157 XmlRpcUtil::error("XmlRpcServer::acceptConnection: Could not set socket to non-blocking input mode (%s).", XmlRpcSocket::getErrorMsg().c_str()); 00158 } 00159 else // Notify the dispatcher to listen for input on this source when we are in work() 00160 { 00161 XmlRpcUtil::log(2, "XmlRpcServer::acceptConnection: creating a connection"); 00162 _disp.addSource(this->createConnection(s), XmlRpcDispatch::ReadableEvent); 00163 } 00164 }
void XmlRpcServer::addMethod | ( | XmlRpcServerMethod * | method | ) |
Add a command to the RPC server.
Definition at line 32 of file XmlRpcServer.cpp.
References _methods, and XmlRpc::XmlRpcServerMethod::name().
Referenced by enableIntrospection(), and XmlRpc::XmlRpcServerMethod::XmlRpcServerMethod().
bool XmlRpcServer::bindAndListen | ( | int | port, | |
int | backlog = 5 | |||
) |
Create a socket, bind to the specified port, and set it in listen mode to make it available for clients.
Definition at line 70 of file XmlRpcServer.cpp.
References _disp, XmlRpc::XmlRpcDispatch::addSource(), XmlRpc::XmlRpcSocket::bind(), EvtCyclic3::c_str(), XmlRpc::XmlRpcSource::close(), ers::error, XmlRpc::XmlRpcSocket::getErrorMsg(), XmlRpc::XmlRpcSocket::listen(), XmlRpc::XmlRpcUtil::log(), XmlRpc::XmlRpcDispatch::ReadableEvent, XmlRpc::XmlRpcSource::setfd(), XmlRpc::XmlRpcSocket::setNonBlocking(), XmlRpc::XmlRpcSocket::setReuseAddr(), and XmlRpc::XmlRpcSocket::socket().
Referenced by main().
00071 { 00072 int fd = XmlRpcSocket::socket(); 00073 if (fd < 0) 00074 { 00075 XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not create socket (%s).", XmlRpcSocket::getErrorMsg().c_str()); 00076 return false; 00077 } 00078 00079 this->setfd(fd); 00080 00081 // Don't block on reads/writes 00082 if ( ! XmlRpcSocket::setNonBlocking(fd)) 00083 { 00084 this->close(); 00085 XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not set socket to non-blocking input mode (%s).", XmlRpcSocket::getErrorMsg().c_str()); 00086 return false; 00087 } 00088 00089 // Allow this port to be re-bound immediately so server re-starts are not delayed 00090 if ( ! XmlRpcSocket::setReuseAddr(fd)) 00091 { 00092 this->close(); 00093 XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not set SO_REUSEADDR socket option (%s).", XmlRpcSocket::getErrorMsg().c_str()); 00094 return false; 00095 } 00096 00097 // Bind to the specified port on the default interface 00098 if ( ! XmlRpcSocket::bind(fd, port)) 00099 { 00100 this->close(); 00101 XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not bind to specified port (%s).", XmlRpcSocket::getErrorMsg().c_str()); 00102 return false; 00103 } 00104 00105 // Set in listening mode 00106 if ( ! XmlRpcSocket::listen(fd, backlog)) 00107 { 00108 this->close(); 00109 XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not set socket in listening mode (%s).", XmlRpcSocket::getErrorMsg().c_str()); 00110 return false; 00111 } 00112 00113 XmlRpcUtil::log(2, "XmlRpcServer::bindAndListen: server listening on port %d fd %d", port, fd); 00114 00115 // Notify the dispatcher to listen on this source when we are in work() 00116 _disp.addSource(this, XmlRpcDispatch::ReadableEvent); 00117 00118 return true; 00119 }
void XmlRpc::XmlRpcSource::close | ( | ) | [virtual, inherited] |
Close the owned fd. If deleteOnClose was specified at construction, the object is deleted.
Reimplemented in XmlRpc::XmlRpcClient.
Definition at line 20 of file XmlRpcSource.cpp.
References XmlRpc::XmlRpcSource::_deleteOnClose, XmlRpc::XmlRpcSource::_fd, XmlRpc::XmlRpcSocket::close(), and XmlRpc::XmlRpcUtil::log().
Referenced by bindAndListen(), XmlRpc::XmlRpcClient::close(), XmlRpc::XmlRpcClient::readHeader(), and XmlRpc::XmlRpcDispatch::work().
00021 { 00022 if (_fd != -1) { 00023 XmlRpcUtil::log(2,"XmlRpcSource::close: closing socket %d.", _fd); 00024 XmlRpcSocket::close(_fd); 00025 XmlRpcUtil::log(2,"XmlRpcSource::close: done closing socket %d.", _fd); 00026 _fd = -1; 00027 } 00028 if (_deleteOnClose) { 00029 XmlRpcUtil::log(2,"XmlRpcSource::close: deleting this"); 00030 _deleteOnClose = false; 00031 delete this; 00032 } 00033 }
XmlRpcServerConnection * XmlRpcServer::createConnection | ( | int | socket | ) | [protected, virtual] |
Create a new connection object for processing requests from a specific client.
Definition at line 169 of file XmlRpcServer.cpp.
00170 { 00171 // Specify that the connection object be deleted when it is closed 00172 return new XmlRpcServerConnection(s, this, true); 00173 }
void XmlRpcServer::enableIntrospection | ( | bool | enabled = true |
) |
Specify whether introspection is enabled or not. Default is not enabled.
Definition at line 245 of file XmlRpcServer.cpp.
References _introspectionEnabled, _listMethods, _methodHelp, addMethod(), LIST_METHODS(), METHOD_HELP(), and removeMethod().
Referenced by main().
00246 { 00247 if (_introspectionEnabled == enabled) 00248 return; 00249 00250 _introspectionEnabled = enabled; 00251 00252 if (enabled) 00253 { 00254 if ( ! _listMethods) 00255 { 00256 _listMethods = new ListMethods(this); 00257 _methodHelp = new MethodHelp(this); 00258 } else { 00259 addMethod(_listMethods); 00260 addMethod(_methodHelp); 00261 } 00262 } 00263 else 00264 { 00265 removeMethod(LIST_METHODS); 00266 removeMethod(METHOD_HELP); 00267 } 00268 }
void XmlRpcServer::exit | ( | ) |
Temporarily stop processing client requests and exit the work() method.
Definition at line 185 of file XmlRpcServer.cpp.
References _disp, and XmlRpc::XmlRpcDispatch::exit().
XmlRpcServerMethod * XmlRpcServer::findMethod | ( | const std::string & | name | ) | const |
Look up a method by name.
Definition at line 58 of file XmlRpcServer.cpp.
References _methods, and genRecEmupikp::i.
Referenced by XmlRpc::XmlRpcServerConnection::executeMethod().
00059 { 00060 MethodMap::const_iterator i = _methods.find(name); 00061 if (i == _methods.end()) 00062 return 0; 00063 return i->second; 00064 }
int XmlRpc::XmlRpcSource::getfd | ( | ) | const [inline, inherited] |
Return the file descriptor being monitored.
Definition at line 25 of file XmlRpcSource.h.
References XmlRpc::XmlRpcSource::_fd.
Referenced by XmlRpc::XmlRpcClient::close(), XmlRpc::XmlRpcClient::readHeader(), and XmlRpc::XmlRpcDispatch::work().
00025 { return _fd; }
bool XmlRpc::XmlRpcSource::getKeepOpen | ( | ) | const [inline, inherited] |
Return whether the file descriptor should be kept open if it is no longer monitored.
Definition at line 30 of file XmlRpcSource.h.
References XmlRpc::XmlRpcSource::_keepOpen.
Referenced by XmlRpc::XmlRpcClient::readHeader(), and XmlRpc::XmlRpcDispatch::work().
00030 { return _keepOpen; }
unsigned XmlRpcServer::handleEvent | ( | unsigned | eventType | ) | [virtual] |
Handle client connection requests.
Implements XmlRpc::XmlRpcSource.
Definition at line 135 of file XmlRpcServer.cpp.
References acceptConnection(), and XmlRpc::XmlRpcDispatch::ReadableEvent.
00136 { 00137 acceptConnection(); 00138 return XmlRpcDispatch::ReadableEvent; // Continue to monitor this fd 00139 }
void XmlRpcServer::listMethods | ( | XmlRpcValue & | result | ) |
Introspection support.
Definition at line 272 of file XmlRpcServer.cpp.
References _methods, genRecEmupikp::i, MULTICALL(), and XmlRpc::XmlRpcValue::setSize().
00273 { 00274 int i = 0; 00275 result.setSize(_methods.size()+1); 00276 for (MethodMap::iterator it=_methods.begin(); it != _methods.end(); ++it) 00277 result[i++] = it->first; 00278 00279 // Multicall support is built into XmlRpcServerConnection 00280 result[i] = MULTICALL; 00281 }
void XmlRpcServer::removeConnection | ( | XmlRpcServerConnection * | ) | [virtual] |
Remove a connection from the dispatcher.
Definition at line 177 of file XmlRpcServer.cpp.
References _disp, and XmlRpc::XmlRpcDispatch::removeSource().
Referenced by XmlRpc::XmlRpcServerConnection::~XmlRpcServerConnection().
00178 { 00179 _disp.removeSource(sc); 00180 }
void XmlRpcServer::removeMethod | ( | const std::string & | methodName | ) |
Remove a command from the RPC server by name.
Definition at line 48 of file XmlRpcServer.cpp.
References _methods, and genRecEmupikp::i.
00049 { 00050 MethodMap::iterator i = _methods.find(methodName); 00051 if (i != _methods.end()) 00052 _methods.erase(i); 00053 }
void XmlRpcServer::removeMethod | ( | XmlRpcServerMethod * | method | ) |
Remove a command from the RPC server.
Definition at line 39 of file XmlRpcServer.cpp.
References _methods, genRecEmupikp::i, and XmlRpc::XmlRpcServerMethod::name().
Referenced by enableIntrospection(), and XmlRpc::XmlRpcServerMethod::~XmlRpcServerMethod().
00040 { 00041 MethodMap::iterator i = _methods.find(method->name()); 00042 if (i != _methods.end()) 00043 _methods.erase(i); 00044 }
void XmlRpc::XmlRpcSource::setfd | ( | int | fd | ) | [inline, inherited] |
Specify the file descriptor to monitor.
Definition at line 27 of file XmlRpcSource.h.
References XmlRpc::XmlRpcSource::_fd.
Referenced by bindAndListen(), and XmlRpc::XmlRpcClient::doConnect().
00027 { _fd = fd; }
void XmlRpc::XmlRpcSource::setKeepOpen | ( | bool | b = true |
) | [inline, inherited] |
Specify whether the file descriptor should be kept open if it is no longer monitored.
Definition at line 32 of file XmlRpcSource.h.
References XmlRpc::XmlRpcSource::_keepOpen.
Referenced by XmlRpc::XmlRpcClient::XmlRpcClient().
00032 { _keepOpen = b; }
void XmlRpcServer::shutdown | ( | ) |
Close all connections with clients and the socket file descriptor.
Definition at line 193 of file XmlRpcServer.cpp.
References _disp, and XmlRpc::XmlRpcDispatch::clear().
Referenced by ~XmlRpcServer().
00194 { 00195 // This closes and destroys all connections as well as closing this socket 00196 _disp.clear(); 00197 }
void XmlRpcServer::work | ( | double | msTime | ) |
Process client requests for the specified time.
Definition at line 124 of file XmlRpcServer.cpp.
References _disp, XmlRpc::XmlRpcUtil::log(), and XmlRpc::XmlRpcDispatch::work().
Referenced by main().
00125 { 00126 XmlRpcUtil::log(2, "XmlRpcServer::work: waiting for a connection"); 00127 _disp.work(msTime); 00128 }
XmlRpcDispatch XmlRpc::XmlRpcServer::_disp [protected] |
Definition at line 91 of file XmlRpcServer.h.
Referenced by acceptConnection(), bindAndListen(), exit(), removeConnection(), shutdown(), and work().
bool XmlRpc::XmlRpcServer::_introspectionEnabled [protected] |
Definition at line 88 of file XmlRpcServer.h.
Referenced by enableIntrospection(), and XmlRpcServer().
XmlRpcServerMethod* XmlRpc::XmlRpcServer::_listMethods [protected] |
Definition at line 98 of file XmlRpcServer.h.
Referenced by enableIntrospection(), XmlRpcServer(), and ~XmlRpcServer().
XmlRpcServerMethod* XmlRpc::XmlRpcServer::_methodHelp [protected] |
Definition at line 99 of file XmlRpcServer.h.
Referenced by enableIntrospection(), XmlRpcServer(), and ~XmlRpcServer().
MethodMap XmlRpc::XmlRpcServer::_methods [protected] |
Definition at line 95 of file XmlRpcServer.h.
Referenced by addMethod(), findMethod(), listMethods(), removeMethod(), and ~XmlRpcServer().