1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
17 // * Neither the name of the copyright holder 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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
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>
44 #include <anna/app/Application.hpp>
46 #include <anna/comm/comm.hpp>
50 class MyCodec : public Codec {
52 MyCodec () : Codec (0xaa) ,
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);
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; }
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; }
79 anna::DataBlock a_dataBlock;
80 std::string a_string2;
85 class Test : public anna::app::Application {
93 void run () throw (anna::RuntimeException);
99 int main (int argc, const char** argv)
101 CommandLine& commandLine (CommandLine::instantiate ());
103 TraceWriter* traceWriter = new TraceWriter;
106 commandLine.initialize (argv, argc);
107 commandLine.verify ();
109 Logger::initialize ("testfunctions", traceWriter);
110 Logger::setLevel (Logger::Debug);
114 catch (Exception& ex) {
115 cout << ex.asString () << endl;
124 anna::app::Application ("testfunctions", "Comprobacion de la clase comm::Codec", "1.0.0")
126 CommandLine& commandLine (CommandLine::instantiate ());
128 commandLine.add ("string", CommandLine::Argument::Mandatory, "Cadena a enviar");
129 commandLine.add ("i", CommandLine::Argument::Mandatory, "Enterno a enviar");
133 throw (RuntimeException)
135 CommandLine& cm (CommandLine::instantiate ());
137 const char* string = cm.getValue ("string");
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);
147 DataBlock dataBlock (true);
149 for (int i = strlen (string) - 1; i > 0; i --)
150 dataBlock += string [i];
152 a_input.set (dataBlock);
154 const DataBlock& result (a_input.code ());
156 a_output.decode (result);
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;
166 * Para comprobar que el modo de codificar los bytes son igual que en la versiĆ³n de Java
168 char buffer [sizeof (S64)];
171 comm::functions::codeShort (buffer, ss);
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);
180 comm::functions::codeInteger (buffer, xx);
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);
188 // S64 ll = 31604938272LL;
189 S64 ll = 98765432101234LL;
190 comm::functions::codeInteger64 (buffer, ll);
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);
198 cout << "Sizeof (float/int): " << sizeof (float) << "/" << sizeof (int) << endl;
202 anna_memcpy (&ii, &ff, sizeof (ff));
203 comm::functions::codeInteger (buffer, ii);
204 cout << "Float I: " << ff << " (" << anna::functions::asHexString (ii) << ") " << endl;
207 ii = comm::functions::decodeInteger (buffer);
208 anna_memcpy (&ff2, &ii, sizeof (ff));
209 cout << "Float O: " << ff2 << " (" << anna::functions::asHexString (ii) << ") " << endl;
211 dd = -123123123.3232;
213 anna_memcpy (&ii64, &dd, sizeof (dd));
214 comm::functions::codeInteger64 (buffer, ii64);
215 cout << "Double I: " << dd << " (" << anna::functions::asHexString (ii64) << ") " << endl;
218 ii64 = comm::functions::decodeInteger64 (buffer);
219 anna_memcpy (&dd2, &ii64, sizeof (dd));
220 cout << "Double O: " << dd2 << " (" << anna::functions::asHexString (ii64) << ") " << endl;