BOSS 6.6.4.p01
BESIII Offline Software System
Loading...
Searching...
No Matches
XmlRpc::XmlRpcDispatch Class Reference

#include <XmlRpcDispatch.h>

Classes

struct  MonitoredSource
 

Public Types

enum  EventType { ReadableEvent = 1 , WritableEvent = 2 , Exception = 4 }
 Values indicating the type of events a source is interested in. More...
 

Public Member Functions

 XmlRpcDispatch ()
 Constructor.
 
 ~XmlRpcDispatch ()
 
void addSource (XmlRpcSource *source, unsigned eventMask)
 
void removeSource (XmlRpcSource *source)
 
void setSourceEvents (XmlRpcSource *source, unsigned eventMask)
 Modify the types of events to watch for on this source.
 
void work (double msTime)
 
void exit ()
 Exit from work routine.
 
void clear ()
 Clear all sources from the monitored sources list. Sources are closed.
 

Protected Types

typedef std::list< MonitoredSourceSourceList
 

Protected Member Functions

double getTime ()
 

Protected Attributes

SourceList _sources
 
double _endTime
 
bool _doClear
 
bool _inWork
 

Detailed Description

An object which monitors file descriptors for events and performs callbacks when interesting events happen.

Definition at line 22 of file XmlRpcDispatch.h.

Member Typedef Documentation

◆ SourceList

typedef std::list< MonitoredSource > XmlRpc::XmlRpcDispatch::SourceList
protected

Definition at line 74 of file XmlRpcDispatch.h.

Member Enumeration Documentation

◆ EventType

Values indicating the type of events a source is interested in.

Enumerator
ReadableEvent 

data available to read

WritableEvent 

connected/data can be written without blocking

Exception 

uh oh

Definition at line 29 of file XmlRpcDispatch.h.

29 {
30 ReadableEvent = 1, //!< data available to read
31 WritableEvent = 2, //!< connected/data can be written without blocking
32 Exception = 4 //!< uh oh
33 };
@ ReadableEvent
data available to read
@ WritableEvent
connected/data can be written without blocking

Constructor & Destructor Documentation

◆ XmlRpcDispatch()

XmlRpcDispatch::XmlRpcDispatch ( )

Constructor.

Definition at line 26 of file XmlRpcDispatch.cpp.

27{
28 _endTime = -1.0;
29 _doClear = false;
30 _inWork = false;
31}

◆ ~XmlRpcDispatch()

XmlRpcDispatch::~XmlRpcDispatch ( )

Definition at line 34 of file XmlRpcDispatch.cpp.

35{
36}

Member Function Documentation

◆ addSource()

void XmlRpcDispatch::addSource ( XmlRpcSource source,
unsigned  eventMask 
)

Monitor this source for the event types specified by the event mask and call its event handler when any of the events occur.

Parameters
sourceThe source to monitor
eventMaskWhich event types to watch for.
See also
EventType

Definition at line 41 of file XmlRpcDispatch.cpp.

42{
43 _sources.push_back(MonitoredSource(source, mask));
44}

Referenced by XmlRpc::XmlRpcServer::acceptConnection(), XmlRpc::XmlRpcServer::bindAndListen(), and XmlRpc::XmlRpcClient::setupConnection().

◆ clear()

void XmlRpcDispatch::clear ( )

Clear all sources from the monitored sources list. Sources are closed.

Definition at line 180 of file XmlRpcDispatch.cpp.

181{
182 if (_inWork)
183 _doClear = true; // Finish reporting current events before clearing
184 else
185 {
186 SourceList closeList = _sources;
187 _sources.clear();
188 for (SourceList::iterator it=closeList.begin(); it!=closeList.end(); ++it)
189 it->getSource()->close();
190 }
191}
std::list< MonitoredSource > SourceList

Referenced by XmlRpc::XmlRpcServer::shutdown().

◆ exit()

void XmlRpcDispatch::exit ( )

Exit from work routine.

Definition at line 173 of file XmlRpcDispatch.cpp.

174{
175 _endTime = 0.0; // Return from work asap
176}

Referenced by XmlRpc::XmlRpcClient::close(), and XmlRpc::XmlRpcServer::exit().

◆ getTime()

double XmlRpcDispatch::getTime ( )
protected

Definition at line 195 of file XmlRpcDispatch.cpp.

196{
197#ifdef USE_FTIME
198 struct timeb tbuff;
199
200 ftime(&tbuff);
201 return ((double) tbuff.time + ((double)tbuff.millitm / 1000.0) +
202 ((double) tbuff.timezone * 60));
203#else
204 struct timeval tv;
205 struct timezone tz;
206
207 gettimeofday(&tv, &tz);
208 return (tv.tv_sec + tv.tv_usec / 1000000.0);
209#endif /* USE_FTIME */
210}

Referenced by work().

◆ removeSource()

void XmlRpcDispatch::removeSource ( XmlRpcSource source)

Stop monitoring this source.

Parameters
sourceThe source to stop monitoring

Definition at line 48 of file XmlRpcDispatch.cpp.

49{
50 for (SourceList::iterator it=_sources.begin(); it!=_sources.end(); ++it)
51 if (it->getSource() == source)
52 {
53 _sources.erase(it);
54 break;
55 }
56}

Referenced by XmlRpc::XmlRpcClient::close(), XmlRpc::XmlRpcServer::removeConnection(), and XmlRpc::XmlRpcClient::setupConnection().

◆ setSourceEvents()

void XmlRpcDispatch::setSourceEvents ( XmlRpcSource source,
unsigned  eventMask 
)

