BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
XmlRpc::XmlRpcServer Class Reference

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

#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.
XmlRpcServerMethodfindMethod (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.
Public Member Functions inherited from XmlRpc::XmlRpcSource
 XmlRpcSource (int fd=-1, bool deleteOnClose=false)
virtual ~XmlRpcSource ()
 Destructor.
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 ()

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

bool _introspectionEnabled
XmlRpcDispatch _disp
MethodMap _methods
XmlRpcServerMethod_listMethods
XmlRpcServerMethod_methodHelp

Detailed Description

A class to handle XML RPC requests.

Definition at line 31 of file XmlRpcServer.h.

Member Typedef Documentation

◆ MethodMap

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

Definition at line 91 of file XmlRpcServer.h.

Constructor & Destructor Documentation

◆ XmlRpcServer()

XmlRpcServer::XmlRpcServer ( )

Create a server object.

Definition at line 11 of file XmlRpcServer.cpp.

11 {
13 _listMethods = 0;
14 _methodHelp = 0;
15}
XmlRpcServerMethod * _listMethods
XmlRpcServerMethod * _methodHelp

◆ ~XmlRpcServer()

XmlRpcServer::~XmlRpcServer ( )
virtual

Destructor.

Definition at line 17 of file XmlRpcServer.cpp.

17 {
18 this->shutdown();
19 _methods.clear();
20 delete _listMethods;
21 delete _methodHelp;
22}
void shutdown()
Close all connections with clients and the socket file descriptor.

Member Function Documentation

◆ acceptConnection()

void XmlRpcServer::acceptConnection ( )
protectedvirtual

Accept a client connection request.

Definition at line 124 of file XmlRpcServer.cpp.

124 {
125 int s = XmlRpcSocket::accept( this->getfd() );
126 XmlRpcUtil::log( 2, "XmlRpcServer::acceptConnection: socket %d", s );
127 if ( s < 0 )
128 {
129 // this->close();
130 XmlRpcUtil::error( "XmlRpcServer::acceptConnection: Could not accept connection (%s).",
132 }
133 else if ( !XmlRpcSocket::setNonBlocking( s ) )
134 {
136 XmlRpcUtil::error( "XmlRpcServer::acceptConnection: Could not set socket to non-blocking "
137 "input mode (%s).",
139 }
140 else // Notify the dispatcher to listen for input on this source when we are in work()
141 {
142 XmlRpcUtil::log( 2, "XmlRpcServer::acceptConnection: creating a connection" );
144 }
145}
XmlRpcServer s
@ ReadableEvent
data available to read
virtual XmlRpcServerConnection * createConnection(int socket)
Create a new connection object for processing requests from a specific client.
XmlRpcDispatch _disp
static int accept(int socket)
Accept a client connection request.
static bool setNonBlocking(int socket)
Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure.
static void close(int socket)
Closes a socket.
static std::string getErrorMsg()
Returns message corresponding to last error.
int getfd() const
Return the file descriptor being monitored.
static void error(const char *fmt,...)
Dump error messages somewhere.
static void log(int level, const char *fmt,...)
Dump messages somewhere.
char * c_str(Index i)

Referenced by handleEvent().

◆ addMethod()

void XmlRpcServer::addMethod ( XmlRpcServerMethod * method)

Add a command to the RPC server.

Definition at line 25 of file XmlRpcServer.cpp.

25 {
26 _methods[method->name()] = method;
27}
std::string & name()
Returns the name of the method.

Referenced by enableIntrospection().

◆ bindAndListen()

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 50 of file XmlRpcServer.cpp.

50 {
51 int fd = XmlRpcSocket::socket();
52 if ( fd < 0 )
53 {
54 XmlRpcUtil::error( "XmlRpcServer::bindAndListen: Could not create socket (%s).",
56 return false;
57 }
58
59 this->setfd( fd );
60
61 // Don't block on reads/writes
63 {
64 this->close();
66 "XmlRpcServer::bindAndListen: Could not set socket to non-blocking input mode (%s).",
68 return false;
69 }
70
71 // Allow this port to be re-bound immediately so server re-starts are not delayed
72 if ( !XmlRpcSocket::setReuseAddr( fd ) )
73 {
74 this->close();
76 "XmlRpcServer::bindAndListen: Could not set SO_REUSEADDR socket option (%s).",
78 return false;
79 }
80
81 // Bind to the specified port on the default interface
82 if ( !XmlRpcSocket::bind( fd, port ) )
83 {
84 this->close();
85 XmlRpcUtil::error( "XmlRpcServer::bindAndListen: Could not bind to specified port (%s).",
87 return false;
88 }
89
90 // Set in listening mode
91 if ( !XmlRpcSocket::listen( fd, backlog ) )
92 {
93 this->close();
95 "XmlRpcServer::bindAndListen: Could not set socket in listening mode (%s).",
97 return false;
98 }
99
100 XmlRpcUtil::log( 2, "XmlRpcServer::bindAndListen: server listening on port %d fd %d", port,
101 fd );
102
103 // Notify the dispatcher to listen on this source when we are in work()
104 _disp.addSource( this, XmlRpcDispatch::ReadableEvent );
105
106 return true;
107}
static int socket()
Creates a stream (TCP) socket. Returns -1 on failure.
static bool listen(int socket, int backlog)
Set socket in listen mode.
static bool setReuseAddr(int socket)
static bool bind(int socket, int port)
Bind to a specified port.
virtual void close()
void setfd(int fd)
Specify the file descriptor to monitor.

◆ createConnection()

XmlRpcServerConnection * XmlRpcServer::createConnection ( int socket)
protectedvirtual

Create a new connection object for processing requests from a specific client.

Definition at line 148 of file XmlRpcServer.cpp.

148 {
149 // Specify that the connection object be deleted when it is closed
150 return new XmlRpcServerConnection( s, this, true );
151}

Referenced by acceptConnection().

◆ enableIntrospection()

void XmlRpcServer::enableIntrospection ( bool enabled = true)

Specify whether introspection is enabled or not. Default is not enabled.

Definition at line 200 of file XmlRpcServer.cpp.

200 {
201 if ( _introspectionEnabled == enabled ) return;
202
203 _introspectionEnabled = enabled;
204
205 if ( enabled )
206 {
207 if ( !_listMethods )
208 {
209 _listMethods = new ListMethods( this );
210 _methodHelp = new MethodHelp( this );
211 }
212 else
213 {
216 }
217 }
218 else
219 {
220 removeMethod( LIST_METHODS );
221 removeMethod( METHOD_HELP );
222 }
223}
void removeMethod(XmlRpcServerMethod *method)
Remove a command from the RPC server.
void addMethod(XmlRpcServerMethod *method)
Add a command to the RPC server.

◆ exit()

void XmlRpcServer::exit ( )

Temporarily stop processing client requests and exit the work() method.

Definition at line 156 of file XmlRpcServer.cpp.

156{ _disp.exit(); }

◆ findMethod()

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

Look up a method by name.

Definition at line 42 of file XmlRpcServer.cpp.

42 {
43 MethodMap::const_iterator i = _methods.find( name );
44 if ( i == _methods.end() ) return 0;
45 return i->second;
46}

◆ handleEvent()

unsigned XmlRpcServer::handleEvent ( unsigned eventType)
virtual

Handle client connection requests.

Implements XmlRpc::XmlRpcSource.

Definition at line 117 of file XmlRpcServer.cpp.

117 {
119 return XmlRpcDispatch::ReadableEvent; // Continue to monitor this fd
120}
virtual void acceptConnection()
Accept a client connection request.

◆ listMethods()

void XmlRpcServer::listMethods ( XmlRpcValue & result)

Introspection support.

Definition at line 225 of file XmlRpcServer.cpp.

225 {
226 int i = 0;
227 result.setSize( _methods.size() + 1 );
228 for ( MethodMap::iterator it = _methods.begin(); it != _methods.end(); ++it )
229 result[i++] = it->first;
230
231 // Multicall support is built into XmlRpcServerConnection
232 result[i] = MULTICALL;
233}
void setSize(int size)
Specify the size for array values. Array values will grow beyond this size if needed.

◆ removeConnection()

void XmlRpcServer::removeConnection ( XmlRpcServerConnection * sc)
virtual

Remove a connection from the dispatcher.

Definition at line 153 of file XmlRpcServer.cpp.

153{ _disp.removeSource( sc ); }

◆ removeMethod() [1/2]

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

Remove a command from the RPC server by name.

Definition at line 36 of file XmlRpcServer.cpp.

36 {
37 MethodMap::iterator i = _methods.find( methodName );
38 if ( i != _methods.end() ) _methods.erase( i );
39}

◆ removeMethod() [2/2]

void XmlRpcServer::removeMethod ( XmlRpcServerMethod * method)

Remove a command from the RPC server.

Definition at line 30 of file XmlRpcServer.cpp.

30 {
31 MethodMap::iterator i = _methods.find( method->name() );
32 if ( i != _methods.end() ) _methods.erase( i );
33}

Referenced by enableIntrospection().

◆ shutdown()

void XmlRpcServer::shutdown ( )

Close all connections with clients and the socket file descriptor.

Definition at line 159 of file XmlRpcServer.cpp.

159 {
160 // This closes and destroys all connections as well as closing this socket
161 _disp.clear();
162}

Referenced by ~XmlRpcServer().

◆ work()

void XmlRpcServer::work ( double msTime)

Process client requests for the specified time.

Definition at line 110 of file XmlRpcServer.cpp.

110 {
111 XmlRpcUtil::log( 2, "XmlRpcServer::work: waiting for a connection" );
112 _disp.work( msTime );
113}

Member Data Documentation

◆ _disp

XmlRpcDispatch XmlRpc::XmlRpcServer::_disp
protected

Definition at line 88 of file XmlRpcServer.h.

Referenced by acceptConnection(), bindAndListen(), exit(), removeConnection(), shutdown(), and work().

◆ _introspectionEnabled

bool XmlRpc::XmlRpcServer::_introspectionEnabled
protected

Definition at line 85 of file XmlRpcServer.h.

Referenced by enableIntrospection(), and XmlRpcServer().

◆ _listMethods

XmlRpcServerMethod* XmlRpc::XmlRpcServer::_listMethods
protected

Definition at line 95 of file XmlRpcServer.h.

Referenced by enableIntrospection(), XmlRpcServer(), and ~XmlRpcServer().

◆ _methodHelp

XmlRpcServerMethod* XmlRpc::XmlRpcServer::_methodHelp
protected

Definition at line 96 of file XmlRpcServer.h.

Referenced by enableIntrospection(), XmlRpcServer(), and ~XmlRpcServer().

◆ _methods

MethodMap XmlRpc::XmlRpcServer::_methods
protected

The documentation for this class was generated from the following files: