Environment redesign and test/examples review. English API revisions
[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 - Methods and variables
56 */
57 struct functions : public anna::app::functions {
58   /**
59      Returns the host name over which we are executing our process.
60      @return host name over which we are executing our process.
61   */
62   static std::string getHostName() throw(RuntimeException);
63
64
65   /**
66      Resolves the host name provided with the first IP returned by the system.
67      Aliases and their addresses are traced as well as the official host name.
68
69      @param hostname Logical name for the server used to resolve (could be www.gopher.net i.e.).
70
71      @return First IP address returned by the system.
72    
73      \see man gethostbyname.
74    */
75   static std::string resolveIP(const char* hostname) throw(RuntimeException);
76
77   /**
78      Returns our application instance on anna.comm layer.
79      @return Reference to our application instance on anna.comm layer.
80   */
81   static comm::Application& getApp() throw(RuntimeException);
82
83   /**
84      Encodes an integer number with 32 bits over a buffer with at least 4 bytes of length.
85      @param result Buffer where the number is encoded.
86      @param n Number to encode.
87      \return Buffer with the encoded number.
88    */
89   static const char* codeInteger(char* result, const int n) throw();
90
91   /**
92      Encodes an integer number with 16 bits over a buffer with at least 2 bytes of length.
93      @param result Buffer where the number is encoded.
94      @param n Number to encode.
95      \return Buffer with the encoded number.
96   */
97   static const char* codeShort(char* result, const short int n) throw();
98
99   /**
100      Encodes an integer number with 64 bits over a buffer with at least 8 bytes of length.
101      @param result Buffer where the number is encoded.
102      @param n Number to encode.
103      \return Buffer with the encoded number.
104    */
105   static const char* codeInteger64(char* result, const Integer64 n) throw();
106
107   /**
108      Encodes a floating number with 32 bits (according to the standard IEEE-754) over a buffer with at least 4 bytes of length.
109      @param result Buffer where the number is encoded.
110      @param n Number to encode.
111      \return Buffer with the encoded number.
112    */
113   static const char* codeFloat(char* result, const float n) throw();
114
115   /**
116      Encodes a floating number with 64 bits (according to the standard IEEE-754) over a buffer with at least 8 bytes of length.
117      @param result Buffer where the number is encoded.
118      @param n Number to encode.
119      \return Buffer with the encoded number.
120    */
121   static const char* codeDouble(char* result, const double n) throw();
122
123   /**
124      Decodes an 32 bits integer number contained in a 4-bytes buffer.
125      @param data Buffer with the encoded number.
126      @return Value for the number contained in the buffer.
127   */
128   static int decodeInteger(const char* data)  throw();
129
130   /**
131      Decodes an 16 bits integer number contained in a 2-bytes buffer.
132      @param data Buffer with the encoded number.
133      @return Value for the number contained in the buffer.
134   */
135   static short int decodeShort(const char* data)  throw();
136
137   /**
138      Decodes an 64 bits integer number contained in a 8-bytes buffer.
139      @param data Buffer with the encoded number.
140      @return Value for the number contained in the buffer.
141   */
142   static Integer64 decodeInteger64(const char* data)  throw();
143
144   /**
145      Decodes an 32 bits floating number (according to the standard IEEE-754) contained in a 4-bytes buffer.
146      @param data Buffer with the encoded number.
147      @return Value for the number contained in the buffer.
148   */
149   static float decodeFloat(const char* data)  throw();
150
151   /**
152      Decodes an 64 bits floating number (according to the standard IEEE-754) contained in a 8-bytes buffer.
153      @param data Buffer with the encoded number.
154      @return Value for the number contained in the buffer.
155   */
156   static double decodeDouble(const char* data)  throw();
157 };
158
159 }
160 }
161
162 #endif
163
164