Updated license
[anna.git] / source / comm / Handler.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <poll.h>
38
39 #include <anna/core/tracing/Logger.hpp>
40 #include <anna/core/mt/Semaphore.hpp>
41
42 #include <anna/xml/Node.hpp>
43 #include <anna/xml/Attribute.hpp>
44
45 #include <anna/comm/Communicator.hpp>
46 #include <anna/comm/Handler.hpp>
47 #include <anna/comm/Transport.hpp>
48
49 using namespace std;
50 using namespace anna;
51
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.
55 //
56 // En modo ST invocamos directamente al Handler::apply.
57 //------------------------------------------------------------------------------------------------
58 void comm::Handler::do_action()
59 throw(RuntimeException) {
60   pollfd pollfd;
61   pollfd.fd = getfd();
62   pollfd.events = POLLIN | POLLRDNORM;
63   const int npoll = poll(&pollfd, 1, 1000);
64
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);
70   }
71
72   bool done(false);
73
74   if(supportTimeout() == false) {
75     if(npoll == 1 && (pollfd.revents & (POLLIN | POLLRDNORM)) != 0) {
76       done = true;
77       a_loop = 0;
78
79       try {
80         apply();
81       } catch(RuntimeException& ex) {
82         ex.trace();
83       }
84     }
85     /*
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.
88      *
89      * Sin embargo en MT el thread está ejecutando este código periódicamente.
90      */
91     else if(++ a_loop == 5) {
92       a_loop = 0;
93
94       try {
95         testClose();
96       } catch(RuntimeException& ex) {
97         ex.trace();
98       }
99     }
100   } else {
101     Microsecond now(anna::functions::hardwareClock());
102
103     if(npoll == 1 && (pollfd.revents & (POLLIN | POLLRDNORM)) != 0) {
104       done = true;
105
106       try {
107         beat(now);
108         apply();
109       } catch(RuntimeException& ex) {
110         ex.trace();
111       }
112     } else if(isTimeout(now) == true) {
113       done = true;
114       LOGWARNING(
115         string msg(asString());
116         msg += " | Closed due to inactivity";
117         Logger::warning(msg, ANNA_FILE_LOCATION);
118       );
119       a_communicator->detach(this);
120     }
121   }
122
123   /**
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
126    */
127   if(npoll == 1 && done == false) {
128     LOGWARNING(
129       string msg(asString());
130       msg += " | Detected unsupported event";
131       Logger::warning(msg, ANNA_FILE_LOCATION);
132     )
133     a_communicator->detach(this);
134   }
135 }
136
137 std::string comm::Handler::asString() const
138 throw() {
139   string msg("comm::Handler { ");
140   msg += Runnable::asString();
141
142   if(a_type == Type::Custom)
143     msg += " | Type: Custom";
144
145   msg += functions::asString(" | fd: %d", a_fd);
146
147   if(a_timeout > 0) {
148     msg += " | Timeout: ";
149     msg += a_timeout.asString();
150     msg += " ms";
151   }
152
153   return msg += " }";
154 }
155
156 xml::Node* comm::Handler::asXML(xml::Node* parent) const
157 throw(RuntimeException) {
158   xml::Node* result = parent->createChild("comm.Handler");
159   asAttribute(result);
160   return result;
161 }
162
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()));
167
168   if(a_type == Type::Custom)
169     node->createAttribute("Type", "Custom");
170
171   node->createAttribute("fd", a_fd);
172
173   if(supportTimeout())
174     node->createAttribute("Timeout", a_timeout / 1000);
175   else
176     node->createAttribute("Timeout", "none");
177 }
178