Subversion Repositories HomeAutomation

Rev

Rev 999 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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