Updated license
[anna.git] / include / anna / comm / INetAddress.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_comm_INetAddress_hpp
38 #define anna_comm_INetAddress_hpp
39
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44
45 #include <string>
46
47 #include <anna/core/RuntimeException.hpp>
48
49 namespace anna {
50
51 namespace xml {
52 class Node;
53 }
54
55 namespace comm {
56
57 class Device;
58
59 /**
60    Network address abstraction.
61 */
62 class INetAddress {
63 public:
64   /**
65      Constructor.
66   */
67   INetAddress() : a_device(NULL), a_port(-1)  {}
68
69   /**
70      Constructor.
71      \param device Instance for device (address).
72      \param port Port number.
73   */
74   INetAddress(const Device* device, const int port = -1)  : a_device(device),  a_port(port) {;}
75
76   /**
77      Copy constructor.
78      \param other Source network address.
79   */
80   INetAddress(const INetAddress& other) : a_device(other.a_device) , a_port(other.a_port) {;}
81
82   /**
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.
86   */
87   const Device* getDevice(const bool exceptionWhenNull = true) const throw(RuntimeException);
88
89   /**
90      Returns the port associated to this instance.
91      \return Port associated to this instance.
92   */
93   int getPort() const throw() { return a_port; }
94
95   /**
96      Sets the address for this instance.
97      \param device Address provided.
98   */
99   void setAddress(const Device* device) throw()  { a_device = device;  }
100
101   /**
102      Sets the port for this instance.
103      \param port Port provided.
104   */
105   void setPort(const int port) throw() { a_port = port; }
106
107   /**
108      Copy operator.
109      \param right Source address to be copied.
110   */
111   INetAddress& operator = (const INetAddress& right) throw() { a_device = right.a_device; a_port = right.a_port; return *this; }
112
113   /**
114     Comparison operator.
115     \param right Source address to be compared.
116     @return \em true when address provided is equal to this \em false in other case.
117   */
118   bool operator == (const INetAddress& right) const throw() { return a_device == right.a_device && a_port == right.a_port; }
119
120   /**
121      Returns the initialized state for this network address.
122      @return \em true when initialized, \em false when not.
123   */
124   bool isNull() const throw() { return (a_device == NULL || a_port == -1);  }
125
126   /**
127      Clear the content for this instance.
128   */
129   void clear() throw() { a_device = NULL; a_port = -1; }
130
131   /**
132      Returns string with relevant information for this instance.
133      @return string with relevant information for this instance.
134   */
135   std::string asString() const throw();
136
137   /**
138      Returns string with low-level format relevant information for this instance.
139      @return string with low-level format relevant information for this instance.
140   */
141   std::string serialize() const throw();
142
143   /**
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.
147   */
148   xml::Node* asXML(xml::Node* parent) const throw(RuntimeException);
149
150 private:
151   const Device* a_device;
152   int a_port;
153 };
154
155 }
156 }
157
158 #endif
159
160
161