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 //
10 #include <sys/socket.h>
12 #include <anna/xml/Node.hpp>
14 #include <anna/comm/AccessPoint.hpp>
15 #include <anna/comm/Socket.hpp>
16 #include <anna/comm/Device.hpp>
21 bool comm::AccessPoint::operator == (const comm::AccessPoint& rhs) const
23 if(rhs.isNull() && this->isNull())
26 if(a_path != NULL && rhs.a_path != NULL)
27 return *a_path == *rhs.a_path;
29 if(a_inetAddress.isNull() == false && rhs.a_inetAddress.isNull() == false)
30 return a_inetAddress == rhs.a_inetAddress;
35 comm::AccessPoint& comm::AccessPoint::operator = (const INetAddress & inetAddress)
42 a_inetAddress = inetAddress;
46 comm::AccessPoint& comm::AccessPoint::operator = (const std::string & path)
51 a_inetAddress.clear();
54 a_path = new std::string(path);
61 comm::AccessPoint& comm::AccessPoint::operator = (const AccessPoint & rhs)
67 a_inetAddress.clear();
77 return (rhs.a_path != NULL) ? operator= (*rhs.a_path) : operator= (rhs.a_inetAddress);
80 void comm::AccessPoint::translate(const comm::Socket& socket, sockaddr*& s, int& len)
81 throw(RuntimeException) {
82 switch(socket.getDomain()) {
83 case Socket::Domain::Inet:
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);
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);
98 case Socket::Domain::Unix:
101 string msg(socket.asString());
102 msg += "Cannot attach UNIX socket without file";
103 throw RuntimeException(msg, ANNA_FILE_LOCATION);
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());
116 string msg(socket.asString());
117 msg += "Domain type unsupported at this version";
118 throw RuntimeException(msg, ANNA_FILE_LOCATION);
125 void comm::AccessPoint::asString(std::string& msg) const
129 if(a_inetAddress.isNull() == false)
130 msg += a_inetAddress.asString();
131 else if(a_path != NULL) {
140 void comm::AccessPoint::asXML(const char* name, xml::Node* parent) const
141 throw(RuntimeException) {
142 xml::Node* accessPoint = parent->createChild(name);
144 if(a_inetAddress.isNull() == false)
145 a_inetAddress.asXML(accessPoint);
146 else if(a_path != NULL)
147 accessPoint->createAttribute("File", *a_path);
150 std::string comm::AccessPoint::serialize() const
154 if(a_inetAddress.isNull() == false)
155 result = a_inetAddress.serialize();
156 else if(a_path != NULL)