PCMNetwork.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00025 //
00026 // Inner includes
00027 //
00028 
00029 #include "../include/PCMNetwork.h"
00030 
00031 namespace ParCompMark
00032 {
00033 
00034   //
00035   // Class variables
00036   //
00037 
00038   Container < Network::IfConf, DummyLock >::Pointer Network::mIfConfs;
00039   unsigned short Network::mCommunicationPort = 1234;
00040   unsigned short Network::mBroadcastPort = 1235;
00041 
00042   //
00043   // Constructors & destructor
00044   //
00045 
00046    Network::Network(const std::string & name):
00047         // Parent initializer 
00048    Thread(name          /* Name of the thread */
00049         )
00050         // You have to initialize the following attributes:
00051         // - mIfConfs
00052         // - mInitialized
00053         // - mCommunicationPort
00054         // - mBroadcastPort
00055         // - mStreamSocket
00056         // - mBroadcastSocket
00057         // - mOwnIP
00058   {
00059 
00060         mInitialized = false;
00061         mStreamSocket = -1;
00062         mBroadcastSocket = -1;
00063         mOwnIP = "";
00064 
00065         Logger::getInstance()->log(Logger::NOTICE, "Network created.");
00066 
00067   }
00068 
00069  /*----------------------------------------------------------------------*/
00070 
00071   Network::~Network()
00072   {
00073   }
00074 
00075  /*----------------------------------------------------------------------*/
00076 
00077   //
00078   // Class methods
00079   //
00080 
00081   void Network::getIP()
00082   {
00083 
00084         if(mIfConfs.isNull())
00085         {
00086 
00087          mIfConfs = new Container < IfConf, DummyLock >;
00088 
00089         } else
00090         {
00091          mIfConfs.kill();
00092          mIfConfs = new Container < IfConf, DummyLock >;
00093         }
00094 
00095         int n,
00096         sock,
00097         max;
00098 
00099         max = 4;
00100         int buffSize = max * sizeof(struct ifreq);
00101         struct ifreq *ifr;
00102         struct ifconf ifc;
00103 
00104         //TODO: ha nagyon sok akkor novelni kell!!!
00105         char *buff = new char[buffSize],
00106         data[16];
00107 
00108         bool enough = false;
00109 
00110         sock = socket(AF_INET, SOCK_STREAM, 0);
00111 
00112         unsigned char *d;
00113 
00114         do
00115         {
00116 
00117          ifc.ifc_buf = buff;
00118          ifc.ifc_len = buffSize;
00119          ioctl(sock, SIOCGIFCONF, &ifc);
00120          n = ifc.ifc_len / sizeof(struct ifreq);
00121 
00122          if(n >= max)
00123          {
00124                 delete buff;
00125 
00126                 max = max + 2;
00127                 buffSize = max * sizeof(struct ifreq);
00128                 buff = new char[buffSize];
00129          } else
00130          {
00131                 enough = true;
00132          }
00133 
00134         } while(!enough);
00135 
00136         for(ifr = ifc.ifc_req; n > 0; n--, ifr++)
00137         {
00138          if(ifr->ifr_addr.sa_family == AF_INET)
00139          {
00140                 std::string name = ifr->ifr_name;
00141                 IfConf::Pointer c(new IfConf);
00142 
00143                 d = (unsigned char *) ifr->ifr_ifru.ifru_addr.sa_data + 2;
00144                 snprintf(data, 16, "%i.%i.%i.%i", d[0], d[1], d[2], d[3]);
00145                 c->IP = data;
00146 
00147                 ioctl(sock, SIOCGIFBRDADDR, ifr);
00148                 d = (unsigned char *) ifr->ifr_ifru.ifru_broadaddr.sa_data + 2;
00149                 snprintf(data, 16, "%i.%i.%i.%i", d[0], d[1], d[2], d[3]);
00150                 c->BroadcastIP = data;
00151 
00152                 ioctl(sock, SIOCGIFNETMASK, ifr);
00153                 d = (unsigned char *) ifr->ifr_ifru.ifru_broadaddr.sa_data + 2;
00154                 snprintf(data, 16, "%i.%i.%i.%i", d[0], d[1], d[2], d[3]);
00155                 c->Netmask = data;
00156 
00157                 mIfConfs->add(name, c);
00158 
00159          }
00160         }
00161 
00162         close(sock);
00163 
00164   }
00165 
00166  /*----------------------------------------------------------------------*/
00167 
00168   std::string Network::getHostName()
00169   {
00170         char name[32];
00171 
00172         gethostname(name, 32);
00173 
00174         std::string result;
00175 
00176         result = name;
00177 
00178         return result;
00179   }
00180 
00181  /*----------------------------------------------------------------------*/
00182 
00183 }