Remove dynamic exceptions
[anna.git] / example / comm / largeBinaryCodec / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <iostream>
10
11 #include <anna/core/util/CommandLine.hpp>
12 #include <anna/core/DataBlock.hpp>
13 #include <anna/core/tracing/TraceWriter.hpp>
14 #include <anna/core/tracing/Logger.hpp>
15
16 #include <anna/comm/comm.hpp>
17
18 #include <anna/app/Application.hpp>
19
20 class MyCodec : public Codec {
21 public:
22    MyCodec () : Codec (0xaa, false) 
23    {
24       attach ("string", a_string);
25       attach ("integer", a_integer);
26    }
27
28    void set (const char* value) { a_string = value; }
29    void set (const int value) { a_integer = value; }
30
31    const std::string& getString () const { return a_string; }
32    const int getInteger () const { return a_integer; }
33
34 private:
35    std::string a_string;
36    int a_integer;
37 };
38
39 class Test : public anna::app::Application {
40 public:
41    Test ();
42
43 private:
44    MyCodec a_input;
45
46    void show (const LargeBinaryCodec& lbc) noexcept(false);
47
48    void run () noexcept(false);
49 };
50
51 using namespace std;
52 using namespace anna;
53
54 int main (int argc, const char** argv)
55 {
56    CommandLine& commandLine (CommandLine::instantiate ());
57    Test test;
58    TraceWriter traceWriter ("my.trace", 2048000);
59
60    try {
61       commandLine.initialize (argv, argc);
62       commandLine.verify ();
63
64       Logger::initialize ("testfunctions", &traceWriter);
65       Logger::setLevel (Logger::Debug);
66
67       test.start ();
68    }
69    catch (Exception& ex) {
70       cout << ex.asString () << endl;
71    }
72
73    return 0;
74 }
75
76 Test::Test () :
77    anna::app::Application ("testfunctions", "Comprobacin de la clase Codec", "1.0")
78 {
79    CommandLine& commandLine (CommandLine::instantiate ());
80
81    commandLine.add ("i", CommandLine::Argument::Mandatory, "Enterno a enviar");
82 }
83
84 void Test::run ()
85    noexcept(false)
86 {
87    CommandLine& cm (CommandLine::instantiate ());
88    LargeBinaryCodec input (1, false);
89    LargeBinaryCodec output (1, false);
90
91    a_input.set ("12345");
92    a_input.set (cm.getIntegerValue ("i"));
93    input += a_input.code ();
94
95    a_input.set ("6789");
96    a_input.set (cm.getIntegerValue ("i") + 10);
97    input += a_input.code ();
98
99    a_input.set ("abcdefg");
100    a_input.set (cm.getIntegerValue ("i") + 20);
101    input += a_input.code ();
102
103    cout << "------------- Input LargeBinaryCodec: " << endl;
104    show (input);
105
106    cout << "------------- Output LargeBinaryCodec: " << endl;
107    const DataBlock& all = input.code ();  // Codifica el LBC.
108    output.decode (all);                   // Lo decodific
109    show (output);
110 }
111
112 void Test::show (const LargeBinaryCodec& lbc) 
113    noexcept(false)
114 {
115    MyCodec output;
116
117    cout << "Size: " << lbc.size () << endl;
118
119    for (LargeBinaryCodec::const_iterator ii = lbc.begin (), maxii = lbc.end (); ii != maxii; ii ++) {
120       const DataBlock& chunk = *LargeBinaryCodec::data (ii);
121
122       Logger::debug (anna::functions::asString (chunk), ANNA_FILE_LOCATION);
123
124       output.decode (chunk);
125
126       cout << "String: " << output.getString () << endl;
127       cout << "Integer: " << output.getInteger () << endl << endl;
128    }
129
130    cout << endl;
131 }
132