Remove warnings
[anna.git] / source / comm / Host.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <anna/core/tracing/TraceMethod.hpp>
10
11 #include <anna/xml/Node.hpp>
12
13 #include <anna/comm/Host.hpp>
14 #include <anna/comm/Server.hpp>
15 #include <anna/comm/Communicator.hpp>
16 #include <anna/comm/ClientSocket.hpp>
17 #include <anna/comm/Device.hpp>
18 #include <anna/comm/ServerAllocator.hpp>
19
20 using namespace std;
21 using namespace anna;
22
23 comm::Host::~Host() {
24   for(server_iterator ii = server_begin(), maxii = server_end(); ii != maxii; ii ++)
25     delete server(ii);
26
27   a_servers.clear();
28   a_devices.clear();
29 }
30
31 int comm::Host::SortBy::value(const Server* server)
32 throw() {
33   return server->getRemotePort();
34 }
35
36
37 const comm::Server* comm::Host::find_server(const int remotePort) const
38 throw() {
39   return a_servers.find(remotePort);
40 }
41
42 comm::Server* comm::Host::find_server(const int remotePort)
43 throw() {
44   return a_servers.find(remotePort);
45 }
46
47
48 comm::Server* comm::Host::createServer(const string& name, const int remotePort, const bool autoRecovery, comm::TransportFactory* transportFactory, const bool ignoreIncomingMessages, const bool doConnect)
49 throw(RuntimeException) {
50   return add(createServer(ServerAllocator(name, *this, remotePort, autoRecovery, transportFactory, ignoreIncomingMessages)), remotePort, doConnect);
51 }
52
53 //-------------------------------------------------------------------------------
54 // (1) Acelera las posibilidades principales de busqueda => por nombre y por
55 // puerto remoto en que esta atendiendo peticiones.
56 //
57 // Ojo!! Siempre devuelve un Server aunque posiblemente no este conectado al
58 // ServerSocket remoto.
59 //-------------------------------------------------------------------------------
60 comm::Server* comm::Host::createServer(const ServerAllocator& serverAllocator)
61 throw(RuntimeException) {
62   Server* result = serverAllocator.apply();
63   result->setIgnoreIncomingMessages(serverAllocator.getIgnoreIncomingMessages());
64   return result;
65 }
66
67 void comm::Host::assign(const Device* device)
68 throw(RuntimeException) {
69   if(contains(device) == false)
70     a_devices.push_back(device);
71 }
72
73
74 string comm::Host::asString() const
75 throw() {
76   string result;
77   result = "comm::Host { Name: ";
78   result += getName();
79   result += " | Devices: ";
80
81   if(a_devices.empty() == true)
82     result += "(null)";
83   else {
84     for(const_device_iterator ii = device_begin(), maxii = device_end(); ii != maxii; ii ++) {
85       result += device(ii)->asString();
86       result += " ";
87     }
88   }
89
90   return result += " }";
91 }
92
93
94 xml::Node* comm::Host::asXML(xml::Node* parent) const
95 throw(RuntimeException) {
96   xml::Node* host = parent->createChild("comm.Host");
97   xml::Node* node;
98   host->createAttribute("Name", getName());
99   node = host->createChild("Devices");
100
101   for(const_device_iterator ii = device_begin(), maxii = device_end(); ii != maxii; ii ++)
102     device(ii)->asXML(node);
103
104   node = host->createChild("Servers");
105
106   for(const_server_iterator ss = server_begin(), maxss = server_end(); ss != maxss; ss ++)
107     server(ss)->asXML(node);
108
109   return host;
110 }
111
112
113 comm::Server* comm::Host::add(comm::Server* result, const int remotePort, const bool doConnect)
114 throw() {
115   result->a_sequence = a_servers.size();
116   a_servers.add(result);
117
118   try {
119     LOGINFORMATION(
120       string msg("comm::Host::add");
121       msg += (doConnect ? "(server creation and connection) | " : "(server creation, but connection not performed now) | ");
122       msg += result->asString();
123       Logger::information(msg,  ANNA_FILE_LOCATION)
124     );
125
126     if(doConnect) result->connect();
127   } catch(RuntimeException& ex) {
128     ex.trace();
129   }
130
131   return result;
132 }
133