Modify the types of events to watch for on this source.

Definition at line 61 of file XmlRpcDispatch.cpp.

62{
63 for (SourceList::iterator it=_sources.begin(); it!=_sources.end(); ++it)
64 if (it->getSource() == source)
65 {
66 it->getMask() = eventMask;
67 break;
68 }
69}

◆ work()

void XmlRpcDispatch::work ( double  msTime)

Watch current set of sources and process events for the specified duration (in ms, -1 implies wait forever, or until exit is called)

Definition at line 75 of file XmlRpcDispatch.cpp.

76{
77 // Compute end time
78 _endTime = (timeout < 0.0) ? -1.0 : (getTime() + timeout);
79 _doClear = false;
80 _inWork = true;
81
82 // Only work while there is something to monitor
83 while (_sources.size() > 0) {
84
85 // Construct the sets of descriptors we are interested in
86 fd_set inFd, outFd, excFd;
87 FD_ZERO(&inFd);
88 FD_ZERO(&outFd);
89 FD_ZERO(&excFd);
90
91 int maxFd = -1; // Not used on windows
92 SourceList::iterator it;
93 for (it=_sources.begin(); it!=_sources.end(); ++it) {
94 int fd = it->getSource()->getfd();
95 if (it->getMask() & ReadableEvent) FD_SET(fd, &inFd);
96 if (it->getMask() & WritableEvent) FD_SET(fd, &outFd);
97 if (it->getMask() & Exception) FD_SET(fd, &excFd);
98 if (it->getMask() && fd > maxFd) maxFd = fd;
99 }
100
101 // Check for events
102 int nEvents;
103 if (timeout < 0.0)
104 nEvents = select(maxFd+1, &inFd, &outFd, &excFd, NULL);
105 else
106 {
107 struct timeval tv;
108 tv.tv_sec = (int)floor(timeout);
109 tv.tv_usec = ((int)floor(1000000.0 * (timeout-floor(timeout)))) % 1000000;
110 nEvents = select(maxFd+1, &inFd, &outFd, &excFd, &tv);
111 }
112
113 if (nEvents < 0)
114 {
115 XmlRpcUtil::error("Error in XmlRpcDispatch::work: error in select (%d).", nEvents);
116 _inWork = false;
117 return;
118 }
119
120 // Process events
121 for (it=_sources.begin(); it != _sources.end(); )
122 {
123 SourceList::iterator thisIt = it++;
124 XmlRpcSource* src = thisIt->getSource();
125 int fd = src->getfd();
126 unsigned newMask = (unsigned) -1;
127 if (fd <= maxFd) {
128 // If you select on multiple event types this could be ambiguous
129 if (FD_ISSET(fd, &inFd))
130 newMask &= src->handleEvent(ReadableEvent);
131 if (FD_ISSET(fd, &outFd))
132 newMask &= src->handleEvent(WritableEvent);
133 if (FD_ISSET(fd, &excFd))
134 newMask &= src->handleEvent(Exception);
135
136 if ( ! newMask) {
137 _sources.erase(thisIt); // Stop monitoring this one
138 if ( ! src->getKeepOpen())
139 src->close();
140 } else if (newMask != (unsigned) -1) {
141 thisIt->getMask() = newMask;
142 }
143 }
144 }
145
146 // Check whether to clear all sources
147 if (_doClear)
148 {
149 SourceList closeList = _sources;
150 _sources.clear();
151 for (SourceList::iterator it=closeList.begin(); it!=closeList.end(); ++it) {
152 XmlRpcSource *src = it->getSource();
153 src->close();
154 }
155
156 _doClear = false;
157 }
158
159 // Check whether end time has passed
160 if (0 <= _endTime && getTime() > _endTime){
161 std::cout<< "XmlRpc : time out when connect to database " << std::endl;//yzhang debug
162 break;
163 }
164 }
165
166 _inWork = false;
167}
An RPC source represents a file descriptor to monitor.
Definition: XmlRpcSource.h:14
virtual void close()
Close the owned fd. If deleteOnClose was specified at construction, the object is deleted.
int getfd() const
Return the file descriptor being monitored.
Definition: XmlRpcSource.h:25
bool getKeepOpen() const
Return whether the file descriptor should be kept open if it is no longer monitored.
Definition: XmlRpcSource.h:30
virtual unsigned handleEvent(unsigned eventType)=0
Return true to continue monitoring this source.
static void error(const char *fmt,...)
Dump error messages somewhere.
Definition: XmlRpcUtil.cpp:85

Referenced by XmlRpc::XmlRpcClient::execute(), and XmlRpc::XmlRpcServer::work().

Member Data Documentation

◆ _doClear

bool XmlRpc::XmlRpcDispatch::_doClear
protected

Definition at line 82 of file XmlRpcDispatch.h.

Referenced by clear(), work(), and XmlRpcDispatch().

◆ _endTime

double XmlRpc::XmlRpcDispatch::_endTime
protected

Definition at line 80 of file XmlRpcDispatch.h.

Referenced by exit(), work(), and XmlRpcDispatch().

◆ _inWork

bool XmlRpc::XmlRpcDispatch::_inWork
protected

Definition at line 83 of file XmlRpcDispatch.h.

Referenced by clear(), work(), and XmlRpcDispatch().

◆ _sources

SourceList XmlRpc::XmlRpcDispatch::_sources
protected

Definition at line 77 of file XmlRpcDispatch.h.

Referenced by addSource(), clear(), removeSource(), setSourceEvents(), and work().


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