00001
00002
00003
00004
00005
00006
00007 #include <iostream>
00008
00009 #include "SysPlusPlus/syscall.h"
00010 #include <string.h>
00011
00012
00013 #include "ComPlusPlus/Poll.h"
00014 #include "ComPlusPlus/Comm.h"
00015
00016 #include "ComPlusPlus/SocketTcp.h"
00017 #include <unistd.h>
00018 #include "SysPlusPlus/ComException.h"
00019
00020
00021 compp::Poll::~Poll () {
00022
00023 }
00024
00025
00031 compp::Poll::Poll ( ) {
00032
00033 NumConn = 0;
00034 Timeout = StdPollTimeout;
00035 FD_ZERO( & rdset );
00036 FD_ZERO( & wrset );
00037 FD_ZERO( & errset );
00038 ErrorContainer.clear();
00039 ReadContainer.clear();
00040 WriteContainer.clear();
00041 }
00042
00046 void compp::Poll::AddWriteQueue( compp::Comm * Channel ) {
00047
00048 if ( Channel == NULL ) {
00049 throw syspp::ComException("CommunicationChannel is NULL.");
00050 }
00051 int FD;
00052
00053 if ( ( FD = Channel->GetFd ()) == -1 ) {
00054 throw syspp::ComException("CommunicationChannel not initialised.");
00055 }
00056 WriteContainer[FD] = Channel;
00057 }
00058
00062 void compp::Poll::AddReadQueue( Comm * Channel ){
00063
00064 if ( Channel == NULL ) {
00065 throw syspp::ComException("CommunicationChannel is NULL.");
00066 }
00067
00068 int FD;
00069
00070 if ( (FD = Channel->GetFd ()) == -1 ) {
00071 throw syspp::ComException("CommunicationChannel not initialised.");
00072 }
00073
00074 ReadContainer[FD] = Channel;
00075
00076
00077 }
00078
00082 void compp::Poll::AddErrorQueue( Comm * Channel ){
00083
00084 if ( Channel == NULL ) {
00085 throw syspp::ComException("CommunicationChannel is NULL.");
00086 }
00087
00088 int FD;
00089
00090 if ( (FD = Channel->GetFd ()) == -1 ) {
00091 throw syspp::ComException("CommunicationChannel not initialised.");
00092 }
00093 ErrorContainer[FD] = Channel;
00094 }
00095
00099 void compp::Poll::SetTimeout ( int usecs ) {
00100 this->Timeout = usecs;
00101 }
00102
00108 int compp::Poll::Multiplexer ( ) {
00109 return Multiplexer ( this->Timeout ) ;
00110 }
00111
00117 int compp::Poll::Multiplexer ( int usecs ) {
00118
00119 struct timeval tv, *tvp;
00120
00121 if ( usecs >= 0 ) {
00122 memset (&tv, 0, sizeof(tv));
00123 tv.tv_sec = usecs / 1000000;
00124 tv.tv_usec = usecs % 1000000;
00125 tvp = &tv;
00126 } else {
00127 tvp = NULL;
00128 }
00129
00130 int maxfd=0;
00131 std::map<int, Comm*>::iterator iter;
00132
00133 for ( iter=WriteContainer.begin(); iter != WriteContainer.end(); ++iter ) {
00134
00135 int i;
00136 i = iter->second->GetFd();
00137 FD_SET( i, &wrset);
00138 maxfd = maxfd < i ? i : maxfd;
00139 }
00140
00141
00142 for ( iter=ReadContainer.begin(); iter != ReadContainer.end(); ++iter ) {
00143 int i;
00144 i = iter->second->GetFd();
00145 FD_SET( i, &rdset);
00146 maxfd = maxfd < i ? i : maxfd;
00147 }
00148
00149
00150 for ( iter=ErrorContainer.begin(); iter != ErrorContainer.end(); ++iter ) {
00151 int i;
00152 i = iter->second->GetFd();
00153 FD_SET( i, &errset);
00154 maxfd = maxfd < i ? i : maxfd;
00155 }
00156
00157
00158 int retv;
00159 if (-1 == (retv = syspp::Call::Select (maxfd + 1, &rdset, &wrset, &errset, tvp))) {
00160 throw syspp::ComException ( "compp::Comm::PollRcv. Select failed" );
00161 }
00162
00163 return retv;
00164 }
00165
00171 bool compp::Poll::CheckAll( Comm * Channel ) {
00172
00173 if ( FD_ISSET ( Channel->GetFd(), &rdset ) ) {
00174 return true;
00175 }
00176
00177 if ( FD_ISSET ( Channel->GetFd(), &wrset ) ) {
00178 return true;
00179 }
00180
00181 if ( FD_ISSET ( Channel->GetFd(), &errset ) ) {
00182 return true;
00183 }
00184
00185 return false;
00186
00187 }
00188
00193 bool compp::Poll::CheckReadQueue( Comm * Channel ) {
00194
00195 if ( FD_ISSET ( Channel->GetFd(), &rdset ) ) {
00196 return true;
00197 }
00198
00199 return false;
00200 }
00201
00206 bool compp::Poll::CheckWriteQueue( Comm * Channel ) {
00207 if ( FD_ISSET ( Channel->GetFd(), &wrset ) ) {
00208 return true;
00209 }
00210 return false;
00211 }
00212
00217 bool compp::Poll::CheckErrorQueue( Comm * Channel ) {
00218
00219
00220 if ( FD_ISSET ( Channel->GetFd(), &errset ) ) {
00221 return true;
00222 }
00223
00224 return false;
00225
00226 }
00227
00231 void compp::Poll::EmptyWriteQueue( ) {
00232 FD_ZERO( & wrset );
00233 WriteContainer.clear();
00234 }
00235
00239 void compp::Poll::EmptyReadQueue ( ){
00240 FD_ZERO( & rdset );
00241 ReadContainer.clear();
00242 }
00243
00247 void compp::Poll::EmptyErrorQueue( ) {
00248 FD_ZERO( & errset );
00249 ErrorContainer.clear();
00250 }
00251
00255 void compp::Poll::EmptyAllQueues( ) {
00256 FD_ZERO( & wrset );
00257 FD_ZERO( & rdset );
00258 FD_ZERO( & errset );
00259 ErrorContainer.clear();
00260 ReadContainer.clear();
00261 WriteContainer.clear();
00262 }
00263