First commit
[anna.git] / source / comm / handler / DatagramSocket.cpp
1 // ANNA - Anna is Not 'N' 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 <anna/core/tracing/Logger.hpp>
38 #include <anna/core/functions.hpp>
39 #include <anna/core/mt/Guard.hpp>
40
41 #include <anna/xml/Node.hpp>
42 #include <anna/xml/Attribute.hpp>
43
44 #include <anna/comm/DatagramSocket.hpp>
45 #include <anna/comm/Communicator.hpp>
46 #include <anna/comm/Device.hpp>
47 #include <anna/comm/Transport.hpp>
48 #include <anna/comm/Receiver.hpp>
49
50 #include <anna/comm/handler/DatagramSocket.hpp>
51
52 using namespace std;
53 using namespace anna;
54
55 void comm::handler::DatagramSocket::initialize()
56 throw(RuntimeException) {
57   if(a_datagramSocket == NULL) {
58     string msg(asString());
59     msg += " | comm::DatagramSocket was not established";
60     throw RuntimeException(msg, ANNA_FILE_LOCATION);
61   }
62
63   if(a_datagramSocket->isConnected() == false)
64     a_datagramSocket->connect();
65
66   setfd(a_datagramSocket->getfd());
67 }
68
69 comm::ClientSocket* comm::handler::DatagramSocket::getClientSocket()
70 throw() {
71   return a_datagramSocket;
72 }
73
74 void comm::handler::DatagramSocket::apply()
75 throw(RuntimeException) {
76   Guard guard(a_datagramSocket, "anna::comm::DatagramSocket from anna::comm::handler::DatagramSocket::apply");
77   comm::DatagramSocket::Notify::_v notify = a_datagramSocket->receive();
78
79   if(notify == comm::DatagramSocket::Notify::Corrupt) {
80     a_datagramSocket->forgot();
81     return;
82   }
83
84   if(notify != comm::DatagramSocket::Notify::ReceiveData)
85     return;
86
87   const Message* message;
88   const DataBlock* dataBlock;
89   Transport* transport = a_datagramSocket->getTransport();
90   Receiver* receiver = a_datagramSocket->getReceiver();
91
92   while((dataBlock = a_datagramSocket->fetch()) != NULL && canContinue()) {
93     try {
94       try {
95         message = transport->decode(*dataBlock);
96       } catch(RuntimeException& ex) {
97         a_datagramSocket->forgot();
98         ex.trace();
99         message = NULL;
100         break;
101       }
102
103       if(receiver == NULL)
104         a_communicator->eventReceiveMessage(*a_datagramSocket, *message);
105       else
106         receiver->apply(*a_datagramSocket, *message);
107     } catch(RuntimeException& ex) {
108       ex.trace();
109     }
110   }
111 }
112
113 /*
114  * Se invoca desde Communicator::detach.
115  *
116  * [Tx] -> Communicator
117  */
118 void comm::handler::DatagramSocket::finalize()
119 throw() {
120   if(a_datagramSocket == NULL)
121     return;
122
123   a_datagramSocket->close();
124   a_datagramSocket = NULL;
125 }
126
127 string comm::handler::DatagramSocket::asString() const
128 throw() {
129   string result("comm::handler::DatagramSocket { ");
130   result += comm::Handler::asString();
131   result += " | ";
132   result += functions::asString(a_datagramSocket);
133   return result += " }";
134 }
135
136 xml::Node* comm::handler::DatagramSocket::asXML(xml::Node* parent) const
137 throw() {
138   xml::Node* result = parent->createChild("comm.handler.DatagramSocket");
139   comm::Handler::asAttribute(result);
140
141   if(a_datagramSocket)
142     a_datagramSocket->asXML(result);
143
144   return result;
145 }
146
147