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
37 #include <anna/core/tracing/Logger.hpp>
38 #include <anna/core/functions.hpp>
39 #include <anna/core/mt/Guard.hpp>
41 #include <anna/xml/Node.hpp>
42 #include <anna/xml/Attribute.hpp>
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>
50 #include <anna/comm/handler/DatagramSocket.hpp>
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);
63 if(a_datagramSocket->isConnected() == false)
64 a_datagramSocket->connect();
66 setfd(a_datagramSocket->getfd());
69 comm::ClientSocket* comm::handler::DatagramSocket::getClientSocket()
71 return a_datagramSocket;
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();
79 if(notify == comm::DatagramSocket::Notify::Corrupt) {
80 a_datagramSocket->forgot();
84 if(notify != comm::DatagramSocket::Notify::ReceiveData)
87 const Message* message;
88 const DataBlock* dataBlock;
89 Transport* transport = a_datagramSocket->getTransport();
90 Receiver* receiver = a_datagramSocket->getReceiver();
92 while((dataBlock = a_datagramSocket->fetch()) != NULL && canContinue()) {
95 message = transport->decode(*dataBlock);
96 } catch(RuntimeException& ex) {
97 a_datagramSocket->forgot();
104 a_communicator->eventReceiveMessage(*a_datagramSocket, *message);
106 receiver->apply(*a_datagramSocket, *message);
107 } catch(RuntimeException& ex) {
114 * Se invoca desde Communicator::detach.
116 * [Tx] -> Communicator
118 void comm::handler::DatagramSocket::finalize()
120 if(a_datagramSocket == NULL)
123 a_datagramSocket->close();
124 a_datagramSocket = NULL;
127 string comm::handler::DatagramSocket::asString() const
129 string result("comm::handler::DatagramSocket { ");
130 result += comm::Handler::asString();
132 result += functions::asString(a_datagramSocket);
133 return result += " }";
136 xml::Node* comm::handler::DatagramSocket::asXML(xml::Node* parent) const
138 xml::Node* result = parent->createChild("comm.handler.DatagramSocket");
139 comm::Handler::asAttribute(result);
142 a_datagramSocket->asXML(result);