Rev 1939 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1939 | Rev 1945 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2012 Mattias Runge |
3 | * Copyright (C) 2012 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "Manager.h" |
21 | #include "Manager.h" |
| 22 | 22 | ||
| 23 | #include <boost/lexical_cast.hpp> |
23 | #include <boost/lexical_cast.hpp> |
| 24 | #include <boost/algorithm/string/trim.hpp> |
24 | #include <boost/algorithm/string/trim.hpp> |
| 25 | 25 | ||
| 26 | #include "net/Manager.h" |
26 | #include "net/Manager.h" |
| 27 | 27 | ||
| 28 | namespace atom { |
28 | namespace atom { |
| 29 | namespace mbb { |
29 | namespace mbb { |
| 30 | 30 | ||
| 31 | Manager::Pointer Manager::instance_; |
31 | Manager::Pointer Manager::instance_; |
| 32 | 32 | ||
| 33 | logging::Logger Manager::LOG("mbb::Manager"); |
33 | logging::Logger Manager::LOG("mbb::Manager"); |
| 34 | 34 | ||
| 35 | Manager::Manager(unsigned int port) |
35 | Manager::Manager(unsigned int port) |
| 36 | { |
36 | { |
| 37 | try |
37 | try |
| 38 | { |
38 | { |
| 39 | this->server_id_ = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, port); |
39 | this->server_id_ = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, port); |
| 40 | LOG.Info("Started TCP server on port " + boost::lexical_cast<std::string>(port) + "."); |
40 | LOG.Info("Started TCP server on port " + boost::lexical_cast<std::string>(port) + "."); |
| 41 | LOG.Debug("Server id is " + boost::lexical_cast<std::string>(this->server_id_) + "."); |
41 | LOG.Debug("Server id is " + boost::lexical_cast<std::string>(this->server_id_) + "."); |
| 42 | } |
42 | } |
| 43 | catch (std::runtime_error& e) |
43 | catch (std::runtime_error& e) |
| 44 | { |
44 | { |
| 45 | LOG.Error(e.what()); |
45 | LOG.Error(e.what()); |
| 46 | } |
46 | } |
| 47 | } |
47 | } |
| 48 | 48 | ||
| 49 | Manager::Pointer Manager::Instance() |
49 | Manager::Pointer Manager::Instance() |
| 50 | { |
50 | { |
| 51 | return Manager::instance_; |
51 | return Manager::instance_; |
| 52 | } |
52 | } |
| 53 | 53 | ||
| 54 | void Manager::Create(unsigned int port) |
54 | void Manager::Create(unsigned int port) |
| 55 | { |
55 | { |
| 56 | Manager::instance_ = Manager::Pointer(new Manager(port)); |
56 | Manager::instance_ = Manager::Pointer(new Manager(port)); |
| 57 | } |
57 | } |
| 58 | 58 | ||
| 59 | void Manager::Delete() |
59 | void Manager::Delete() |
| 60 | { |
60 | { |
| 61 | Manager::instance_.reset(); |
61 | Manager::instance_.reset(); |
| 62 | } |
62 | } |
| 63 | 63 | ||
| 64 | Manager::~Manager() |
64 | Manager::~Manager() |
| 65 | { |
65 | { |
| 66 | net::Manager::Instance()->StopServer(this->server_id_); |
66 | net::Manager::Instance()->StopServer(this->server_id_); |
| 67 | this->server_id_ = 0; |
67 | this->server_id_ = 0; |
| 68 | } |
68 | } |
| 69 | 69 | ||
| 70 | 70 | ||
| 71 | void Manager::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, common::Byteset data) |
71 | void Manager::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, common::Byteset data) |
| 72 | { |
72 | { |
| 73 | if (server_id != this->server_id_) |
73 | if (server_id != this->server_id_) |
| 74 | { |
74 | { |
| 75 | return; |
75 | return; |
| 76 | } |
76 | } |
| 77 | 77 | ||
| 78 | std::string str = data.ToCharString(); |
78 | std::string str = data.ToCharString(); |
| 79 | 79 | ||
| 80 |
|
80 | std::cout << str << std::endl; |
| 81 | 81 | ||
| 82 | LOG. |
82 | //LOG.Info("Received: \"" + str + "\" from client " + boost::lexical_cast<std::string>(client_id) + " on server " + boost::lexical_cast<std::string>(server_id)); |
| 83 | 83 | ||
| 84 | if (str == "q" || str == "quit") |
84 | if (str == "q" || str == "quit") |
| 85 | { |
85 | { |
| 86 | LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has requested to be disconnected."); |
86 | LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has requested to be disconnected."); |
| 87 | net::Manager::Instance()->Disconnect(client_id); |
87 | net::Manager::Instance()->Disconnect(client_id); |
| 88 | } |
88 | } |
| 89 | } |
89 | } |
| 90 | 90 | ||
| 91 | void Manager::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state) |
91 | void Manager::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state) |
| 92 | { |
92 | { |
| 93 | if (server_id != this->server_id_) |
93 | if (server_id != this->server_id_) |
| 94 | { |
94 | { |
| 95 | return; |
95 | return; |
| 96 | } |
96 | } |
| 97 | 97 | ||
| 98 | if (client_state == net::CLIENT_STATE_DISCONNECTED) |
98 | if (client_state == net::CLIENT_STATE_DISCONNECTED) |
| 99 | { |
99 | { |
| 100 | LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has disconnected."); |
100 | LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has disconnected."); |
| 101 | } |
101 | } |
| 102 | else if (client_state == net::CLIENT_STATE_CONNECTED) |
102 | else if (client_state == net::CLIENT_STATE_CONNECTED) |
| 103 | { |
103 | { |
| 104 | LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has connected."); |
104 | LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has connected."); |
| 105 | } |
105 | } |
| 106 | else |
106 | else |
| 107 | { |
107 | { |
| 108 | LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state)); |
108 | LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state)); |
| 109 | } |
109 | } |
| 110 | } |
110 | } |
| 111 | 111 | ||
| 112 | }; // namespace mbb |
112 | }; // namespace mbb |
| 113 | }; // namespace atom |
113 | }; // namespace atom |
| 114 | 114 | ||