First commit
[anna.git] / example / comm / codec / main.cpp
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 #include <iostream>
38
39 #include <anna/core/util/CommandLine.hpp>
40 #include <anna/core/DataBlock.hpp>
41 #include <anna/core/tracing/TraceWriter.hpp>
42 #include <anna/core/functions.hpp>
43
44 #include <anna/app/Application.hpp>
45
46 #include <anna/comm/comm.hpp>
47
48 using namespace anna;
49
50 class MyCodec : public Codec {
51 public:
52    MyCodec () : Codec (0xaa) ,
53       a_dataBlock (true)
54    {
55       attach ("string", a_string);
56       attach ("integer", a_integer);
57       attach ("block", a_dataBlock);
58       attach ("string", a_string2);
59       attach ("float", a_float);
60       attach ("double", a_double);
61    }
62    
63    void set (const std::string& value) throw () { a_string = a_string2 = value; a_string2 += value; }
64    void set (const int value) throw () { a_integer = value; }
65    void set (const anna::DataBlock& value) throw () { a_dataBlock = value; }
66    void set (const float value) throw () { a_float = value; }
67    void set (const double value) throw () { a_double = value; }
68    
69    const std::string& getString () const throw () { return a_string; }
70    const int getInteger () const throw () { return a_integer; }
71    const anna::DataBlock& getDataBlock () const throw (anna::RuntimeException) { return a_dataBlock; }
72    const std::string& getString2 () const throw () { return a_string2; }
73    float getFloat () const throw () { return a_float; }
74    float getDouble () const throw () { return a_double; }
75    
76 private:
77    std::string a_string;
78    int a_integer;
79    anna::DataBlock a_dataBlock;
80    std::string a_string2;
81    float a_float;
82    double a_double;
83 };
84
85 class Test : public anna::app::Application {
86 public:
87    Test ();
88    
89 private:
90    MyCodec a_input;
91    MyCodec a_output;
92
93    void run () throw (anna::RuntimeException);      
94 };
95
96 using namespace std;
97 using namespace anna;
98
99 int main (int argc, const char** argv)
100 {
101    CommandLine& commandLine (CommandLine::instantiate ());
102    Test test;
103    TraceWriter* traceWriter = new TraceWriter;
104
105    try {
106       commandLine.initialize (argv, argc);
107       commandLine.verify ();
108
109       Logger::initialize ("testfunctions", traceWriter);
110       Logger::setLevel (Logger::Debug); 
111  
112       test.start ();
113    }
114    catch (Exception& ex) {
115       cout << ex.asString () << endl;
116    }
117    
118    delete traceWriter;
119    
120    return 0;
121 }
122
123 Test::Test () : 
124    anna::app::Application ("testfunctions", "Comprobacion de la clase comm::Codec", "1.0.0") 
125 {
126    CommandLine& commandLine (CommandLine::instantiate ());
127       
128    commandLine.add ("string", CommandLine::Argument::Mandatory, "Cadena a enviar");
129    commandLine.add ("i", CommandLine::Argument::Mandatory, "Enterno a enviar");
130 }
131
132 void Test::run () 
133    throw (RuntimeException)
134 {
135    CommandLine& cm (CommandLine::instantiate ());
136    
137    const char* string = cm.getValue ("string");
138
139    float ff;
140    double dd;
141    
142    a_input.set (string);
143    a_input.set (cm.getIntegerValue ("i"));
144    a_input.set (ff = 1.12345);
145    a_input.set (dd = 2.12345);
146    
147    DataBlock dataBlock (true);
148    
149    for (int i = strlen (string) - 1; i > 0; i --)
150       dataBlock += string [i];
151       
152    a_input.set (dataBlock);
153
154    const DataBlock& result (a_input.code ());
155
156    a_output.decode (result);
157    
158    cout << "String: " << a_output.getString () << endl;
159    cout << "Integer: " << a_output.getInteger () << endl;
160    cout << "Data block: " << anna::functions::asString (a_output.getDataBlock ()) << endl;
161    cout << "String2: " << a_output.getString2 () << endl << endl;
162    cout << "Float: " << a_output.getFloat () << endl << endl;
163    cout << "Double: " << a_output.getDouble () << endl << endl;
164
165    /*
166     * Para comprobar que el modo de codificar los bytes son igual que en la versiĆ³n de Java
167     */
168    char buffer [sizeof (Integer64)];
169
170    short ss = 23456;
171    comm::functions::codeShort (buffer, ss);
172    dataBlock.clear ();
173    dataBlock.append (buffer, sizeof (ss));
174    cout << anna::functions::asString ("Short: %d - 0x%x", ss, ss);
175    cout << anna::functions::asString (dataBlock) << endl << endl;
176    ss = comm::functions::decodeShort (buffer);
177    cout << anna::functions::asString ("Short2: %d - 0x%x\n\n", ss, ss);
178
179    int xx = 123456;
180    comm::functions::codeInteger (buffer, xx);
181    dataBlock.clear ();
182    dataBlock.append (buffer, sizeof (xx));
183    cout << anna::functions::asString ("Integer: %d - 0x%x", xx, xx);
184    cout << anna::functions::asString (dataBlock) << endl << endl;
185    xx = comm::functions::decodeInteger (buffer);
186    cout << anna::functions::asString ("Integer2: %d - 0x%x\n\n", xx, xx);
187
188 //   Integer64 ll = 31604938272LL;
189    Integer64 ll = 98765432101234LL;
190    comm::functions::codeInteger64 (buffer, ll);
191    dataBlock.clear ();
192    dataBlock.append (buffer, sizeof (ll));
193    cout << anna::functions::asString ("Integer64: %lld - 0x%llx", ll, ll);
194    cout << anna::functions::asString (dataBlock) << endl << endl;
195    ll = comm::functions::decodeInteger64 (buffer);
196    cout << anna::functions::asString ("Integer64-2: %lld - 0x%llx\n\n", ll, ll);
197   
198    cout << "Sizeof (float/int): " << sizeof (float) << "/" << sizeof (int) << endl;
199
200    ff = -123.3232;
201    int ii;
202    anna_memcpy (&ii, &ff, sizeof (ff));
203    comm::functions::codeInteger (buffer, ii);
204    cout << "Float I: " << ff << " (" << anna::functions::asHexString (ii) << ") " << endl;
205
206    float ff2;
207    ii = comm::functions::decodeInteger (buffer);
208    anna_memcpy (&ff2, &ii, sizeof (ff));
209    cout << "Float O: " << ff2 << " (" << anna::functions::asHexString (ii) << ") " << endl;
210
211    dd = -123123123.3232;
212    Integer64 ii64;
213    anna_memcpy (&ii64, &dd, sizeof (dd));
214    comm::functions::codeInteger64 (buffer, ii64);
215    cout << "Double I: " << dd << " (" << anna::functions::asHexString (ii64) << ") " << endl;
216
217    double dd2;
218    ii64 = comm::functions::decodeInteger64 (buffer);
219    anna_memcpy (&dd2, &ii64, sizeof (dd));
220    cout << "Double O: " << dd2 << " (" << anna::functions::asHexString (ii64) << ") " << endl;
221 }
222