First commit
[anna.git] / include / anna / comm / functions.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_functions_hpp
38 #define anna_comm_functions_hpp
39
40 #include <netinet/in.h>
41
42 #include <vector>
43
44 #include <anna/app/functions.hpp>
45
46 namespace anna {
47
48 namespace comm {
49
50 class Application;
51 class Server;
52 class Service;
53
54 /**
55    functions - Metodos y variables
56 */
57 struct functions : public anna::app::functions {
58   /**
59      Devuelve el nombre de la maquina sobre la que estamos ejecutando.
60      \return El nombre de la maquina sobre la que estamos ejecutando.
61   */
62   static std::string getHostName() throw(RuntimeException);
63
64
65   /**
66    * Helper de uso externo que resuelve el nombre de la maquina recibido como parametro y devuelve
67    * la primera IP resuelta por el sistema. Tracea los aliases y sus direcciones IP, asi como el
68    * nombre oficial de la maquina.
69    *
70    * \param hostname Nombre logico del servidor que sera usado para resolver. Podria ser una cadena
71    * de la forma www.gopher.net
72    *
73    * \return Primera direccion IP retornadas por el sistema
74    *
75    * \see man gethostbyname.
76    */
77   static std::string resolveIP(const char* hostname) throw(RuntimeException);   // EDU Nov 2012
78
79   /**
80      Devuelve la referencia de la instancia de nuestra aplicación para trabajar en la
81      capa anna.comm
82      @return La referencia de la instancia de nuestra aplicación para trabajar en la
83      capa anna.comm
84   */
85   static comm::Application& getApp() throw(RuntimeException);
86
87   /**
88       Codifica un entero de 32 bits en buffer que debe tener, al menos, 4 bytes de longitud.
89       @param result Buffer donde vamos a codificar el número.
90       @param n Nmero a codificar.
91       \return El buffer que contiene el número aplanado.
92    */
93   static const char* codeInteger(char* result, const int n) throw();
94
95   /**
96      Codifica un entero de 16 bits en buffer que debe tener 2 bytes de longitud.
97      @param result Buffer donde vamos a codificar el número.
98      @param n Nmero a codificar.
99      \return El buffer que contiene el número aplanado.
100   */
101   static const char* codeShort(char* result, const short int n) throw();
102
103   /**
104       Codifica un entero de 64 bits en buffer que debe tener, al menos, 8 bytes de longitud.
105       @param result Buffer donde vamos a codificar el numero.
106       @param n numero a codificar.
107       \return El buffer que contiene el numero aplanado.
108    */
109   static const char* codeInteger64(char* result, const Integer64 n) throw();
110
111   /**
112    * Codifica un número en coma flotante de 32 bits según el estandard IEEE-754, en un buffer
113    * que debe tener, al menos, 4 bytes de longitud.
114    * @param result Buffer donde vamos a codificar el número.
115    * @param n Nmero a codificar.
116    * \return El buffer que contiene el número aplanado.
117    */
118   static const char* codeFloat(char* result, const float n) throw();
119
120   /**
121    * Codifica un número en coma flotante de 64 bits según el estandard IEEE-754, en un buffer
122    * que debe tener, al menos, 8 bytes de longitud.
123    * @param result Buffer donde vamos a codificar el número.
124    * @param n Nmero a codificar.
125    * \return El buffer que contiene el número aplanado.
126    */
127   static const char* codeDouble(char* result, const double n) throw();
128
129   /**
130      Decodifica un numero entero de 32 bits contenido en un buffer de 4 bytes.
131      @param data Buffer que contiene el numero aplanado.
132      @return El valor del numero contenido en el buffer.
133   */
134   static int decodeInteger(const char* data)  throw();
135
136   /**
137      Decodifica un numero entero de 16 bits contenido en un buffer de 2 bytes.
138      @param data Buffer que contiene el numero aplanado.
139      @return El valor del numero contenido en el buffer.
140   */
141   static short int decodeShort(const char* data)  throw();
142
143   /**
144      Decodifica un numero entero de 64 bits contenido en un buffer de 8 bytes.
145      @param data Buffer que contiene el numero aplanado.
146      @return El valor del numero contenido en el buffer.
147   */
148   static Integer64 decodeInteger64(const char* data)  throw();
149
150   /**
151      Decodifica un numero en coma flotante de 32 bits contenido en un buffer de 4 bytes, y codificado
152      según el estandard IEEE-754.
153      @param data Buffer que contiene el numero aplanado.
154      @return El valor del numero contenido en el buffer.
155   */
156   static float decodeFloat(const char* data)  throw();
157
158   /**
159      Decodifica un numero en coma flotante de 64 bits contenido en un buffer de 8 bytes, y codificado
160      según el estandard IEEE-754.
161      @param data Buffer que contiene el numero aplanado.
162      @return El valor del numero contenido en el buffer.
163   */
164   static double decodeDouble(const char* data)  throw();
165 };
166
167 }
168 }
169
170 #endif
171
172