d2f7aaec8b7acb4420641e841b8217c0cde2aeac
[anna.git] / source / comm / handler / ClientSocket.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #include <anna/core/tracing/Logger.hpp>
10 #include <anna/core/functions.hpp>
11 #include <anna/core/mt/Guard.hpp>
12
13 #include <anna/xml/Node.hpp>
14 #include <anna/xml/Attribute.hpp>
15
16 #include <anna/comm/ClientSocket.hpp>
17 #include <anna/comm/Communicator.hpp>
18 #include <anna/comm/Device.hpp>
19 #include <anna/comm/Receiver.hpp>
20
21 #include <anna/comm/handler/ClientSocket.hpp>
22
23 using namespace std;
24 using namespace anna;
25
26 void comm::handler::ClientSocket::initialize()
27 noexcept(false) {
28   if(a_clientSocket == NULL) {
29     string msg(asString());
30     msg += " | comm::ClientSocket was not established";
31     throw RuntimeException(msg, ANNA_FILE_LOCATION);
32   }
33
34   if(a_clientSocket->isConnected() == false)
35     a_clientSocket->connect();
36
37   setfd(a_clientSocket->getfd());
38 }
39
40 /*
41  * Se invoca desde Communicator::detach.
42  *
43  * [Tx] -> Communicator
44  */
45 void comm::handler::ClientSocket::finalize()
46 {
47   if(a_clientSocket == NULL)
48     return;
49
50   Receiver* receiver;
51
52   if((receiver = a_clientSocket->getReceiver()) != NULL)
53     receiver->eventBreakConnection(*a_clientSocket);
54
55   a_clientSocket->close();
56   a_communicator->eventBreakConnection(*a_clientSocket);
57   a_clientSocket = NULL;
58 }
59
60 void comm::handler::ClientSocket::clone()
61 noexcept(false) {
62   if(a_clientSocket->isConnected() == true)
63     a_clientSocket->close();
64
65   a_clientSocket->connect();
66   const int fd = a_clientSocket->getfd();
67
68   if(fd == -1) {
69     string msg("comm::handler::ClientSocket::clone | ");
70     msg += asString();
71     msg += " | Cloning error";
72     throw RuntimeException(msg, ANNA_FILE_LOCATION);
73   }
74
75   setfd(fd);
76 }
77
78 string comm::handler::ClientSocket::asString() const
79 {
80   string result("comm::handler::ClientSocket { ");
81   result += comm::Handler::asString();
82   result += " | ";
83   result += functions::asString(a_clientSocket);
84   return result += " }";
85 }
86
87 xml::Node* comm::handler::ClientSocket::asXML(xml::Node* parent) const
88 {
89   xml::Node* result = parent->createChild("comm.handler.ClientSocket");
90   comm::Handler::asAttribute(result);
91
92   if(a_clientSocket)
93     a_clientSocket->asXML(result);
94
95   return result;
96 }
97
98