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/mt/Guard.hpp>
13 #include <anna/xml/Node.hpp>
14 #include <anna/xml/Attribute.hpp>
16 #include <anna/comm/DatagramSocket.hpp>
17 #include <anna/comm/Communicator.hpp>
18 #include <anna/comm/Device.hpp>
19 #include <anna/comm/Transport.hpp>
20 #include <anna/comm/Receiver.hpp>
22 #include <anna/comm/handler/DatagramSocket.hpp>
27 void comm::handler::DatagramSocket::initialize()
28 throw(RuntimeException) {
29 if(a_datagramSocket == NULL) {
30 string msg(asString());
31 msg += " | comm::DatagramSocket was not established";
32 throw RuntimeException(msg, ANNA_FILE_LOCATION);
35 if(a_datagramSocket->isConnected() == false)
36 a_datagramSocket->connect();
38 setfd(a_datagramSocket->getfd());
41 comm::ClientSocket* comm::handler::DatagramSocket::getClientSocket()
43 return a_datagramSocket;
46 void comm::handler::DatagramSocket::apply()
47 throw(RuntimeException) {
48 Guard guard(a_datagramSocket, "anna::comm::DatagramSocket from anna::comm::handler::DatagramSocket::apply");
49 comm::DatagramSocket::Notify::_v notify = a_datagramSocket->receive();
51 if(notify == comm::DatagramSocket::Notify::Corrupt) {
52 a_datagramSocket->forgot();
56 if(notify != comm::DatagramSocket::Notify::ReceiveData)
59 const Message* message;
60 const DataBlock* dataBlock;
61 Transport* transport = a_datagramSocket->getTransport();
62 Receiver* receiver = a_datagramSocket->getReceiver();
64 while((dataBlock = a_datagramSocket->fetch()) != NULL && canContinue()) {
67 message = transport->decode(*dataBlock);
68 } catch(RuntimeException& ex) {
69 a_datagramSocket->forgot();
76 a_communicator->eventReceiveMessage(*a_datagramSocket, *message);
78 receiver->apply(*a_datagramSocket, *message);
79 } catch(RuntimeException& ex) {
86 * Se invoca desde Communicator::detach.
88 * [Tx] -> Communicator
90 void comm::handler::DatagramSocket::finalize()
92 if(a_datagramSocket == NULL)
95 a_datagramSocket->close();
96 a_datagramSocket = NULL;
99 string comm::handler::DatagramSocket::asString() const
101 string result("comm::handler::DatagramSocket { ");
102 result += comm::Handler::asString();
104 result += functions::asString(a_datagramSocket);
105 return result += " }";
108 xml::Node* comm::handler::DatagramSocket::asXML(xml::Node* parent) const
110 xml::Node* result = parent->createChild("comm.handler.DatagramSocket");
111 comm::Handler::asAttribute(result);
114 a_datagramSocket->asXML(result);