Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

DCDT_LinkTx.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 dEVICE cOMMUNITIES dEVELOPMENT tOOLKIT 
00003 
00004 DCDT_LinkTx.cpp
00005 
00006 COPYRIGHT (C) 2002   Alessandro Mazzini (mazzini@airlab.elet.polimi.it)
00007                      Cristian Giussani (cgiussani@fastflow.it)
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
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    // Don't free the payload it wasn't dynamically allocated ( see LinkTx.h:68 )
00060    // We can't "delete msgsub" because it will free payload; we have to change
00061    // the payload pointer the delete msgsub but payload is private
00062    //msgsub->header->payload_len=0;
00063    //msgsub->payload=NULL;
00064    //delete msgsub;
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       // KeepAlive must be sent periodically
00095       channel->KeepAlive();
00096       TRC_PRINT( DCDT_TRC_COMM, TRC1, ("LINKTX %i WORKING", linkID));
00097 
00098       // Read the new suscribers mask, and store it in subs_mask
00099       // if the suscribers mask was changed send the new values 
00100       if (myPostOffice->ReadSubscribedMsgMask(subs_mask,ReadID()))
00101         channel->Send(msgsub);
00102 
00103       // Sending messages 
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         // ANCHE QUI VA GESTITA UNA PERIODICITÀ ( ??? )
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

Generated on Sun Jun 19 10:35:50 2005 for dcdt by  doxygen 1.3.9.1