NetworkAgent.cpp

Go to the documentation of this file.
00001 /*
00002  * This file is part of Micole Architecture
00003  *
00004  * Copyright (C) 2007 Micole partners
00005  *
00006  * Micole Architecture is free software: you can redistribute it 
00007  * and/or modify it under the terms of the GNU Lesser General 
00008  * Public License as published by the Free Software Foundation, 
00009  * either version 3 of the License, or (at your option) any 
00010  * later version.
00011  *
00012  * Micole Architecture is distributed in the hope that it will be 
00013  * useful, * but WITHOUT ANY WARRANTY; without even the implied 
00014  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00015  * PURPOSE.  See the GNU Lesser General Public License for more 
00016  * details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Micole Architecture.  If not, see
00020  * <http://www.gnu.org/licenses/>.
00021  */
00022 
00023 #include "stdafx.h"
00024 #include "NetworkAgent.h"
00025 #include "MicoleAgentThread.h"
00026 #include "MicoleStringStream.h"
00027 #include "ServerAgent.h"
00028 
00029 NetworkAgent::NetworkAgent(char * domain, int port)
00030 : MicoleAgentThread("NetworkAgent","NetworkAgent")
00031 {
00032         initSocket(domain, port);
00033         bindMessage( "^Net: (.*)$", BUS_CALLBACK_OF(NetworkAgent, handleNetworkMessage ));
00034         start(); //Start thread*/
00035 }
00036 
00037 void NetworkAgent::handleNetworkMessage ( MicoleBus *app, int argc, const char **argv )
00038 {
00039         string message = string(argv[0]);
00040         sendOnNetwork(message);
00041 }
00042 
00043 void NetworkAgent::initSocket(char * domain, int port) 
00044 {
00045         WSADATA WSAData;
00046         WSAStartup(MAKEWORD(2,0), &WSAData);
00047         
00048         SOCKADDR_IN sin;
00049         _sock = socket(AF_INET, SOCK_STREAM, 0);
00050         
00051         //host=(struct hostent*)gethostbyname(serverhost);
00052         
00053         sin.sin_addr.s_addr                     = inet_addr(domain);
00054         sin.sin_family                          = AF_INET;
00055         sin.sin_port                            = htons(port);
00056         int retCode = connect(_sock, (SOCKADDR *)&sin, sizeof(sin));
00057         
00058         if ((retCode==-1)&&(strcmp(domain,"127.0.0.1")==0)) //if error and server is localhost
00059         {
00060                 new ServerAgent(port);
00061                 ::Sleep(10);
00062                 initSocket(domain, port);
00063         }
00064 }
00065 
00066 NetworkAgent::~NetworkAgent()
00067 {}
00068 
00069 void NetworkAgent::run ()
00070 {
00071         char *buffer = new char[1024];
00072 
00073         while(getState()==ACTIVE)
00074         {
00075                 recv(_sock, buffer, 1023, 0);
00076                 OutputDebugStringA("In network: ");
00077                 OutputDebugStringA(buffer);
00078                 OutputDebugStringA("\n");
00079                 sendMessage(buffer);
00080                 //::Sleep(1);
00081         }
00082         closesocket(_sock);
00083         WSACleanup();
00084 }
00085 
00086 void NetworkAgent::sendOnNetwork(string s) 
00087 {
00088         send(_sock, (s+"\0").c_str(), (int(s.size())+1), 0);
00089 }
00090 
00091 
00095 void NetworkAgent::prepareToStop(int argc, const char **argv)
00096 {
00097 }
00098 
00099 void NetworkAgent::prepareToSuspend(int argc, const char **argv)
00100 {
00101 }

Generated on Tue Oct 16 17:10:43 2007 for Micole by  doxygen 1.4.7