ServerAgent.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 "ServerAgent.h"
00025 #include "MicoleAgentThread.h"
00026 #include "MicoleStringStream.h"
00027 
00028 #include "DServerAgent.h"
00029 
00030 
00031 ServerAgent::ServerAgent(int port)
00032 : MicoleAgentThread("ServerAgent","ServerAgent")
00033 {
00034         _port = port;
00035         start(); //Start thread*/
00036 }
00037 
00038 ServerAgent::~ServerAgent()
00039 {}
00040 
00041 void ServerAgent::run ()
00042 {
00043         WSADATA WSAData;
00044         WSAStartup(MAKEWORD(2,0), &WSAData);
00045         SOCKET sock;
00046         SOCKET csock;
00047         SOCKADDR_IN sin;
00048         SOCKADDR_IN csin;
00049         sock = socket(AF_INET, SOCK_STREAM, 0);
00050         sin.sin_addr.s_addr                     = INADDR_ANY;
00051         sin.sin_family                          = AF_INET;
00052         sin.sin_port                            = htons(_port);
00053         bind(sock, (SOCKADDR *)&sin, sizeof(sin));
00054         listen(sock, 0);
00055         while(getState()==ACTIVE)
00056         {
00057                 int sinsize = sizeof(csin);
00058                 if((csock = accept(sock, (SOCKADDR *)&csin, &sinsize)) != INVALID_SOCKET)
00059                         _DServList.push_back(new DServerAgent(csock,this));
00060         }
00061 }
00062 
00063 void ServerAgent::sendOnAllClients(string s) 
00064 {
00065         for (list<DServerAgent*>::iterator k = _DServList.begin() ; k != _DServList.end() ; k++) 
00066                 (*k)->sendOnNetwork(s);
00067 }
00068 
00069 
00073 void ServerAgent::prepareToStop(int argc, const char **argv)
00074 {
00075 }
00076 
00077 void ServerAgent::prepareToSuspend(int argc, const char **argv)
00078 {
00079 }

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