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 LINKTX_CPP
00026 #define LINKTX_CPP
00027
00028 using namespace std;
00029
00030 #include <DCDT_LinkTx.h>
00031 #include <DCDT_LinkRx.h>
00032 #include <DCDT_Finder.h>
00033 #define LINKTX_PERIOD 100000
00034
00035 DCDT_LinkTx::DCDT_LinkTx(DCDT_Agora *agora, int id, int r_id, DCDT_LinkRx *ptr) : DCDT_Member(agora)
00036 {
00037 linkID = id;
00038 remoteID = r_id;
00039 status = restart_counter = 0;
00040 for (int i=0; i<NUMINT_MSGTYPE; i++)
00041 mymask[i] = 0;
00042 msgrcv = NULL;
00043 msgsub = CreateMsg(MT_SUBSUPD);
00044 msgsub->SetDeliveryWarranty(TCP_WARRANTY);
00045 msgsub->SetPayload(subs_mask, NUMINT_MSGTYPE*sizeof(unsigned int));
00046 for (int i=0; i<NUMINT_MSGTYPE; i++)
00047 subs_mask[i] = 0;
00048 LinkRxPtr = ptr;
00049 };
00050
00051
00052 void DCDT_LinkTx::Init()
00053 {
00054 }
00055
00056 void DCDT_LinkTx::Close()
00057 {
00058
00059
00060
00061
00062
00063
00064
00065 }
00066
00067 void DCDT_LinkTx::Prepare(DCDT_Channel *ch)
00068 {
00069 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("Preparing LINKTX"));
00070 channel = ch;
00071 status = L_WORKING;
00072 }
00073
00074
00087 void DCDT_LinkTx::DoYourJob(int par)
00088 {
00089 try {
00090 switch (status) {
00091
00092 case L_WORKING:
00093
00094
00095 channel->KeepAlive();
00096 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("LINKTX %i WORKING", linkID));
00097
00098
00099
00100 if (myPostOffice->ReadSubscribedMsgMask(subs_mask,ReadID()))
00101 channel->Send(msgsub);
00102
00103
00104 if ((msgrcv = ReceiveMsg( false ))) {
00105 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("LINKTX %i read local msg type %i", linkID, msgrcv->ReadType()));
00106 channel->Send(msgrcv);
00107
00108
00109 }
00110 Delay(LINKTX_PERIOD);
00111 break;
00112
00113 case L_LOST:
00114 Delay(LINK_LOST_TIMER);
00115 break;
00116
00117 case L_RESTARTING:
00118 channel = restarting_ch;
00119 status = L_WORKING;
00120 }
00121 } catch(TimeOut e) {
00122 cout << "\nTimeout on linktx " << linkID << "\nConnection lost";
00123 LinkRxPtr->ChangeStatus(L_STOPPING);
00124 UnSubscribeAll();
00125 status = L_LOST;
00126 } catch(ConnError e) {
00127 cout << "\nError on linktx " << linkID << "\nerrno=" << e.errval << "\nConnection lost";
00128 LinkRxPtr->ChangeStatus(L_STOPPING);
00129 UnSubscribeAll();
00130 status = L_LOST;
00131 } catch(ChannelError e) {
00132 cout << "\nError on linktx " << linkID << "\nerrno=" << e.errval << "\nConnection lost";
00133 LinkRxPtr->ChangeStatus(L_STOPPING);
00134 UnSubscribeAll();
00135 status = L_LOST;
00136 } catch(Exception e) {
00137 cout << "\nException! Closing linktx " << linkID << "\n";
00138 UnSubscribeAll();
00139 Close();
00140 }
00141 }
00142
00144 void DCDT_LinkTx::Subscribe(unsigned int *mask)
00145 {
00146 int i,j,dim = 8*sizeof(unsigned int), filter;
00147 for (i=0; i<NUMINT_MSGTYPE; i++) {
00148 filter = 1;
00149 for (j=1; j<=dim; j++) {
00150 if ((mask[i] & filter) > 0) {
00151 TRC_PRINT( DCDT_TRC_COMM, TRC1, ("Subscribed to type %i", j+i*dim));
00152 SubscribeMsgType(j+i*dim);
00153 }
00154 filter <<= 1;
00155 }
00156 }
00157 }
00158
00159 void DCDT_LinkTx::Restart(DCDT_Channel *ch)
00160 {
00161 restarting_ch = ch;
00162 status = L_RESTARTING;
00163 }
00164
00165 #endif