Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

XmlRpc::XmlRpcServer Class Reference

A class to handle XML RPC requests. More...

#include <XmlRpcServer.h>

Inheritance diagram for XmlRpc::XmlRpcServer:

XmlRpc::XmlRpcSource List of all members.

Public Member Functions

void addMethod (XmlRpcServerMethod *method)
 Add a command to the RPC server.
bool bindAndListen (int port, int backlog=5)
virtual void close ()
 Close the owned fd. If deleteOnClose was specified at construction, the object is deleted.
void enableIntrospection (bool enabled=true)
 Specify whether introspection is enabled or not. Default is not enabled.
void exit ()
 Temporarily stop processing client requests and exit the work() method.
XmlRpcServerMethodfindMethod (const std::string &name) const
 Look up a method by name.
int getfd () const
 Return the file descriptor being monitored.
bool getKeepOpen () const
 Return whether the file descriptor should be kept open if it is no longer monitored.
virtual unsigned handleEvent (unsigned eventType)
 Handle client connection requests.
void listMethods (XmlRpcValue &result)
 Introspection support.
virtual void removeConnection (XmlRpcServerConnection *)
 Remove a connection from the dispatcher.
void removeMethod (const std::string &methodName)
 Remove a command from the RPC server by name.
void removeMethod (XmlRpcServerMethod *method)
 Remove a command from the RPC server.
void setfd (int fd)
 Specify the file descriptor to monitor.
void setKeepOpen (bool b=true)
 Specify whether the file descriptor should be kept open if it is no longer monitored.
void shutdown ()
 Close all connections with clients and the socket file descriptor.
void work (double msTime)
 Process client requests for the specified time.
 XmlRpcServer ()
 Create a server object.
virtual ~XmlRpcServer ()
 Destructor.

Protected Types

typedef std::map< std::string,
XmlRpcServerMethod * > 
MethodMap

Protected Member Functions

virtual void acceptConnection ()
 Accept a client connection request.
virtual XmlRpcServerConnectioncreateConnection (int socket)
 Create a new connection object for processing requests from a specific client.

Protected Attributes

XmlRpcDispatch _disp
bool _introspectionEnabled
XmlRpcServerMethod_listMethods
XmlRpcServerMethod_methodHelp
MethodMap _methods

Detailed Description

A class to handle XML RPC requests.


Member Typedef Documentation

typedef std::map< std::string, XmlRpcServerMethod* > XmlRpc::XmlRpcServer::MethodMap [protected]
 


Constructor & Destructor Documentation

XmlRpcServer::XmlRpcServer  ) 
 

Create a server object.

00014 {
00015   _introspectionEnabled = false;
00016   _listMethods = 0;
00017   _methodHelp = 0;
00018 }

XmlRpcServer::~XmlRpcServer  )  [virtual]
 

Destructor.

00022 {
00023   this->shutdown();
00024   _methods.clear();
00025   delete _listMethods;
00026   delete _methodHelp;
00027 }


Member Function Documentation

void XmlRpcServer::acceptConnection  )  [protected, virtual]
 

Accept a client connection request.

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.

00033 {
00034   _methods[method->name()] = method;
00035 }

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.

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.

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.

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.

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.

00186 {
00187   _disp.exit();
00188 }

XmlRpcServerMethod * XmlRpcServer::findMethod const std::string &  name  )  const
 

Look up a method by name.

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.

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.

00030 { return _keepOpen; }

unsigned XmlRpcServer::handleEvent unsigned  eventType  )  [virtual]
 

Handle client connection requests.

Implements XmlRpc::XmlRpcSource.

00136 {
00137   acceptConnection();
00138   return XmlRpcDispatch::ReadableEvent;         // Continue to monitor this fd
00139 }

void XmlRpcServer::listMethods XmlRpcValue result  ) 
 

Introspection support.

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.

00178 {
00179   _disp.removeSource(sc);
00180 }

void XmlRpcServer::removeMethod const std::string &  methodName  ) 
 

Remove a command from the RPC server by name.

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.

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.

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.

00032 { _keepOpen = b; }

void XmlRpcServer::shutdown  ) 
 

Close all connections with clients and the socket file descriptor.

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.

00125 {
00126   XmlRpcUtil::log(2, "XmlRpcServer::work: waiting for a connection");
00127   _disp.work(msTime);
00128 }


Member Data Documentation

XmlRpcDispatch XmlRpc::XmlRpcServer::_disp [protected]
 

bool XmlRpc::XmlRpcServer::_introspectionEnabled [protected]
 

XmlRpcServerMethod* XmlRpc::XmlRpcServer::_listMethods [protected]
 

XmlRpcServerMethod* XmlRpc::XmlRpcServer::_methodHelp [protected]
 

MethodMap XmlRpc::XmlRpcServer::_methods [protected]
 


The documentation for this class was generated from the following files:
Generated on Wed Feb 2 19:25:26 2011 for BOSS6.5.5 by  doxygen 1.3.9.1