#include <XmlRpcDispatch.h>
Public Types | |
ReadableEvent = 1 | |
data available to read | |
WritableEvent = 2 | |
connected/data can be written without blocking | |
Exception = 4 | |
uh oh | |
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< MonitoredSource > | SourceList |
Protected Member Functions | |
double | getTime () |
Protected Attributes | |
SourceList | _sources |
double | _endTime |
bool | _doClear |
bool | _inWork |
Classes | |
struct | MonitoredSource |
Definition at line 22 of file XmlRpcDispatch.h.
typedef std::list< MonitoredSource > XmlRpc::XmlRpcDispatch::SourceList [protected] |
Definition at line 74 of file XmlRpcDispatch.h.
Values indicating the type of events a source is interested in.
ReadableEvent | data available to read |
WritableEvent | connected/data can be written without blocking |
Exception | uh oh |
Definition at line 29 of file XmlRpcDispatch.h.
00029 { 00030 ReadableEvent = 1, 00031 WritableEvent = 2, 00032 Exception = 4 00033 };
XmlRpcDispatch::XmlRpcDispatch | ( | ) |
XmlRpcDispatch::~XmlRpcDispatch | ( | ) |
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.
source | The source to monitor | |
eventMask | Which event types to watch for. |
Definition at line 41 of file XmlRpcDispatch.cpp.
References _sources.
Referenced by XmlRpc::XmlRpcServer::acceptConnection(), XmlRpc::XmlRpcServer::bindAndListen(), and XmlRpc::XmlRpcClient::setupConnection().
00042 { 00043 _sources.push_back(MonitoredSource(source, mask)); 00044 }
void XmlRpcDispatch::clear | ( | ) |
Clear all sources from the monitored sources list. Sources are closed.
Definition at line 180 of file XmlRpcDispatch.cpp.
References _doClear, _inWork, and _sources.
Referenced by XmlRpc::XmlRpcServer::shutdown().
00181 { 00182 if (_inWork) 00183 _doClear = true; // Finish reporting current events before clearing 00184 else 00185 { 00186 SourceList closeList = _sources; 00187 _sources.clear(); 00188 for (SourceList::iterator it=closeList.begin(); it!=closeList.end(); ++it) 00189 it->getSource()->close(); 00190 } 00191 }
void XmlRpcDispatch::exit | ( | ) |
Exit from work routine.
Definition at line 173 of file XmlRpcDispatch.cpp.
References _endTime.
Referenced by XmlRpc::XmlRpcClient::close(), and XmlRpc::XmlRpcServer::exit().
00174 { 00175 _endTime = 0.0; // Return from work asap 00176 }
double XmlRpcDispatch::getTime | ( | ) | [protected] |
Definition at line 195 of file XmlRpcDispatch.cpp.
Referenced by work().
00196 { 00197 #ifdef USE_FTIME 00198 struct timeb tbuff; 00199 00200 ftime(&tbuff); 00201 return ((double) tbuff.time + ((double)tbuff.millitm / 1000.0) + 00202 ((double) tbuff.timezone * 60)); 00203 #else 00204 struct timeval tv; 00205 struct timezone tz; 00206 00207 gettimeofday(&tv, &tz); 00208 return (tv.tv_sec + tv.tv_usec / 1000000.0); 00209 #endif /* USE_FTIME */ 00210 }
void XmlRpcDispatch::removeSource | ( | XmlRpcSource * | source | ) |
Stop monitoring this source.
source | The source to stop monitoring |
Definition at line 48 of file XmlRpcDispatch.cpp.
References _sources.
Referenced by XmlRpc::XmlRpcClient::close(), XmlRpc::XmlRpcServer::removeConnection(), and XmlRpc::XmlRpcClient::setupConnection().
00049 { 00050 for (SourceList::iterator it=_sources.begin(); it!=_sources.end(); ++it) 00051 if (it->getSource() == source) 00052 { 00053 _sources.erase(it); 00054 break; 00055 } 00056 }
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.
References _sources.
00062 { 00063 for (SourceList::iterator it=_sources.begin(); it!=_sources.end(); ++it) 00064 if (it->getSource() == source) 00065 { 00066 it->getMask() = eventMask; 00067 break; 00068 } 00069 }
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.
References _doClear, _endTime, _inWork, _sources, XmlRpc::XmlRpcSource::close(), ers::error, Exception, XmlRpc::XmlRpcSource::getfd(), XmlRpc::XmlRpcSource::getKeepOpen(), getTime(), XmlRpc::XmlRpcSource::handleEvent(), ReadableEvent, and WritableEvent.
Referenced by XmlRpc::XmlRpcClient::execute(), and XmlRpc::XmlRpcServer::work().
00076 { 00077 // Compute end time 00078 _endTime = (timeout < 0.0) ? -1.0 : (getTime() + timeout); 00079 _doClear = false; 00080 _inWork = true; 00081 00082 // Only work while there is something to monitor 00083 while (_sources.size() > 0) { 00084 00085 // Construct the sets of descriptors we are interested in 00086 fd_set inFd, outFd, excFd; 00087 FD_ZERO(&inFd); 00088 FD_ZERO(&outFd); 00089 FD_ZERO(&excFd); 00090 00091 int maxFd = -1; // Not used on windows 00092 SourceList::iterator it; 00093 for (it=_sources.begin(); it!=_sources.end(); ++it) { 00094 int fd = it->getSource()->getfd(); 00095 if (it->getMask() & ReadableEvent) FD_SET(fd, &inFd); 00096 if (it->getMask() & WritableEvent) FD_SET(fd, &outFd); 00097 if (it->getMask() & Exception) FD_SET(fd, &excFd); 00098 if (it->getMask() && fd > maxFd) maxFd = fd; 00099 } 00100 00101 // Check for events 00102 int nEvents; 00103 if (timeout < 0.0) 00104 nEvents = select(maxFd+1, &inFd, &outFd, &excFd, NULL); 00105 else 00106 { 00107 struct timeval tv; 00108 tv.tv_sec = (int)floor(timeout); 00109 tv.tv_usec = ((int)floor(1000000.0 * (timeout-floor(timeout)))) % 1000000; 00110 nEvents = select(maxFd+1, &inFd, &outFd, &excFd, &tv); 00111 } 00112 00113 if (nEvents < 0) 00114 { 00115 XmlRpcUtil::error("Error in XmlRpcDispatch::work: error in select (%d).", nEvents); 00116 _inWork = false; 00117 return; 00118 } 00119 00120 // Process events 00121 for (it=_sources.begin(); it != _sources.end(); ) 00122 { 00123 SourceList::iterator thisIt = it++; 00124 XmlRpcSource* src = thisIt->getSource(); 00125 int fd = src->getfd(); 00126 unsigned newMask = (unsigned) -1; 00127 if (fd <= maxFd) { 00128 // If you select on multiple event types this could be ambiguous 00129 if (FD_ISSET(fd, &inFd)) 00130 newMask &= src->handleEvent(ReadableEvent); 00131 if (FD_ISSET(fd, &outFd)) 00132 newMask &= src->handleEvent(WritableEvent); 00133 if (FD_ISSET(fd, &excFd)) 00134 newMask &= src->handleEvent(Exception); 00135 00136 if ( ! newMask) { 00137 _sources.erase(thisIt); // Stop monitoring this one 00138 if ( ! src->getKeepOpen()) 00139 src->close(); 00140 } else if (newMask != (unsigned) -1) { 00141 thisIt->getMask() = newMask; 00142 } 00143 } 00144 } 00145 00146 // Check whether to clear all sources 00147 if (_doClear) 00148 { 00149 SourceList closeList = _sources; 00150 _sources.clear(); 00151 for (SourceList::iterator it=closeList.begin(); it!=closeList.end(); ++it) { 00152 XmlRpcSource *src = it->getSource(); 00153 src->close(); 00154 } 00155 00156 _doClear = false; 00157 } 00158 00159 // Check whether end time has passed 00160 if (0 <= _endTime && getTime() > _endTime){ 00161 std::cout<< "XmlRpc : time out when connect to database " << std::endl;//yzhang debug 00162 break; 00163 } 00164 } 00165 00166 _inWork = false; 00167 }
bool XmlRpc::XmlRpcDispatch::_doClear [protected] |
Definition at line 82 of file XmlRpcDispatch.h.
Referenced by clear(), work(), and XmlRpcDispatch().
double XmlRpc::XmlRpcDispatch::_endTime [protected] |
bool XmlRpc::XmlRpcDispatch::_inWork [protected] |
Definition at line 83 of file XmlRpcDispatch.h.
Referenced by clear(), work(), and XmlRpcDispatch().
SourceList XmlRpc::XmlRpcDispatch::_sources [protected] |
Definition at line 77 of file XmlRpcDispatch.h.
Referenced by addSource(), clear(), removeSource(), setSourceEvents(), and work().