Rev 1124 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 999 | runge | 1 | /*************************************************************************** |
| 2 | * Copyright (C) January 4, 2009 by Mattias Runge * |
||
| 3 | * mattias@runge.se * |
||
| 4 | * server.cpp * |
||
| 5 | * * |
||
| 6 | * This program is free software; you can redistribute it and/or modify * |
||
| 7 | * it under the terms of the GNU General Public License as published by * |
||
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
||
| 9 | * (at your option) any later version. * |
||
| 10 | * * |
||
| 11 | * This program is distributed in the hope that it will be useful, * |
||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
| 14 | * GNU General Public License for more details. * |
||
| 15 | * * |
||
| 16 | * You should have received a copy of the GNU General Public License * |
||
| 17 | * along with this program; if not, write to the * |
||
| 18 | * Free Software Foundation, Inc., * |
||
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
| 20 | ***************************************************************************/ |
||
| 21 | |||
| 22 | #include "server.h" |
||
| 23 | |||
| 24 | Server::Server(int port, string name) |
||
| 25 | { |
||
| 26 | setSettings(port, name); |
||
| 27 | } |
||
| 28 | |||
| 29 | Server::~Server() |
||
| 30 | { |
||
| 31 | stop(); |
||
| 32 | |||
| 33 | for (map<int, AsyncSocket*>::iterator iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
||
| 34 | { |
||
| 35 | if (iter->second->isConnected()) |
||
| 36 | { |
||
| 37 | iter->second->stop(); |
||
| 38 | } |
||
| 39 | |||
| 40 | delete iter->second; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | void Server::setSettings(int port, string name) |
||
| 45 | { |
||
| 46 | myName = name; |
||
| 47 | mySocket.setPort(port); |
||
| 48 | } |
||
| 49 | |||
| 50 | void Server::run() |
||
| 51 | { |
||
| 1024 | runge | 52 | Logger &log = Logger::getInstance(); |
| 999 | runge | 53 | |
| 54 | try |
||
| 55 | { |
||
| 56 | mySocket.startListen(); |
||
| 57 | |||
| 1024 | runge | 58 | log.add(myName + " is listening for connections on port " + itos(mySocket.getPort()) + ".\n"); |
| 999 | runge | 59 | |
| 60 | while (true) |
||
| 61 | { |
||
| 62 | AsyncSocket *newSocket = new AsyncSocket(); |
||
| 63 | |||
| 64 | if (mySocket.accept(newSocket)) |
||
| 65 | { |
||
| 1122 | linlun | 66 | newSocket->sendData("Welcome to Atom " + ftos(VERSION) + " - " + myName + "\r\n"); |
| 999 | runge | 67 | |
| 1024 | runge | 68 | log.add(myName + " accepted new client with id " + itos(newSocket->getId()) + ".\n"); |
| 999 | runge | 69 | myClientSockets[newSocket->getId()] = newSocket; |
| 70 | myClientSockets[newSocket->getId()]->eventSetCallback(this); |
||
| 71 | myClientSockets[newSocket->getId()]->start(); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | catch (SocketException* e) |
||
| 76 | { |
||
| 1024 | runge | 77 | log.add(myName + " caught an exception:" + e->getDescription() + ".\n"); |
| 999 | runge | 78 | } |
| 79 | } |
||
| 80 | |||
| 81 | void Server::sendToAll(string data) |
||
| 82 | { |
||
| 1024 | runge | 83 | Logger &log = Logger::getInstance(); |
| 999 | runge | 84 | |
| 85 | for (map<int, AsyncSocket*>::iterator iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
||
| 86 | { |
||
| 87 | if (iter->second->isConnected()) |
||
| 88 | { |
||
| 1024 | runge | 89 | //log.add("Server: sent data to client " + itos(iter->second->getSocket()) + ".\n"; |
| 999 | runge | 90 | |
| 91 | try |
||
| 92 | { |
||
| 93 | iter->second->sendData(data); |
||
| 94 | } |
||
| 95 | catch (SocketException* e) |
||
| 96 | { |
||
| 1024 | runge | 97 | log.add(myName + " caught an exception:" + e->getDescription() + ".\n"); |
| 999 | runge | 98 | } |
| 99 | } |
||
| 100 | else |
||
| 101 | { |
||
| 1024 | runge | 102 | log.add(myName + " had a client disconnect.\n"); |
| 999 | runge | 103 | delete iter->second; |
| 104 | myClientSockets.erase(iter); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | void Server::sendTo(int id, string data) |
||
| 110 | { |
||
| 1024 | runge | 111 | Logger &log = Logger::getInstance(); |
| 999 | runge | 112 | |
| 113 | if (myClientSockets.find(id) != myClientSockets.end()) |
||
| 114 | { |
||
| 115 | if (myClientSockets[id]->isConnected()) |
||
| 116 | { |
||
| 1024 | runge | 117 | //log.add("Server: sent data to client " + itos(iter->second->getSocket()) + ".\n"; |
| 999 | runge | 118 | |
| 119 | try |
||
| 120 | { |
||
| 121 | myClientSockets[id]->sendData(data); |
||
| 122 | } |
||
| 123 | catch (SocketException* e) |
||
| 124 | { |
||
| 1024 | runge | 125 | log.add(myName + " caught an exception:" + e->getDescription() + ".\n"); |
| 999 | runge | 126 | } |
| 127 | } |
||
| 128 | else |
||
| 129 | { |
||
| 1124 | runge | 130 | log.add(myName + " had a client disconnect2.\n"); |
| 1122 | linlun | 131 | |
| 999 | runge | 132 | delete myClientSockets[id]; |
| 133 | myClientSockets.erase(id); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | void Server::disconnectClient(int id) |
||
| 139 | { |
||
| 1024 | runge | 140 | Logger &log = Logger::getInstance(); |
| 999 | runge | 141 | |
| 142 | if (myClientSockets.find(id) != myClientSockets.end()) |
||
| 143 | { |
||
| 1124 | runge | 144 | log.add(myName + " had client " + itos(id) + " disconnect!.\n"); |
| 1122 | linlun | 145 | |
| 1402 | runge | 146 | myClientSockets[id]->stop(); |
| 147 | |||
| 148 | AsyncSocket* socket = myClientSockets[id]; |
||
| 1124 | runge | 149 | myClientSockets.erase(id); |
| 1402 | runge | 150 | |
| 151 | delete socket; |
||
| 999 | runge | 152 | } |
| 153 | } |
||
| 154 | |||
| 155 | void Server::handleEvent(int id, SocketEvent socketEvent) |
||
| 156 | { |
||
| 157 | switch (socketEvent.getType()) |
||
| 158 | { |
||
| 159 | case SocketEvent::TYPE_CONNECTION_CLOSED: |
||
| 160 | case SocketEvent::TYPE_CONNECTION_DIED: |
||
| 161 | case SocketEvent::TYPE_CONNECTION_RESET: |
||
| 162 | disconnectClient(id); |
||
| 163 | break; |
||
| 164 | |||
| 165 | case SocketEvent::TYPE_DATA: |
||
| 166 | handleClientData(id, socketEvent.getData()); |
||
| 167 | break; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | void Server::handleClientData(int id, string data) |
||
| 172 | { |
||
| 1024 | runge | 173 | Logger &log = Logger::getInstance(); |
| 174 | log.add(myName + " received data from client " + itos(id) + ": \"" + data + "\"\n"); |
||
| 999 | runge | 175 | } |