Updated license
[anna.git] / include / anna / comm / Device.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_Device_hpp
38 #define anna_comm_Device_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 Network;
58
59 /**
60    Abstraccion de un dispositivo de Red.
61    Un Host puede tener asociado un numero indeterminado de dispositivos de red.
62
63    El metodo Network::find(in_addr_t) creara un nuevo dispositivo de red.
64 */
65 class Device {
66 public:
67   /**
68      Estados en los que puede estar un dispositivo de red.
69   */
70   struct Status { enum _v { Down, Up }; };
71
72   /**
73      Devuelve la direccion asociada a este instancia.
74      \return La direccion asociada a este instancia.
75   */
76   in_addr_t getAddress() const throw() { return a_address; }
77
78   /**
79      Devuelve el estado asociado al dispositivo.
80      \return el estado asociado al dispositivo.
81   */
82   Status::_v getStatus() const throw() { return a_status; }
83
84   /**
85      Establece el estado del dispositivo.
86      \param status Nuevo estado del dispositivo.
87   */
88   void setStatus(const Status::_v status) throw() { a_status = status; }
89
90   /**
91     Operador de comparacion.
92     \param right Direccion con la comparar.
93     @return \em true si la direccion recibida como parametro coincide con esta.
94     \em false en otro caso.
95   */
96   bool operator == (const Device& right) const throw()  { return a_address == right.a_address; }
97
98   /**
99     Operador de comparacion.
100     \param right Direccion con la comparar.
101     @return \em true si la direccion recibida como parametro coincide con esta.
102     \em false en otro caso.
103   */
104   bool operator == (const in_addr_t& right) const throw() { return a_address == right; }
105
106   /**
107     Operador de comparacion.
108     \param ip Direccion con la comparar. En formato A.B.C.D.
109     @return \em true si la direccion recibida como parametro coincide con esta.
110     \em false en otro caso.
111   */
112   bool operator == (const char* ip) const throw() { return a_address == inet_addr(ip); }
113
114   /**
115     Operador de comparacion.
116     \param ip Direccion con la comparar. En formato A.B.C.D.
117     @return \em true si la direccion recibida como parametro coincide con esta.
118     \em false en otro caso.
119   */
120   bool operator == (const std::string& ip) const throw() { return a_address == inet_addr(ip.c_str()); }
121
122   /**
123     Operador distinto.
124     @return \em true si la direccin recibida como par�etro coincide con �ta o
125     \em false en otro caso.
126   */
127   bool operator != (const Device& right) const throw() { return a_address != right.a_address; }
128
129   /**
130     Operador distinto.
131     @return \em true si la direccin recibida como par�etro no coincide con �ta o
132     \em false en otro caso.
133   */
134   bool operator != (const in_addr_t right) const throw() { return a_address != right; }
135
136   /**
137      Devuelve una cadena la informacion mas relevante de esta instancia.
138      @return una cadena la informacion mas relevante de esta instancia.
139   */
140   std::string asString() const throw();
141
142   /**
143      Devuelve un documento XML con la informacion mas relevante de esta instancia.
144      \param parent Nodo XML del que deben depender los datos a crear.
145      @return Un documento XML con la informacion mas relevante de esta instancia.
146   */
147   xml::Node* asXML(xml::Node* parent) const throw(RuntimeException);
148
149   /**
150      Incorpora los paremetros de esta instancia como atributos del nodo XML
151      recibido como parametro.
152      \param node Nodo del que dependen los atributos a crear.
153   */
154   void asAttribute(xml::Node* node) const throw(RuntimeException);
155
156   /**
157      Devuelve el nombre de esta clase. Se puede invocar desde \code template <class T>::asString (const T*); \endcode
158      \return Devuelve el nombre de esta clase.
159   */
160   static const char* className() throw() { return "anna::comm::Device"; }
161
162   /**
163      Devuelve la direccion INET recibida como parametro en formato cadena.
164      \param address Direccion INET a convertir.
165      \return la direccion INET recibida como parametro en formato cadena.
166   */
167   static std::string asString(const in_addr_t& address) throw();
168
169   /**
170      Devuelve la dirección IP recibida como cadena en un tipo in_addr_t.
171      \return la dirección IP recibida como cadena en un tipo in_addr_t.
172   */
173   static in_addr_t asAddress(const std::string& ip) throw() { return inet_addr(ip.c_str()); }
174
175 protected:
176   /**
177      Constructor.
178      \param ip Texto con la direccin IP en formato A.B.C.D.
179   */
180   Device(const char* ip)  : a_address(inet_addr(ip)), a_status(Status::Up) {;}
181
182   /**
183      Constructor.
184      \param ip Texto con la direccin IP en formato A.B.C.D.
185   */
186   Device(const std::string& ip)  : a_address(inet_addr(ip.c_str())), a_status(Status::Up) {;}
187
188   /**
189      Constructor.
190      \param address Direccin IP de esta instancia
191   */
192   Device(const in_addr_t& address)  : a_address(address), a_status(Status::Up) {;}
193
194   /**
195      Constructor copia.
196      \param other Dispositivo
197   */
198   Device(const Device& other) : a_address(other.a_address), a_status(Status::Up) {;}
199
200 private:
201   const in_addr_t a_address;
202   Status::_v a_status;
203
204   friend class Network;
205 };
206
207 }
208 }
209
210 #endif
211
212
213