Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

XmlRpc::XmlRpcDispatch Class Reference

#include <XmlRpcDispatch.h>

List of all members.

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

void addSource (XmlRpcSource *source, unsigned eventMask)
void clear ()
 Clear all sources from the monitored sources list. Sources are closed.
void exit ()
 Exit from work routine.
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)
 XmlRpcDispatch ()
 Constructor.
 ~XmlRpcDispatch ()

Protected Types

typedef std::list< MonitoredSourceSourceList

Protected Member Functions

double getTime ()

Protected Attributes

bool _doClear
double _endTime
bool _inWork
SourceList _sources


Detailed Description

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


Member Typedef Documentation

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


Member Enumeration Documentation

enum XmlRpc::XmlRpcDispatch::EventType
 

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

Enumeration values:
ReadableEvent  data available to read
WritableEvent  connected/data can be written without blocking
Exception  uh oh
00029                    {
00030       ReadableEvent = 1,    
00031       WritableEvent = 2,    
00032       Exception     = 4     
00033     };


Constructor & Destructor Documentation

XmlRpcDispatch::XmlRpcDispatch  ) 
 

Constructor.

00027 {
00028   _endTime = -1.0;
00029   _doClear = false;
00030   _inWork = false;
00031 }

XmlRpcDispatch::~XmlRpcDispatch  ) 
 

00035 {
00036 }


Member Function Documentation

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:
source The source to monitor
eventMask Which event types to watch for.
See also:
EventType
00042 {
00043   _sources.push_back(MonitoredSource(source, mask));
00044 }

void XmlRpcDispatch::clear  ) 
 

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

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.

00174 {
00175   _endTime = 0.0;   // Return from work asap
00176 }

double XmlRpcDispatch::getTime  )  [protected]
 

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.

Parameters:
source The source to stop monitoring
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.

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)

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 }


Member Data Documentation

bool XmlRpc::XmlRpcDispatch::_doClear [protected]
 

double XmlRpc::XmlRpcDispatch::_endTime [protected]
 

bool XmlRpc::XmlRpcDispatch::_inWork [protected]
 

SourceList XmlRpc::XmlRpcDispatch::_sources [protected]
 


The documentation for this class was generated from the following files:
Generated on Wed Feb 2 19:25:19 2011 for BOSS6.5.5 by  doxygen 1.3.9.1