1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
39 #include <anna/core/tracing/Logger.hpp>
40 #include <anna/core/mt/Semaphore.hpp>
42 #include <anna/xml/Node.hpp>
43 #include <anna/xml/Attribute.hpp>
45 #include <anna/comm/Communicator.hpp>
46 #include <anna/comm/Handler.hpp>
47 #include <anna/comm/Transport.hpp>
52 //------------------------------------------------------------------------------------------------
53 // En modo MT aprovechamos las capacidades que tiene esta clase por el hecho de heredar de
54 // anna::Runnable -> invocaremos a do_action atraves de Runnnable::run.
56 // En modo ST invocamos directamente al Handler::apply.
57 //------------------------------------------------------------------------------------------------
58 void comm::Handler::do_action()
59 throw(RuntimeException) {
62 pollfd.events = POLLIN | POLLRDNORM;
63 const int npoll = poll(&pollfd, 1, 1000);
65 if(npoll == -1 && errno != EINTR) {
66 const int xerrno(errno);
67 string msg(asString());
68 msg += " | Error checking messages";
69 throw RuntimeException(msg, xerrno, ANNA_FILE_LOCATION);
74 if(supportTimeout() == false) {
75 if(npoll == 1 && (pollfd.revents & (POLLIN | POLLRDNORM)) != 0) {
81 } catch(RuntimeException& ex) {
86 * En la práctica este código sólo se ejecutará en modo MT, ya que en modo ST, sólo se invocará a Handler::do_action
87 * cuando se compruebe que actividad en el 'fd' asociado a este manejador.
89 * Sin embargo en MT el thread está ejecutando este código periódicamente.
91 else if(++ a_loop == 5) {
96 } catch(RuntimeException& ex) {
101 Microsecond now(anna::functions::hardwareClock());
103 if(npoll == 1 && (pollfd.revents & (POLLIN | POLLRDNORM)) != 0) {
109 } catch(RuntimeException& ex) {
112 } else if(isTimeout(now) == true) {
115 string msg(asString());
116 msg += " | Closed due to inactivity";
117 Logger::warning(msg, ANNA_FILE_LOCATION);
119 a_communicator->detach(this);
124 * Parece ser que puede ser que además de los POLLIN y POLLRDNORN que estamos comprobando
125 * pueden aparecer más bits, POLLINVAL (p.e) lo que provocaría
127 if(npoll == 1 && done == false) {
129 string msg(asString());
130 msg += " | Detected unsupported event";
131 Logger::warning(msg, ANNA_FILE_LOCATION);
133 a_communicator->detach(this);
137 std::string comm::Handler::asString() const
139 string msg("comm::Handler { ");
140 msg += Runnable::asString();
142 if(a_type == Type::Custom)
143 msg += " | Type: Custom";
145 msg += functions::asString(" | fd: %d", a_fd);
148 msg += " | Timeout: ";
149 msg += a_timeout.asString();
156 xml::Node* comm::Handler::asXML(xml::Node* parent) const
157 throw(RuntimeException) {
158 xml::Node* result = parent->createChild("comm.Handler");
163 void comm::Handler::asAttribute(xml::Node* node) const
164 throw(RuntimeException) {
165 node->createAttribute("Id", getId());
166 node->createAttribute("RequestStop", anna::functions::asString(hasRequestedStop()));
168 if(a_type == Type::Custom)
169 node->createAttribute("Type", "Custom");
171 node->createAttribute("fd", a_fd);
174 node->createAttribute("Timeout", a_timeout / 1000);
176 node->createAttribute("Timeout", "none");