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 #ifndef anna_comm_INetAddress_hpp
38 #define anna_comm_INetAddress_hpp
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
47 #include <anna/core/RuntimeException.hpp>
60 Network address abstraction.
67 INetAddress() : a_device(NULL), a_port(-1) {}
71 \param device Instance for device (address).
72 \param port Port number.
74 INetAddress(const Device* device, const int port = -1) : a_device(device), a_port(port) {;}
78 \param other Source network address.
80 INetAddress(const INetAddress& other) : a_device(other.a_device) , a_port(other.a_port) {;}
83 Returns the device (address) associated to this instance.
84 \param exceptionWhenNull Exception is launched in case the device returned is NULL.
85 \return Device (address) associated to this instance.
87 const Device* getDevice(const bool exceptionWhenNull = true) const throw(RuntimeException);
90 Returns the port associated to this instance.
91 \return Port associated to this instance.
93 int getPort() const throw() { return a_port; }
96 Sets the address for this instance.
97 \param device Address provided.
99 void setAddress(const Device* device) throw() { a_device = device; }
102 Sets the port for this instance.
103 \param port Port provided.
105 void setPort(const int port) throw() { a_port = port; }
109 \param right Source address to be copied.
111 INetAddress& operator = (const INetAddress& right) throw() { a_device = right.a_device; a_port = right.a_port; return *this; }
115 \param right Source address to be compared.
116 @return \em true when address provided is equal to this \em false in other case.
118 bool operator == (const INetAddress& right) const throw() { return a_device == right.a_device && a_port == right.a_port; }
121 Returns the initialized state for this network address.
122 @return \em true when initialized, \em false when not.
124 bool isNull() const throw() { return (a_device == NULL || a_port == -1); }
127 Clear the content for this instance.
129 void clear() throw() { a_device = NULL; a_port = -1; }
132 Returns string with relevant information for this instance.
133 @return string with relevant information for this instance.
135 std::string asString() const throw();
138 Returns string with low-level format relevant information for this instance.
139 @return string with low-level format relevant information for this instance.
141 std::string serialize() const throw();
144 Returns XML document with relevant information for this instance.
145 \param parent XML node from which created data will depend on.
146 @return XML document with relevant information for this instance.
148 xml::Node* asXML(xml::Node* parent) const throw(RuntimeException);
151 const Device* a_device;