a592b53090ed4cb594b9685448ff6c2e5e891754
[anna.git] / include / anna / comm / INetAddress.hpp
1 // ANNA - Anna is Not 'N' 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    Abstraccion de direcciones de red.
61
62    Facilita el uso de las direcciones de red.
63 */
64 class INetAddress {
65 public:
66   /**
67      Constructor.
68   */
69   INetAddress() : a_device(NULL), a_port(-1)  {}
70
71   /**
72      Constructor.
73      \param device Instancia del dispositivo.
74      \param port Numero de puerto.
75   */
76   INetAddress(const Device* device, const int port = -1)  : a_device(device),  a_port(port) {;}
77
78   /**
79      Constructor copia.
80      \param other Direccin IP de la que obtener la informacin.
81   */
82   INetAddress(const INetAddress& other) : a_device(other.a_device) , a_port(other.a_port) {;}
83
84   /**
85      Devuelve la direccion asociada a esta instancia.
86      \param exceptionWhenNull Indica si debemos lanzar una excepcion en caso de que el dispositivo
87      asociado sea NULL.
88      \return La direccion asociada a esta instancia.
89   */
90   const Device* getDevice(const bool exceptionWhenNull = true) const throw(RuntimeException);
91
92   /**
93      Devuelve el puerto asociada a esta instancia.
94      \return El puerto asociada a esta instancia.
95   */
96   int getPort() const throw() { return a_port; }
97
98   /**
99      Establece la direccion IP correspondiente a este objeto.
100      \param device Dispositivo de red asociado a este objeto.
101   */
102   void setAddress(const Device* device) throw()  { a_device = device;  }
103
104   /**
105      Establece el puerto correspondiente a este objeto.
106      \param port Numero de puerto correspondiente a este objeto.
107   */
108   void setPort(const int port) throw() { a_port = port; }
109
110   /**
111      Operador copia.
112      \param right Direccin IP de la que obtener la informacin.
113   */
114   INetAddress& operator = (const INetAddress& right) throw() { a_device = right.a_device; a_port = right.a_port; return *this; }
115
116   /**
117     Operador de comparacion.
118     \param right Direccion con la comparar.
119     @return \em true si la direccion recibida como parametro coincide con esta.
120     \em false en otro caso.
121   */
122   bool operator == (const INetAddress& right) const throw() { return a_device == right.a_device && a_port == right.a_port; }
123
124   /**
125      Devuelve el estado de inicializacin de esta direccin de red.
126      @return \em true si no ha sido inicializa o \em false en otro caso.
127   */
128   bool isNull() const throw() { return (a_device == NULL || a_port == -1);  }
129
130   /**
131      Elimina el contenido de esta instancia.
132   */
133   void clear() throw() { a_device = NULL; a_port = -1; }
134
135   /**
136      Devuelve una cadena la informacion mas relevante de esta instancia.
137      @return Una cadena la informacion mas relevante de esta instancia.
138   */
139   std::string asString() const throw();
140
141   /**
142      Devuelve una cadena la informacion mas relevante de esta instancia en formato de bajo nivel.
143      @return Una cadena la informacion mas relevante de esta instancia en formato de bajo nivel.
144   */
145   std::string serialize() const throw();
146
147   /**
148      Devuelve un documento XML con la informacion mas relevante de esta instancia.
149      \param parent Nodo XML del que deben depender los datos a crear.
150      @return Un documento XML con la informacion mas relevante de esta instancia.
151   */
152   xml::Node* asXML(xml::Node* parent) const throw(RuntimeException);
153
154 private:
155   const Device* a_device;
156   int a_port;
157 };
158
159 }
160 }
161
162 #endif
163
164
165