CGEM BOSS 6.6.5.g
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 ()
 Close the owned fd. If deleteOnClose was specified at construction, the object is deleted.
 
virtual unsigned handleEvent (unsigned eventType)=0
 Return true to continue monitoring this source.
 

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 33 of file XmlRpcServer.h.

Member Typedef Documentation

◆ MethodMap

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

Definition at line 94 of file XmlRpcServer.h.

Constructor & Destructor Documentation

◆ XmlRpcServer()

XmlRpcServer::XmlRpcServer ( )

Create a server object.

Definition at line 13 of file XmlRpcServer.cpp.

14{
16 _listMethods = 0;
17 _methodHelp = 0;
18}
XmlRpcServerMethod * _listMethods
Definition: XmlRpcServer.h:98
XmlRpcServerMethod * _methodHelp
Definition: XmlRpcServer.h:99

◆ ~XmlRpcServer()

XmlRpcServer::~XmlRpcServer ( )
virtual

Destructor.

Definition at line 21 of file XmlRpcServer.cpp.

22{
23 this->shutdown();
24 _methods.clear();
25 delete _listMethods;
26 delete _methodHelp;
27}
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 145 of file XmlRpcServer.cpp.

146{
147 int s = XmlRpcSocket::accept(this->getfd());
148 XmlRpcUtil::log(2, "XmlRpcServer::acceptConnection: socket %d", s);
149 if (s < 0)
150 {
151 //this->close();
152 XmlRpcUtil::error("XmlRpcServer::acceptConnection: Could not accept connection (%s).", XmlRpcSocket::getErrorMsg().c_str());
153 }
154 else if ( ! XmlRpcSocket::setNonBlocking(s))
155 {
157 XmlRpcUtil::error("XmlRpcServer::acceptConnection: Could not set socket to non-blocking input mode (%s).", XmlRpcSocket::getErrorMsg().c_str());
158 }
159 else // Notify the dispatcher to listen for input on this source when we are in work()
160 {
161 XmlRpcUtil::log(2, "XmlRpcServer::acceptConnection: creating a connection");
163 }
164}
XmlRpcServer s
Definition: HelloServer.cpp:11
@ ReadableEvent
data available to read
void addSource(XmlRpcSource *source, unsigned eventMask)
virtual XmlRpcServerConnection * createConnection(int socket)
Create a new connection object for processing requests from a specific client.
XmlRpcDispatch _disp
Definition: XmlRpcServer.h:91
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.
Definition: XmlRpcSource.h:25
static void error(const char *fmt,...)
Dump error messages somewhere.
Definition: XmlRpcUtil.cpp:85
static void log(int level, const char *fmt,...)
Dump messages somewhere.
Definition: XmlRpcUtil.cpp:71
char * c_str(Index i)
Definition: EvtCyclic3.cc:252

Referenced by handleEvent().

◆ addMethod()

void XmlRpcServer::addMethod ( XmlRpcServerMethod method)

Add a command to the RPC server.

Definition at line 32 of file XmlRpcServer.cpp.

33{
34 _methods[method->name()] = method;
35}
std::string & name()
Returns the name of the method.

Referenced by enableIntrospection(), and XmlRpc::XmlRpcServerMethod::XmlRpcServerMethod().

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

71{
72 int fd = XmlRpcSocket::socket();
73 if (fd < 0)
74 {
75 XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not create socket (%s).", XmlRpcSocket::getErrorMsg().c_str());
76 return false;
77 }
78
79 this->setfd(fd);
80
81 // Don't block on reads/writes
83 {
84 this->close();
85 XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not set socket to non-blocking input mode (%s).", XmlRpcSocket::getErrorMsg().c_str());
86 return false;
87 }
88
89 // Allow this port to be re-bound immediately so server re-starts are not delayed
91 {
92 this->close();
93 XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not set SO_REUSEADDR socket option (%s).", XmlRpcSocket::getErrorMsg().c_str());
94 return false;
95 }
96
97 // Bind to the specified port on the default interface
98 if ( ! XmlRpcSocket::bind(fd, port))
99 {
100 this->close();
101 XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not bind to specified port (%s).", XmlRpcSocket::getErrorMsg().c_str());
102 return false;
103 }
104
105 // Set in listening mode
106 if ( ! XmlRpcSocket::listen(fd, backlog))
107 {
108 this->close();
109 XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not set socket in listening mode (%s).", XmlRpcSocket::getErrorMsg().c_str());
110 return false;
111 }
112
113 XmlRpcUtil::log(2, "XmlRpcServer::bindAndListen: server listening on port %d fd %d", port, fd);
114
115 // Notify the dispatcher to listen on this source when we are in work()
117
118 return true;
119}
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()
Close the owned fd. If deleteOnClose was specified at construction, the object is deleted.
void setfd(int fd)
Specify the file descriptor to monitor.
Definition: XmlRpcSource.h:27

Referenced by execute(), and main().

◆ createConnection()

XmlRpcServerConnection * XmlRpcServer::createConnection ( int  socket)
protectedvirtual

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

Definition at line 169 of file XmlRpcServer.cpp.

