First commit
[anna.git] / example / comm / largeBinaryCodec / 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/tracing/Logger.hpp>
43
44 #include <anna/comm/comm.hpp>
45
46 #include <anna/app/Application.hpp>
47
48 class MyCodec : public Codec {
49 public:
50    MyCodec () : Codec (0xaa, false) 
51    {
52       attach ("string", a_string);
53       attach ("integer", a_integer);
54    }
55
56    void set (const char* value) throw () { a_string = value; }
57    void set (const int value) throw () { a_integer = value; }
58
59    const std::string& getString () const throw () { return a_string; }
60    const int getInteger () const throw () { return a_integer; }
61
62 private:
63    std::string a_string;
64    int a_integer;
65 };
66
67 class Test : public anna::app::Application {
68 public:
69    Test ();
70
71 private:
72    MyCodec a_input;
73
74    void show (const LargeBinaryCodec& lbc) throw (anna::RuntimeException);
75
76    void run () throw (anna::RuntimeException);
77 };
78
79 using namespace std;
80 using namespace anna;
81
82 int main (int argc, const char** argv)
83 {
84    CommandLine& commandLine (CommandLine::instantiate ());
85    Test test;
86    TraceWriter traceWriter ("my.trace", 2048000);
87
88    try {
89       commandLine.initialize (argv, argc);
90       commandLine.verify ();
91
92       Logger::initialize ("testfunctions", &traceWriter);
93       Logger::setLevel (Logger::Debug);
94
95       test.start ();
96    }
97    catch (Exception& ex) {
98       cout << ex.asString () << endl;
99    }
100
101    return 0;
102 }
103
104 Test::Test () :
105    anna::app::Application ("testfunctions", "Comprobacin de la clase Codec", "1.0")
106 {
107    CommandLine& commandLine (CommandLine::instantiate ());
108
109    commandLine.add ("i", CommandLine::Argument::Mandatory, "Enterno a enviar");
110 }
111
112 void Test::run ()
113    throw (RuntimeException)
114 {
115    CommandLine& cm (CommandLine::instantiate ());
116    LargeBinaryCodec input (1, false);
117    LargeBinaryCodec output (1, false);
118
119    a_input.set ("12345");
120    a_input.set (cm.getIntegerValue ("i"));
121    input += a_input.code ();
122
123    a_input.set ("6789");
124    a_input.set (cm.getIntegerValue ("i") + 10);
125    input += a_input.code ();
126
127    a_input.set ("abcdefg");
128    a_input.set (cm.getIntegerValue ("i") + 20);
129    input += a_input.code ();
130
131    cout << "------------- Input LargeBinaryCodec: " << endl;
132    show (input);
133
134    cout << "------------- Output LargeBinaryCodec: " << endl;
135    const DataBlock& all = input.code ();  // Codifica el LBC.
136    output.decode (all);                   // Lo decodific
137    show (output);
138 }
139
140 void Test::show (const LargeBinaryCodec& lbc) 
141    throw (RuntimeException)
142 {
143    MyCodec output;
144
145    cout << "Size: " << lbc.size () << endl;
146
147    for (LargeBinaryCodec::const_iterator ii = lbc.begin (), maxii = lbc.end (); ii != maxii; ii ++) {
148       const DataBlock& chunk = *LargeBinaryCodec::data (ii);
149
150       Logger::debug (anna::functions::asString (chunk), ANNA_FILE_LOCATION);
151
152       output.decode (chunk);
153
154       cout << "String: " << output.getString () << endl;
155       cout << "Integer: " << output.getInteger () << endl << endl;
156    }
157
158    cout << endl;
159 }
160