00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef DCDT_INNERCHANNEL_H
00026 #define DCDT_INNERCHANNEL_H
00027
00028 #include <InnerSocket.h>
00029 #include <DCDT_Channel.h>
00030
00031 #define LOST_TIMER_VAL 5000000
00032 #define SEND_TIMER_VAL 3000000
00033 #define RECV_TIMER_VAL 2000000
00034 #define CONN_TIMER_VAL 10000000
00035
00036 class InnerCommData;
00037
00040 class DCDT_InnerChannel : public DCDT_Channel {
00041 public:
00042 DCDT_InnerChannel(int AgoraID) : DCDT_Channel(AgoraID) {
00043 notify.payload_len = answer.payload_len = 0;
00044 notify.channel = answer.channel = 0;
00045 notify.AgoraID = answer.AgoraID = AgoraID;
00046 localCD = remoteCD = NULL;
00047 inner_name = NULL;
00048 sock = new InnerSocket();
00049 }
00050
00051
00052 virtual inline ~DCDT_InnerChannel();
00053
00054 inline void Open(int link_status = 0);
00055 void Reopen(int link_status){link_status=0;};
00056 inline void StartingPrepare(InnerCommData *cd, char *to);
00057 inline void Prepare(CommData *local, CommData *remote);
00058 void ChooseChannel(int channel){channel=0;};
00059 inline void WaitConn();
00060 inline void StartConn();
00061 inline void Send(const DCDT_Msg *msg);
00062 inline void SendNotify();
00063 inline void SendAnswer(CommData* to, CommData* local);
00064 inline DCDT_Msg* Receive();
00065 inline void ReceiveHS(HSMsgHeader*& header, CommData*& remote);
00066 inline void Dispose();
00067 inline void Close();
00068 inline void KeepAlive(){};
00069 inline CommData* GetCommData() {return NULL;};
00070 inline CommData* GetStartingCD() {return NULL;};
00071 inline unsigned int ReadStartingProfile() {return 0;};
00072 inline void SetTimers();
00073 inline void SetLostTimer(int usec);
00074 inline void LostTimerOn();
00075 inline void LostTimerOff();
00076 inline void SetConnTimer(int usec);
00077 inline void ConnTimerOn();
00078 inline void ConnTimerOff();
00079 inline void SetSendTimer(int usec);
00080 inline void SendTimerOn();
00081 inline void SendTimerOff();
00082 inline void UnblockSend();
00083 inline void SetReceiveTimer(int usec);
00084 inline void ReceiveTimerOn();
00085 inline void ReceiveTimerOff();
00086 inline void UnblockReceive();
00087 void Restart(int link_status);
00088 void Stop();
00089
00090 private:
00091 ProfileType profile;
00092 InnerSocket *sock;
00093 InnerCommData *localCD, *remoteCD;
00094
00096 char *inner_name;
00097 };
00098
00099 class InnerCommData : public CommData {
00100 public:
00101 char* name;
00102
00103 InnerCommData() {
00104 name = NULL;
00105 }
00106
00107 virtual ~InnerCommData() {
00108 if ( name )
00109 delete[] name;
00110 }
00111
00112 InnerCommData(char* n) {
00113 name = new char[strlen(n)+2];
00114 strcpy (name, n);
00115 }
00116
00117 InnerCommData(InnerCommData *cd) {
00118
00119 name = new char[strlen(cd->name)+2];
00120 strcpy(name, cd->name);
00121 }
00122
00123 InnerCommData& operator=(const InnerCommData& cd) {
00124
00125 name = new char[strlen(cd.name)+2];
00126 strcpy(name, cd.name);
00127 return (*this);
00128 }
00129
00130 DCDT_Channel* CreateChannel(int AgoraID) {
00131 return (new DCDT_InnerChannel(AgoraID));
00132 }
00133 };
00134
00135
00136 inline DCDT_InnerChannel::~DCDT_InnerChannel()
00137 {
00138 if ( sock )
00139 delete sock;
00140
00141 if ( localCD )
00142 delete localCD;
00143
00144 if ( remoteCD )
00145 delete remoteCD;
00146
00147 if ( inner_name )
00148 delete[] inner_name;
00149 };
00150
00151 inline void DCDT_InnerChannel::Prepare(CommData *local, CommData *remote)
00152 {
00153 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("Prepare InnerChannel"));
00154
00155 if ( localCD )
00156 delete localCD;
00157
00158 if (local) {
00159 localCD = (InnerCommData*)local;
00160
00161 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("localCD: %s", localCD->name));
00162 } else
00163 localCD = new InnerCommData();
00164
00165 if ( remoteCD )
00166 delete remoteCD;
00167
00168 if (remote) {
00169 remoteCD = (InnerCommData*)remote;
00170 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("remoteCD: %s", remoteCD->name));
00171 } else
00172 remoteCD = new InnerCommData();
00173 }
00174
00175 inline void DCDT_InnerChannel::StartingPrepare(InnerCommData *cd, char *to)
00176 {
00177 if (cd) {
00178 localCD = new InnerCommData(cd);
00179
00180 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("localCD: %s", localCD->name));
00181 } else
00182 localCD = new InnerCommData();
00183
00184 if (to) {
00185 inner_name = new char[strlen(to) + 1];
00186 strcpy(inner_name, to);
00187
00188 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("dest: %s", inner_name));
00189 }
00190 }
00191
00192 inline void DCDT_InnerChannel::Open(int link_status)
00193 {
00194 sock->Open();
00195 sock->Bind(localCD->name);
00196 }
00197
00198 inline void DCDT_InnerChannel::WaitConn()
00199 {
00200 DCDT_Msg *m;
00201 char *n;
00202
00203 sock->SetReceiveTimer(CONN_TIMER_VAL);
00204 m = sock->Receive();
00205
00206 if ( m != NULL ) {
00207 if (m->ReadType() == MT_CONNECT) {
00208 n = (char*)m->GetPayload();
00209
00210 if (remoteCD) {
00211 if (remoteCD->name)
00212 delete[] remoteCD->name;
00213 remoteCD->name = new char[strlen(n) + 1];
00214 strcpy(remoteCD->name, n);
00215 }
00216 else
00217 remoteCD = new InnerCommData(n);
00218
00219 sock->Connect(remoteCD->name);
00220 }
00221
00222 delete m;
00223 sock->SetReceiveTimer(RECV_TIMER_VAL);
00224 }
00225
00226 }
00227
00228 inline DCDT_Msg* DCDT_InnerChannel::Receive()
00229 {
00230 return sock->Receive();
00231 }
00232
00233 inline void DCDT_InnerChannel::ReceiveHS(HSMsgHeader*& header, CommData*& remote)
00234 {
00235 char* data;
00236 sock->ReceiveHS(header, data);
00237
00238 remote = new InnerCommData(data);
00239 }
00240
00242 inline void DCDT_InnerChannel::Send(const DCDT_Msg *msg)
00243 {
00244 sock->Send(msg);
00245 }
00246
00251 inline void DCDT_InnerChannel::SendNotify()
00252 {
00253 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("Sending HS_NOTIFY"));
00254
00255 notify.AgoraID=0;
00256 sock->SendHS(inner_name, ¬ify, localCD->name);
00257 }
00258
00260 inline void DCDT_InnerChannel::SendAnswer(CommData* to, CommData* local)
00261 {
00262 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("Sending HS_ANSWER"));
00263
00264 answer.AgoraID=0;
00265 sock->SendHS(((InnerCommData*)to)->name, &answer, ((InnerCommData*)local)->name);
00266 }
00267
00268 inline void DCDT_InnerChannel::StartConn()
00269 {
00270 DCDT_Msg m(MT_CONNECT, 1);
00271
00272 sock->Connect(remoteCD->name);
00273
00274 m.SetPayload(localCD->name, strlen(localCD->name)+1);
00275 sock->Send(&m);
00276 }
00277
00278 inline void DCDT_InnerChannel::Dispose()
00279 {
00280 if ( localCD ) {
00281 if ( localCD->name )
00282 delete[] localCD->name;
00283
00284 delete localCD;
00285 }
00286
00287 if ( remoteCD ) {
00288 if ( remoteCD->name )
00289 delete[] remoteCD->name;
00290
00291 delete remoteCD;
00292 }
00293 }
00294
00295 inline void DCDT_InnerChannel::Close()
00296 {
00297 sock->Close();
00298 }
00299
00300 inline void DCDT_InnerChannel::SetTimers()
00301 {
00302 sock->SetLostTimer(LOST_TIMER_VAL);
00303 sock->LostTimerOn();
00304 sock->SetConnTimer(CONN_TIMER_VAL);
00305 sock->ConnTimerOn();
00306 sock->SetSendTimer(SEND_TIMER_VAL);
00307 sock->SendTimerOn();
00308 sock->SetReceiveTimer(RECV_TIMER_VAL);
00309 sock->ReceiveTimerOn();
00310 }
00311
00312 inline void DCDT_InnerChannel::SetLostTimer(int usec)
00313 {
00314 sock->SetLostTimer(usec);
00315 }
00316
00317 inline void DCDT_InnerChannel::LostTimerOn()
00318 {
00319 sock->LostTimerOn();
00320 }
00321
00322 inline void DCDT_InnerChannel::LostTimerOff()
00323 {
00324 sock->LostTimerOff();
00325 }
00326
00327 inline void DCDT_InnerChannel::SetConnTimer(int usec)
00328 {
00329 sock->SetConnTimer(usec);
00330 }
00331
00332 inline void DCDT_InnerChannel::ConnTimerOn()
00333 {
00334 sock->ConnTimerOn();
00335 }
00336
00337 inline void DCDT_InnerChannel::ConnTimerOff()
00338 {
00339 sock->ConnTimerOff();
00340 }
00341
00342 inline void DCDT_InnerChannel::SetSendTimer(int usec)
00343 {
00344 sock->SetSendTimer(usec);
00345 }
00346
00347 inline void DCDT_InnerChannel::SendTimerOn()
00348 {
00349 sock->SendTimerOn();
00350 }
00351
00352 inline void DCDT_InnerChannel::SendTimerOff()
00353 {
00354 sock->SendTimerOff();
00355 }
00356
00357 inline void DCDT_InnerChannel::UnblockSend()
00358 {
00359 sock->UnblockSend();
00360 }
00361
00362 inline void DCDT_InnerChannel::SetReceiveTimer(int usec)
00363 {
00364 sock->SetReceiveTimer(usec);
00365 }
00366
00367 inline void DCDT_InnerChannel::ReceiveTimerOn()
00368 {
00369 sock->ReceiveTimerOn();
00370 }
00371
00372 inline void DCDT_InnerChannel::ReceiveTimerOff()
00373 {
00374 sock->ReceiveTimerOff();
00375 }
00376
00377 inline void DCDT_InnerChannel::UnblockReceive()
00378 {
00379 sock->UnblockReceive();
00380 }
00381
00382 inline void DCDT_InnerChannel::Restart(int link_status)
00383 {
00384 Open(link_status);
00385 }
00386
00387 inline void DCDT_InnerChannel::Stop()
00388 {
00389 if (sock)
00390 sock->ForcedClose();
00391 }
00392
00393 #endif