Remove dynamic exceptions
[anna.git] / source / comm / AccessPoint.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 <strings.h>
10 #include <sys/socket.h>
11
12 #include <anna/xml/Node.hpp>
13
14 #include <anna/comm/AccessPoint.hpp>
15 #include <anna/comm/Socket.hpp>
16 #include <anna/comm/Device.hpp>
17
18 using namespace std;
19 using namespace anna;
20
21 bool comm::AccessPoint::operator == (const comm::AccessPoint& rhs) const
22 {
23   if(rhs.isNull() && this->isNull())
24     return true;
25
26   if(a_path != NULL && rhs.a_path != NULL)
27     return *a_path == *rhs.a_path;
28
29   if(a_inetAddress.isNull() == false && rhs.a_inetAddress.isNull() == false)
30     return a_inetAddress == rhs.a_inetAddress;
31
32   return false;
33 }
34
35 comm::AccessPoint& comm::AccessPoint::operator = (const INetAddress & inetAddress)
36 {
37   if(a_path != NULL) {
38     delete a_path;
39     a_path = NULL;
40   }
41
42   a_inetAddress = inetAddress;
43   return *this;
44 }
45
46 comm::AccessPoint& comm::AccessPoint::operator = (const std::string & path)
47 {
48   if(a_path == &path)
49     return *this;
50
51   a_inetAddress.clear();
52
53   if(a_path == NULL)
54     a_path = new std::string(path);
55   else
56     *a_path = path;
57
58   return *this;
59 }
60
61 comm::AccessPoint& comm::AccessPoint::operator = (const AccessPoint & rhs)
62 {
63   if(this == &rhs)
64     return *this;
65
66   if(rhs.isNull()) {
67     a_inetAddress.clear();
68
69     if(a_path != NULL) {
70       delete a_path;
71       a_path = NULL;
72     }
73
74     return *this;
75   }
76
77   return (rhs.a_path != NULL) ? operator= (*rhs.a_path) : operator= (rhs.a_inetAddress);
78 }
79
80 void comm::AccessPoint::translate(const comm::Socket& socket, sockaddr*& s, int& len)
81 noexcept(false) {
82   switch(socket.getDomain()) {
83   case Socket::Domain::Inet:
84
85     if(a_inetAddress.isNull()  == true) {
86       string msg(socket.asString());
87       msg += " | Cannot attach INET socket without address";
88       throw RuntimeException(msg, ANNA_FILE_LOCATION);
89     }
90
91     anna_memset(&a_sockaddr_in, 0, sizeof(a_sockaddr_in));
92     a_sockaddr_in.sin_family = AF_INET;
93     a_sockaddr_in.sin_port = htons(a_inetAddress.getPort());
94     a_sockaddr_in.sin_addr.s_addr = (in_addr_t) a_inetAddress.getDevice()->getAddress();
95     s = (sockaddr*) & a_sockaddr_in;
96     len = sizeof(a_sockaddr_in);
97     break;
98   case Socket::Domain::Unix:
99
100     if(a_path  == NULL) {
101       string msg(socket.asString());
102       msg += "Cannot attach UNIX socket without file";
103       throw RuntimeException(msg, ANNA_FILE_LOCATION);
104     }
105
106     s = (sockaddr*) & a_sockaddr_un;
107     bzero(s, len = sizeof(a_sockaddr_un));
108     a_sockaddr_un.sun_family = AF_UNIX;
109     anna_strcpy(a_sockaddr_un.sun_path, a_path->c_str());
110     break;
111   default:
112     s = NULL;
113     len = 0;
114
115     if(true) {
116       string msg(socket.asString());
117       msg += "Domain type unsupported at this version";
118       throw RuntimeException(msg, ANNA_FILE_LOCATION);
119     }
120
121     break;
122   }
123 }
124
125 void comm::AccessPoint::asString(std::string& msg) const
126 {
127   msg += "{ ";
128
129   if(a_inetAddress.isNull() == false)
130     msg += a_inetAddress.asString();
131   else if(a_path != NULL) {
132     msg += "File: ";
133     msg += *a_path;
134   } else
135     msg += "<null>";
136
137   msg += "  }";
138 }
139
140 void comm::AccessPoint::asXML(const char* name, xml::Node* parent) const
141 noexcept(false) {
142   xml::Node* accessPoint  = parent->createChild(name);
143
144   if(a_inetAddress.isNull() == false)
145     a_inetAddress.asXML(accessPoint);
146   else if(a_path != NULL)
147     accessPoint->createAttribute("File", *a_path);
148 }
149
150 std::string comm::AccessPoint::serialize() const
151 {
152   std::string result;
153
154   if(a_inetAddress.isNull() == false)
155     result = a_inetAddress.serialize();
156   else if(a_path != NULL)
157     result = *a_path;
158   else
159     result = "unknown";
160
161   return result;
162 }
163