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

DCDT_InnerLinkManager.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 dEVICE cOMMUNITIES dEVELOPMENT tOOLKIT 
00003 
00004 DCDT_InnerLinkManager.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 INNERLINKMAN_CPP
00026 #define INNERLINKMAN_CPP
00027 
00028 using namespace std;
00029 
00030 #include <DCDT_InnerLinkManager.h>
00031 #include <DCDT_Agora.h>
00032 #include <DCDT_ctrace.h>
00033 
00034 DCDT_InnerLinkManager::DCDT_InnerLinkManager(DCDT_Agora* agora) : DCDT_Member(agora)
00035 {
00036   InnerChannel = new DCDT_InnerChannel(myAgoraID);
00037   InnerStartCD = new InnerCommData(INNER_STARTNAME);
00038   InnerCD = new InnerCommData(INNER_NAME);
00039 
00040   hs_header = NULL;
00041   hs_cd = NULL;
00042   innerlink_counter = 0;
00043   localCD  = remoteCD = NULL;
00044   name = NULL;
00045   name_cd = NULL;
00046 }
00047 
00048 DCDT_InnerLinkManager::~DCDT_InnerLinkManager()
00049 {
00050   /* ??? Usare un metodo diverso da delete per eliminare l'InnerLinkChannel
00051          da' un warning in compilazione se dichiaro il distruttore
00052   */
00053   delete InnerChannel;
00054   delete InnerStartCD;
00055   //delete InnerCD;
00056   delete hs_header;
00057   delete hs_cd;
00058 
00059   //localCD->Dispose();
00060   //delete localCD;
00061 
00062   //remoteCD->Dispose();
00063   delete remoteCD;
00064   delete name;
00065 
00066   delete name_cd;
00067 }
00068 
00074 int DCDT_InnerLinkManager::LookForExistingAgora(int &agoraID)
00075 {
00076   try {
00077     remove(InnerStartCD->name);
00078     InnerChannel->StartingPrepare(InnerStartCD, INNER_NAME);
00079     InnerChannel->Open();
00080     try {
00081       InnerChannel->SendNotify();
00082     } catch (ConnError e) {
00083       // no socket on this machine or nobody linked to it: this is the first Agorà
00084         cout << "ConnErr\n";
00085       remove(INNER_NAME);
00086       return 0;
00087     }
00088     InnerChannel->SetReceiveTimer(ILM_RECV_TIMER);
00089     InnerChannel->ReceiveTimerOn();
00090     try {
00091       InnerChannel->ReceiveHS(hs_header, hs_cd);
00092     } catch (TimeOut t) {
00093       // nobody answered: this becomes the first Agorà
00094       cout << "Timeout\n";
00095       remove(INNER_NAME);
00096       return 0;
00097     }
00098 
00099     // this is not the first Agorà, setting up the inner link
00100     InnerChannel->Close();
00101 
00102     agoraID = hs_header->AgoraID;
00103     delete localCD;
00104     localCD = (InnerCommData*)hs_cd; // name assigned to the couple of sockets
00105     cout << "\n" << localCD->name;
00106     remoteCD = new InnerCommData(localCD);
00107     cout << "\n" << remoteCD->name;
00108     ++innerlink_counter;
00109     Delay(500000); // necessary ???
00110     CreateLink(L_CONNECTING);
00111     return 1;
00112   } catch (ChannelError e) {
00113     cout << "\nException! errno=" << e.errval << "\n";
00114     // problem with the inner channel, starting without it
00115     InnerChannel->Close();
00116     return 1;
00117   }
00118 }
00119 
00122 void DCDT_InnerLinkManager::Init()
00123 {
00124  try {
00125   InnerChannel->Close();
00126   InnerChannel->Prepare(InnerCD, NULL);
00127   InnerChannel->Open();
00128   InnerChannel->ReceiveTimerOff();
00129  } catch (ChannelError e) {
00130   cout << "\nInit InnerLinkManager exception! The InnerChannel is broken";
00131   cout << "\nerrno=" << e.errval << "\n";
00132   myAgora->RemoveMember(this);
00133  }
00134 }
00135 
00136 
00141 void DCDT_InnerLinkManager::DoYourJob(int par)
00142 {
00143   try {
00144     InnerChannel->ReceiveHS(hs_header, hs_cd);
00145 
00146     if (hs_header) {
00147       switch (hs_header->type) {
00148 
00149         case HS_NOTIFY:
00150           TRC_PRINT( DCDT_TRC_COMM, TRC1, ("InnerNotify received from Agorà n.%i", ++innerlink_counter));
00151 
00152           name_cd = CreateName(innerlink_counter);
00153 
00154           // Create a new LinkRx
00155           CreateLink(L_WAITING);
00156           InnerChannel->SendAnswer(hs_cd, name_cd);
00157           delete name_cd;
00158           name_cd = NULL;
00159           break;
00160 
00161         case HS_ANSWER:
00162         // error!
00163 
00164         default:
00165           cout << "\nInnerLinkManager received unknown or wrong msg!";
00166       }
00167       delete hs_header;
00168       delete hs_cd;
00169       hs_header = NULL;
00170       hs_cd = NULL;
00171     }
00172   } catch (Exception e) {
00173     cout << "\nInnerLinkManager exception! The InnerChannel is broken";
00174     myAgora->RemoveMember(this);
00175   }
00176 }
00177 
00182 void DCDT_InnerLinkManager::CreateLink(int status)
00183 {
00184   ProfileType profile = MPM_SYSTEM | MPM_BRIDGE;
00185 
00186   SetCDs(status);
00187 
00188   DCDT_LinkRx *newLink;
00189   newLink = new DCDT_LinkRx(myAgora, 1000+innerlink_counter, 0);
00190   myAgora->AddSysMember(newLink, profile);
00191   newLink->Prepare(status, localCD, remoteCD);
00192 
00193   TRC_PRINT( DCDT_TRC_COMM, TRC1, ("Link created!"));
00194   myAgora->ActivateMember(newLink);
00195 }
00196 
00202 InnerCommData* DCDT_InnerLinkManager::CreateName(int count)
00203 {
00204   InnerCommData *cd;
00205   char *name, c=0;
00206   
00207   switch (count) {
00208   case 1 :
00209     c = '1';
00210     break;
00211   case 2 :
00212     c = '2';
00213     break;
00214   case 3 :
00215     c = '3';
00216     break;
00217   case 4 :
00218     c = '4';
00219     break;
00220   case 5 :
00221     c = '5';
00222     break;
00223   case 6 :
00224     c = '6';
00225     break;
00226   case 7 :
00227     c = '7';
00228     break;
00229   case 8 :
00230     c = '8';
00231     break;
00232   case 9 :
00233     c = '9';
00234     break;
00235   }
00236   
00237   name = new char[strlen(INNER_NAME)+2];
00238   strcpy(name, INNER_NAME);
00239   name[strlen(INNER_NAME)] = c;
00240   name[strlen(INNER_NAME)+1] = '\0';
00241   
00242   cd = new InnerCommData(name);
00243   
00244   localCD = new InnerCommData(cd);
00245   remoteCD = new InnerCommData(cd);
00246   
00247   return cd;
00248 }
00249 
00252 void DCDT_InnerLinkManager::SetCDs(int status)
00253 {
00254   if (status == L_WAITING) {
00255     strcat(localCD->name, "s");
00256     strcat(remoteCD->name, "c");
00257   } else {
00258     strcat(localCD->name, "c");
00259     strcat(remoteCD->name, "s");
00260   }
00261 }
00262 
00263 #endif

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