PCMNetClient.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/PCMNetClient.h"
00030 
00031 namespace ParCompMark
00032 {
00033 
00034   //
00035   // Constructors & destructor
00036   //
00037 
00038   NetClient::NetClient(const std::string & name):
00039         // Parent initializer 
00040   Client(name                   /* Name of the class */
00041         )
00042   {
00043         getIP();
00044         mOwnIP = mIfConfs->get("eth0")->IP.c_str();
00045         initialize();
00046   }
00047 
00048  /*----------------------------------------------------------------------*/
00049 
00050   NetClient::~NetClient()
00051   {
00052         if(mInitialized)
00053         {
00054          finalize();
00055         }
00056   }
00057 
00058  /*----------------------------------------------------------------------*/
00059 
00060   //
00061 
00062   //
00063 
00064   void NetClient::initialize()
00065   {
00066 
00067         Assert(!mInitialized, INVALID_OPERATION_ERROR, "NetClient::initialize()");
00068 
00069         //broadcast recieve initialize
00070 
00071         struct sockaddr_in addr;
00072 
00073         if((mBroadcastSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
00074         {
00075          Except(INTERNAL_ERROR, "Network::initBroadcastRecieve", "Unable to create socket");
00076         }
00077 
00078         addr.sin_family = AF_INET;
00079         addr.sin_addr.s_addr = htonl(INADDR_ANY);
00080         addr.sin_port = htons(mBroadcastPort);
00081         memset(&(addr.sin_zero), '\0', 8);
00082 
00083         if(bind(mBroadcastSocket, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0)
00084         {
00085          Except(INTERNAL_ERROR, "NetClient::initialize()", "Unable to bind socket");
00086         }
00087 
00088         initThread(0, 0, true);
00089 
00090         mInitialized = true;
00091 
00092         Logger::getInstance()->log(Logger::NOTICE, "NetClient initialized.");
00093   }
00094 
00095  /*----------------------------------------------------------------------*/
00096 
00097   void NetClient::finalize()
00098   {
00099         Assert(mInitialized, INVALID_OPERATION_ERROR, "NetClient::finalize()");
00100 
00101         if(mBroadcastSocket != -1)
00102         {
00103          close(mBroadcastSocket);
00104          mBroadcastSocket = -1;
00105         }
00106 
00107         if(mStreamSocket != -1)
00108         {
00109          close(mStreamSocket);
00110          mStreamSocket = -1;
00111         }
00112 
00113         mInitialized = false;
00114 
00115   }
00116 
00117  /*----------------------------------------------------------------------*/
00118 
00119   void NetClient::task()
00120   {
00121 
00122         //recieve broadcast messages
00123 
00124         struct sockaddr_in addr;
00125 
00126         unsigned int addrLen = sizeof(struct sockaddr);
00127 
00128         int max = 100;
00129 
00130         char recvString[max + 1];
00131 
00132         int recvStringLen = -1;
00133 
00134         if((recvStringLen = recvfrom(mBroadcastSocket, recvString, max, 0, (struct sockaddr *) &addr, &addrLen)) < 0)
00135         {
00136          Except(INTERNAL_ERROR, "NetClient::task()", "Unable to recieve broadcast message");
00137         }
00138         //server IP, we have to answer for this IP address
00139 
00140         mServerIP = inet_ntoa(addr.sin_addr);
00141 
00142         recvString[recvStringLen] = '\0';
00143 
00144         std::string message = recvString;
00145 
00146         int index = message.find("|");
00147 
00148         std::string type = message.substr(0, index);
00149 
00150         std::string m = message.substr(index + 1);
00151         m = m.substr(0, m.length());
00152 
00153         mType = type;
00154         mMessage = m;
00155 
00156         handleMessage();
00157 
00158   }
00159 
00160  /*----------------------------------------------------------------------*/
00161 
00162 }