1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
9 #include <anna/core/tracing/Logger.hpp>
10 #include <anna/core/functions.hpp>
11 #include <anna/core/tracing/TraceMethod.hpp>
13 #include <anna/xml/Node.hpp>
14 #include <anna/xml/Attribute.hpp>
16 #include <anna/comm/ServerSocket.hpp>
17 #include <anna/comm/Communicator.hpp>
18 #include <anna/comm/internal/LocalConnection.hpp>
19 #include <anna/comm/ClientSocket.hpp>
20 #include <anna/comm/Device.hpp>
22 #include <anna/comm/handler/ServerSocket.hpp>
23 #include <anna/comm/handler/Manager.hpp>
28 void comm::handler::ServerSocket::initialize()
30 LOGMETHOD(TraceMethod traceMethod("comm::handler::ServerSocket", "initialize", ANNA_FILE_LOCATION));
32 if(a_serverSocket == NULL) {
33 string msg(asString());
34 msg += " | comm::ServerSocket was not established";
35 throw RuntimeException(msg, ANNA_FILE_LOCATION);
38 if(a_serverSocket->isBound() == false)
39 a_serverSocket->bind();
41 setfd(a_serverSocket->getfd());
42 a_serverSocket->prepare();
43 a_serverSocket->setBlockingMode(false);
47 // Cuando un ServerSocket recibe actividad en el fd sólo puede ser para
48 // recibir una solicitud de conexión de un proceso remoto, que estará
49 // instanciando un ClientSocket contra este.
51 // (1) Esta situacion se ha dado cuando hemos probado varios servidores
52 // en modo compartido.
54 // Se invoca desde [Tx] -> <null>
55 // La secuencia de bloqueo tiene que coincidir con la del método comm::handler::LocalConnection::finalize:
57 <Method Id="comm::handler::MetaClientSocket::apply" File="handler.db/comm.handler.MetaClientSocket.cc" Line="45" LineNo="281">
58 <Guard Id="0x68ccd0" Name="comm::ClientSocket from comm::handler::MetaClientSocket::apply" LineNo="283"/>
59 <Guard Id="0x68c6a0" Name="comm::Communicator::detach" LineNo="289">
60 <Method Id="comm::handler::LocationConnection::finalize" File="handler.db/comm.handler.LocalConnection.cc" Line="78" LineNo="293">
61 <Guard Id="0x68ccd0" Name="comm::Socket::close" LineNo="295">
62 <Guard Id="0x6818e0" Name="PN7anna4comm16TransportFactoryE" LineNo="297"/>
64 <Guard Id="0x68c678" Name="N7anna12SafeRecyclerINS_4comm15LocalConnectionENS_9AllocatorIS2_EEEE::release (T*)" LineNo="305"/>
69 // para eso bloqueamos el comm::Communicator.
71 void comm::handler::ServerSocket::apply()
73 LOGMETHOD(TraceMethod traceMethod("handler::ServerSocket", "apply", ANNA_FILE_LOCATION));
74 Guard guard(a_communicator, "Communicator from handler::ServerSocket");
75 comm::LocalConnection* localConnection = a_serverSocket->accept();
77 if(localConnection == NULL) // (1)
80 bool accept = a_communicator->eventAcceptConnection(*localConnection->getClientSocket());
82 // @Eduardo (independence from communicator)
83 if(accept) accept = a_serverSocket->eventAcceptConnection(*localConnection->getClientSocket());
85 // Ver comentarios adicionales en void comm::handler::LocalConnection::finalize ()
89 string msg(localConnection->asString());
90 msg += " | Service denied";
91 Logger::warning(msg, ANNA_FILE_LOCATION);
93 localConnection->getClientSocket()->close();
94 a_serverSocket->release(localConnection);
97 a_communicator->attach(localConnection);
98 } catch(RuntimeException&) {
99 localConnection->getClientSocket()->close();
100 a_serverSocket->release(localConnection);
106 //--------------------------------------------------------------------------
107 // Si cae la dirección en la que está escuchando finalizamos todos los
108 // que tengan establecidos contra este ServerSocket.
109 //--------------------------------------------------------------------------
110 void comm::handler::ServerSocket::breakAddress(const in_addr_t& address)
112 LOGMETHOD(TraceMethod traceMethod("comm::handler::ServerSocket", "breakAddress", ANNA_FILE_LOCATION));
113 const comm::AccessPoint& accessPoint = a_serverSocket->getLocalAccessPoint();
114 const comm::Device* device = accessPoint.getINetAddress().getDevice(false);
119 if(device->getAddress() != address)
122 Guard guard(a_serverSocket, "comm::handler::ServerSocket::breakAddress");
124 * Trabaja sobre una copia para no perder la referencia cuando se elimine un miembro de la lista original
126 typedef vector <comm::LocalConnection*> work_container;
127 typedef work_container::iterator work_iterator;
130 for(comm::ServerSocket::iterator ii = a_serverSocket->begin(), maxii = a_serverSocket->end(); ii != maxii; ii ++)
131 ww.push_back(comm::ServerSocket::localConnection(ii));
135 for(work_iterator ii = ww.begin(), maxii = ww.end(); ii != maxii; ii ++) {
136 a_communicator->detach((*ii)->getClientSocket());
141 string msg("comm::handler::ServerSocket::breakAddress | ");
142 msg += a_serverSocket->asString();
143 msg += " | Clients number: ";
144 msg += functions::asString(n);
145 Logger::debug(msg, ANNA_FILE_LOCATION);
150 * No cierra las conexiones locales que hayan partido desde este ServerSocket, porque tambien cerriamos el
151 * MainHandler cuando se use el Communicator::Clone.
153 void comm::handler::ServerSocket::finalize()
155 LOGMETHOD(TraceMethod traceMethod("comm::handler::ServerSocket", "finalize", ANNA_FILE_LOCATION));
157 if(a_serverSocket == NULL)
160 for(comm::ServerSocket::iterator ii = a_serverSocket->begin(), maxii = a_serverSocket->end(); ii != maxii; ii ++)
161 comm::ServerSocket::localConnection(ii)->setServerSocket(NULL);
163 a_serverSocket->close();
164 a_serverSocket = NULL;
167 string comm::handler::ServerSocket::asString() const
169 string result("comm::handler::ServerSocket { ");
170 result += comm::Handler::asString();
172 result += functions::asString(a_serverSocket);
173 return result += " }";
176 xml::Node* comm::handler::ServerSocket::asXML(xml::Node* parent) const
178 xml::Node* result = parent->createChild("comm.handler.ServerSocket");
179 comm::Handler::asAttribute(result);
182 a_serverSocket->asXML(result);