BOSS 8.0.0
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

Definition at line 72 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 25 of file XmlRpcDispatch.cpp.

25 {
26 _endTime = -1.0;
27 _doClear = false;
28 _inWork = false;
29}

◆ ~XmlRpcDispatch()

XmlRpcDispatch::~XmlRpcDispatch ( )

Definition at line 31 of file XmlRpcDispatch.cpp.

31{}

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 35 of file XmlRpcDispatch.cpp.

35 {
36 _sources.push_back( MonitoredSource( source, mask ) );
37}

◆ clear()

void XmlRpcDispatch::clear ( )

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

Definition at line 161 of file XmlRpcDispatch.cpp.

161 {
162 if ( _inWork ) _doClear = true; // Finish reporting current events before clearing
163 else
164 {
165 SourceList closeList = _sources;
166 _sources.clear();
167 for ( SourceList::iterator it = closeList.begin(); it != closeList.end(); ++it )
168 it->getSource()->close();
169 }
170}
std::list< MonitoredSource > SourceList

◆ exit()

void XmlRpcDispatch::exit ( )

Exit from work routine.

Definition at line 156 of file XmlRpcDispatch.cpp.

156 {
157 _endTime = 0.0; // Return from work asap
158}

◆ getTime()

double XmlRpcDispatch::getTime ( )
protected

Definition at line 172 of file XmlRpcDispatch.cpp.

172 {
173#ifdef USE_FTIME
174 struct timeb tbuff;
175
176 ftime( &tbuff );
177 return ( (double)tbuff.time + ( (double)tbuff.millitm / 1000.0 ) +
178 ( (double)tbuff.timezone * 60 ) );
179#else
180 struct timeval tv;
181 struct timezone tz;
182
183 gettimeofday( &tv, &tz );
184 return ( tv.tv_sec + tv.tv_usec / 1000000.0 );
185#endif /* USE_FTIME */
186}

Referenced by work().

◆ removeSource()

void XmlRpcDispatch::removeSource ( XmlRpcSource * source)

Stop monitoring this source.

Parameters
sourceThe source to stop monitoring

Definition at line 40 of file XmlRpcDispatch.cpp.

40 {
41 for ( SourceList::iterator it = _sources.begin(); it != _sources.end(); ++it )
42 if ( it->getSource() == source )
43 {
44 _sources.erase( it );
45 break;
46 }
47}

◆ setSourceEvents()

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

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

Definition at line 50 of file XmlRpcDispatch.cpp.

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

◆ 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 60 of file XmlRpcDispatch.cpp.

60 {
61 // Compute end time
62 _endTime = ( timeout < 0.0 ) ? -1.0 : ( getTime() + timeout );
63 _doClear = false;
64 _inWork = true;
65
66 // Only work while there is something to monitor
67 while ( _sources.size() > 0 )
68 {
69
70 // Construct the sets of descriptors we are interested in
71 fd_set inFd, outFd, excFd;
72 FD_ZERO( &inFd );
73 FD_ZERO( &outFd );
74 FD_ZERO( &excFd );
75
76 int maxFd = -1; // Not used on windows
77 SourceList::iterator it;
78 for ( it = _sources.begin(); it != _sources.end(); ++it )
79 {
80 int fd = it->getSource()->getfd();
81 if ( it->getMask() & ReadableEvent ) FD_SET( fd, &inFd );
82 if ( it->getMask() & WritableEvent ) FD_SET( fd, &outFd );
83 if ( it->getMask() & Exception ) FD_SET( fd, &excFd );
84 if ( it->getMask() && fd > maxFd ) maxFd = fd;
85 }
86
87 // Check for events
88 int nEvents;
89 if ( timeout < 0.0 ) nEvents = select( maxFd + 1, &inFd, &outFd, &excFd, NULL );
90 else
91 {
92 struct timeval tv;
93 tv.tv_sec = (int)floor( timeout );
94 tv.tv_usec = ( (int)floor( 1000000.0 * ( timeout - floor( timeout ) ) ) ) % 1000000;
95 nEvents = select( maxFd + 1, &inFd, &outFd, &excFd, &tv );
96 }
97
98 if ( nEvents < 0 )
99 {
100 XmlRpcUtil::error( "Error in XmlRpcDispatch::work: error in select (%d).", nEvents );
101 _inWork = false;
102 return;
103 }
104
105 // Process events
106 for ( it = _sources.begin(); it != _sources.end(); )
107 {
108 SourceList::iterator thisIt = it++;
109 XmlRpcSource* src = thisIt->getSource();
110 int fd = src->getfd();
111 unsigned newMask = (unsigned)-1;
112 if ( fd <= maxFd )
113 {
114 // If you select on multiple event types this could be ambiguous
115 if ( FD_ISSET( fd, &inFd ) ) newMask &= src->handleEvent( ReadableEvent );
116 if ( FD_ISSET( fd, &outFd ) ) newMask &= src->handleEvent( WritableEvent );
117 if ( FD_ISSET( fd, &excFd ) ) newMask &= src->handleEvent( Exception );
118
119 if ( !newMask )
120 {
121 _sources.erase( thisIt ); // Stop monitoring this one
122 if ( !src->getKeepOpen() ) src->close();
123 }
124 else if ( newMask != (unsigned)-1 ) { thisIt->getMask() = newMask; }
125 }
126 }
127
128 // Check whether to clear all sources
129 if ( _doClear )
130 {
131 SourceList closeList = _sources;
132 _sources.clear();
133 for ( SourceList::iterator it = closeList.begin(); it != closeList.end(); ++it )
134 {
135 XmlRpcSource* src = it->getSource();
136 src->close();
137 }
138
139 _doClear = false;
140 }
141
142 // Check whether end time has passed
143 if ( 0 <= _endTime && getTime() > _endTime )
144 {
145 std::cout << "XmlRpc : time out when connect to database " << std::endl; // yzhang
146 // debug
147 break;
148 }
149 }
150
151 _inWork = false;
152}
virtual void close()
static void error(const char *fmt,...)
Dump error messages somewhere.

Member Data Documentation

◆ _doClear

bool XmlRpc::XmlRpcDispatch::_doClear
protected

Definition at line 80 of file XmlRpcDispatch.h.

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

◆ _endTime

double XmlRpc::XmlRpcDispatch::_endTime
protected

Definition at line 78 of file XmlRpcDispatch.h.

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

◆ _inWork

bool XmlRpc::XmlRpcDispatch::_inWork
protected

Definition at line 81 of file XmlRpcDispatch.h.

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

◆ _sources

SourceList XmlRpc::XmlRpcDispatch::_sources
protected

Definition at line 75 of file XmlRpcDispatch.h.

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


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