00001 #include "headers.h"
00002
00008
00009
00014 ServiceEntry::ServiceEntry(ServiceTable* serviceTable) {
00015 m_serviceNumber = 0;
00016 m_connectionDescriptor = 0;
00017 m_state = ServiceEntry::State_Disconnected;
00018 m_transportLayerRole = None;
00019 m_retry = 0;
00020 m_sar = NULL;
00021 m_serviceTable = serviceTable;
00022 }
00023
00024
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00047
00048
00049
00050
00051
00052 ServiceEntry::~ServiceEntry() {
00053
00054 if (m_sar) {
00055 delete m_sar;
00056 }
00057 }
00058
00059
00063 void
00064 ServiceEntry::toConnectingState() {
00065 Guard guard(&m_serviceEntryLock);
00066
00067 if (m_state != ServiceEntry::State_Disconnected) {
00068 debug(DEBUG_SE, "Transitioning from illegal state %s", stateToString().c_str());
00069 return;
00070 }
00071
00072 if (m_serviceTable != NULL) {
00073 m_serviceNumber = m_serviceTable->generateServiceNumber();
00074 m_connectionDescriptor = m_serviceTable->generateConnectionDescriptor();
00075 }
00076
00077 m_sar = new SAR(m_serviceTable, m_serviceNumber);
00078 MEMCHECK(m_sar);
00079
00080 m_state = ServiceEntry::State_Connecting;
00081 m_transportLayerRole = ServiceEntry::OriginPeer;
00082 m_retry = 0;
00083 }
00084
00085
00089 void
00090 ServiceEntry::toConnectedState() {
00091 Guard guard(&m_serviceEntryLock);
00092
00093 if ((m_state != ServiceEntry::State_Disconnected)
00094 && (m_state != ServiceEntry::State_Connecting)
00095 && (m_state != ServiceEntry::State_Reconnecting)) {
00096 debug(DEBUG_SE, "Transitioning from illegal state %s", stateToString().c_str());
00097 return;
00098 }
00099
00100 m_state = ServiceEntry::State_Connected;
00101 }
00102
00103
00108 void
00109 ServiceEntry::toReconnectingState() {
00110 Guard guard(&m_serviceEntryLock);
00111
00112 if (m_state != ServiceEntry::State_Broken) {
00113 debug(DEBUG_SE, "Transitioning from illegal state %s", stateToString().c_str());
00114 return;
00115 }
00116
00117 if (m_serviceTable != NULL) {
00118 m_connectionDescriptor = m_serviceTable->generateConnectionDescriptor();
00119 }
00120
00121 m_state = ServiceEntry::State_Reconnecting;
00122
00123 m_retry = 0;
00124 }
00125
00126
00130 void
00131 ServiceEntry::toDisconnectedState() {
00132 Guard guard(&m_serviceEntryLock);
00133
00134 m_state = ServiceEntry::State_Disconnected;
00135 m_sar->connectionClosed();
00136 signalDisconnect();
00137 }
00138
00139
00143 void
00144 ServiceEntry::toBrokenState() {
00145 Guard guard(&m_serviceEntryLock);
00146 if (m_state != ServiceEntry::State_Connected) {
00147 debug(DEBUG_SE, "Transitioning from illegal state %s", stateToString().c_str());
00148 return;
00149 }
00150 m_state = ServiceEntry::State_Broken;
00151 }
00152
00153
00157 void
00158 ServiceEntry::toFailedState() {
00159 Guard guard(&m_serviceEntryLock);
00160
00161 if ( (m_state != ServiceEntry::State_Connecting)
00162 && (m_state != ServiceEntry::State_Reconnecting)
00163 && (m_state != ServiceEntry::State_Broken) ) {
00164 debug(DEBUG_SE, "Transitioning from illegal state %s", stateToString().c_str());
00165 return;
00166 }
00167 m_state = ServiceEntry::State_Failed;
00168 }
00169
00170
00174 void
00175 ServiceEntry::connectionAccepted(int connectionDescriptor) {
00176 m_transportLayerRole = ServiceEntry::DestinationPeer;
00177 m_connectionDescriptor = connectionDescriptor;
00178
00179 if (m_serviceTable != NULL) {
00180 m_serviceNumber = m_serviceTable->generateServiceNumber();
00181 }
00182
00183 m_sar = new SAR(m_serviceTable, m_serviceNumber);
00184 MEMCHECK(m_sar);
00185 toConnectedState();
00186 }
00187
00188
00192 int
00193 ServiceEntry::getRetries() {
00194 return m_retry;
00195 }
00196
00197
00201 void
00202 ServiceEntry::incrementRetries() {
00203 ++m_retry;
00204 }
00205
00206
00211 int
00212 ServiceEntry::getConnectionDescriptor() {
00213 return m_connectionDescriptor;
00214 }
00215
00216
00221 int
00222 ServiceEntry::getServiceNumber() {
00223 return m_serviceNumber;
00224 }
00225
00226
00232 SAR*
00233 ServiceEntry::getSar() {
00234 return m_sar;
00235 }
00236
00237
00241 bool
00242 ServiceEntry::isReconnecting() {
00243 return (m_state == ServiceEntry::State_Reconnecting);
00244 }
00245
00246
00250 bool
00251 ServiceEntry::isConnecting() {
00252 return (m_state == ServiceEntry::State_Connecting);;
00253 }
00254
00255
00259 bool
00260 ServiceEntry::isBroken() {
00261 return (m_state == ServiceEntry::State_Broken);;
00262 }
00263
00264
00268 bool
00269 ServiceEntry::isConnected() {
00270 return (m_state == ServiceEntry::State_Connected);
00271 }
00272
00273
00277 bool
00278 ServiceEntry::isDisconnected() {
00279 return (m_state == ServiceEntry::State_Disconnected);
00280 }
00281
00282
00286 bool
00287 ServiceEntry::isFailed() {
00288 return (m_state == ServiceEntry::State_Failed);
00289 }
00290
00291
00292 bool
00293 ServiceEntry::isOrigin() {
00294 return (this->m_transportLayerRole == OriginPeer);
00295 }
00296
00297
00298 bool
00299 ServiceEntry::isDestination() {
00300 return (this->m_transportLayerRole == DestinationPeer);
00301 }
00302
00303
00315 bool
00316 ServiceEntry::waitForConnectComplete(int timeToWait) {
00317 Guard guard(&m_serviceEntryLock);
00318
00319
00320 if (m_state == ServiceEntry::State_Connected) {
00321 return true;
00322 }
00323 return m_estReceivedCondition.timedWait(&m_serviceEntryLock, timeToWait);
00324 }
00325
00326
00338 bool
00339 ServiceEntry::waitForDisconnect(int timeToWait) {
00340 Guard guard(&m_serviceEntryLock);
00341
00342
00343 if (m_state == ServiceEntry::State_Disconnected) {
00344 return true;
00345 }
00346 return m_finReceivedCondition.timedWait(&m_serviceEntryLock, timeToWait);
00347 }
00348
00349
00353 void
00354 ServiceEntry::signalDisconnect() {
00355 m_finReceivedCondition.signal();
00356 }
00357
00358
00362 void
00363 ServiceEntry::signalConnectComplete() {
00364 m_estReceivedCondition.signal();
00365 }
00366
00367
00371 string
00372 ServiceEntry::stateToString() {
00373 switch (m_state) {
00374 case State_Connected:
00375 return "Connected";
00376 case State_Connecting:
00377 return "Connecting";
00378 case State_Reconnecting:
00379 return "Reconnecting";
00380 case State_Failed:
00381 return "Failed";
00382 case State_Disconnected:
00383 return "Disconnected";
00384 case State_Broken:
00385 return "Broken";
00386 }
00387 return "Unknown";
00388 }
00389
00390
00394 string
00395 ServiceEntry::transportLayerRoleToString() {
00396 switch (m_transportLayerRole) {
00397 case OriginPeer:
00398 return "Origin";
00399 case DestinationPeer:
00400 return "Destination";
00401 case None:
00402 return "None";
00403 }
00404 return "Unknown";
00405 }
00406
00407
00411 void
00412 ServiceEntry::toStream(std::ostream& out) {
00413 out << "+ Service number " << m_serviceNumber << "\n";
00414 out << " + Connection descriptor: " << m_connectionDescriptor << "\n";
00415 out << " + State : " << stateToString() << "\n";
00416 out << " + Transport layer role: " << transportLayerRoleToString() << "\n";
00417 if (m_sar != NULL) {
00418 TimeValue tmp(m_sar->getAverageRtt());
00419 out << " + RTT : " << tmp << "\n";
00420 }
00421 out << " + Retry Count: " << m_retry << "\n";
00422 out << "Segmentation and Reassembly:\n";
00423 out << *m_sar;
00424 }
00425