00001 // 00002 // Copyright 2004 Andras Varga 00003 // 00004 // This library is free software, you can redistribute it and/or modify 00005 // it under the terms of the GNU Lesser General Public License 00006 // as published by the Free Software Foundation; 00007 // either version 2 of the License, or any later version. 00008 // The library is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00011 // See the GNU Lesser General Public License for more details. 00012 // 00013 00014 00015 #include "TCPSrvHostApp.h" 00016 00017 00018 Define_Module(TCPSrvHostApp); 00019 00020 00021 void TCPSrvHostApp::initialize() 00022 { 00023 const char *address = par("address"); 00024 int port = par("port"); 00025 00026 serverSocket.setOutputGate(gate("tcpOut")); 00027 serverSocket.bind(address[0] ? IPvXAddress(address) : IPvXAddress(), port); 00028 serverSocket.listen(); 00029 } 00030 00031 void TCPSrvHostApp::updateDisplay() 00032 { 00033 if (!ev.isGUI()) return; 00034 00035 char buf[32]; 00036 sprintf(buf, "%d threads", socketMap.size()); 00037 getDisplayString().setTagArg("t", 0, buf); 00038 } 00039 00040 void TCPSrvHostApp::handleMessage(cMessage *msg) 00041 { 00042 if (msg->isSelfMessage()) 00043 { 00044 TCPServerThreadBase *thread = (TCPServerThreadBase *)msg->getContextPointer(); 00045 thread->timerExpired(msg); 00046 } 00047 else 00048 { 00049 TCPSocket *socket = socketMap.findSocketFor(msg); 00050 if (!socket) 00051 { 00052 // new connection -- create new socket object and server process 00053 socket = new TCPSocket(msg); 00054 socket->setOutputGate(gate("tcpOut")); 00055 00056 const char *serverThreadClass = par("serverThreadClass"); 00057 TCPServerThreadBase *proc = check_and_cast<TCPServerThreadBase *>(createOne(serverThreadClass)); 00058 00059 socket->setCallbackObject(proc); 00060 proc->init(this, socket); 00061 00062 socketMap.addSocket(socket); 00063 00064 updateDisplay(); 00065 } 00066 socket->processMessage(msg); 00067 } 00068 } 00069 00070 void TCPSrvHostApp::finish() 00071 { 00072 } 00073 00074 void TCPSrvHostApp::removeThread(TCPServerThreadBase *thread) 00075 { 00076 // remove socket 00077 socketMap.removeSocket(thread->getSocket()); 00078 00079 // remove thread object 00080 delete thread; 00081 00082 updateDisplay(); 00083 } 00084 00085