170{
171 // Specify that the connection object be deleted when it is closed
172 return new XmlRpcServerConnection(s, this, true);
173}
A class to handle XML RPC requests from a particular client.

Referenced by acceptConnection().

◆ enableIntrospection()

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.

246{
247 if (_introspectionEnabled == enabled)
248 return;
249
250 _introspectionEnabled = enabled;
251
252 if (enabled)
253 {
254 if ( ! _listMethods)
255 {
256 _listMethods = new ListMethods(this);
257 _methodHelp = new MethodHelp(this);
258 } else {
261 }
262 }
263 else
264 {
265 removeMethod(LIST_METHODS);
266 removeMethod(METHOD_HELP);
267 }
268}
void removeMethod(XmlRpcServerMethod *method)
Remove a command from the RPC server.
void addMethod(XmlRpcServerMethod *method)
Add a command to the RPC server.

Referenced by main().

◆ exit()

void XmlRpcServer::exit ( )

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

Definition at line 185 of file XmlRpcServer.cpp.

186{
187 _disp.exit();
188}
void exit()
Exit from work routine.

◆ findMethod()

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

Look up a method by name.

Definition at line 58 of file XmlRpcServer.cpp.

59{
60 MethodMap::const_iterator i = _methods.find(name);
61 if (i == _methods.end())
62 return 0;
63 return i->second;
64}

Referenced by XmlRpc::XmlRpcServerConnection::executeMethod().

◆ handleEvent()

unsigned XmlRpcServer::handleEvent ( unsigned  eventType)
virtual

Handle client connection requests.

Implements XmlRpc::XmlRpcSource.

Definition at line 135 of file XmlRpcServer.cpp.

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

◆ listMethods()

void XmlRpcServer::listMethods ( XmlRpcValue result)

Introspection support.

Definition at line 272 of file XmlRpcServer.cpp.

273{
274 int i = 0;
275 result.setSize(_methods.size()+1);
276 for (MethodMap::iterator it=_methods.begin(); it != _methods.end(); ++it)
277 result[i++] = it->first;
278
279 // Multicall support is built into XmlRpcServerConnection
280 result[i] = MULTICALL;
281}
void setSize(int size)
Specify the size for array values. Array values will grow beyond this size if needed.
Definition: XmlRpcValue.h:111

◆ removeConnection()

void XmlRpcServer::removeConnection ( XmlRpcServerConnection sc)
virtual

Remove a connection from the dispatcher.

Definition at line 177 of file XmlRpcServer.cpp.

178{
180}
void removeSource(XmlRpcSource *source)

Referenced by XmlRpc::XmlRpcServerConnection::~XmlRpcServerConnection().

◆ removeMethod() [1/2]

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

Remove a command from the RPC server by name.

Definition at line 48 of file XmlRpcServer.cpp.

49{
50 MethodMap::iterator i = _methods.find(methodName);
51 if (i != _methods.end())
52 _methods.erase(i);
53}

◆ removeMethod() [2/2]

void XmlRpcServer::removeMethod ( XmlRpcServerMethod method)

Remove a command from the RPC server.

Definition at line 39 of file XmlRpcServer.cpp.

40{
41 MethodMap::iterator i = _methods.find(method->name());
42 if (i != _methods.end())
43 _methods.erase(i);
44}

Referenced by enableIntrospection(), and XmlRpc::XmlRpcServerMethod::~XmlRpcServerMethod().

◆ shutdown()

void XmlRpcServer::shutdown ( )

Close all connections with clients and the socket file descriptor.

Definition at line 193 of file XmlRpcServer.cpp.

194{
195 // This closes and destroys all connections as well as closing this socket
196 _disp.clear();
197}
void clear()
Clear all sources from the monitored sources list. Sources are closed.

Referenced by ~XmlRpcServer().

◆ work()

void XmlRpcServer::work ( double  msTime)

Process client requests for the specified time.

Definition at line 124 of file XmlRpcServer.cpp.

125{
126 XmlRpcUtil::log(2, "XmlRpcServer::work: waiting for a connection");
127 _disp.work(msTime);
128}
void work(double msTime)

Referenced by execute(), and main().

Member Data Documentation

◆ _disp

XmlRpcDispatch XmlRpc::XmlRpcServer::_disp
protected

Definition at line 91 of file XmlRpcServer.h.

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

◆ _introspectionEnabled

bool XmlRpc::XmlRpcServer::_introspectionEnabled
protected

Definition at line 88 of file XmlRpcServer.h.

Referenced by enableIntrospection(), and XmlRpcServer().

◆ _listMethods

XmlRpcServerMethod* XmlRpc::XmlRpcServer::_listMethods
protected

Definition at line 98 of file XmlRpcServer.h.

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

◆ _methodHelp

XmlRpcServerMethod* XmlRpc::XmlRpcServer::_methodHelp
protected

Definition at line 99 of file XmlRpcServer.h.

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

◆ _methods

MethodMap XmlRpc::XmlRpcServer::_methods
protected

Definition at line 95 of file XmlRpcServer.h.

Referenced by addMethod(), findMethod(), listMethods(), removeMethod(), and ~XmlRpcServer().


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