Updated license
[anna.git] / source / comm / transport / SureTransport.cpp
1 // ANNA - Anna is Not Nothingness 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 <anna/config/defines.hpp>
38
39 #include <anna/comm/SureTransport.hpp>
40 #include <anna/comm/Socket.hpp>
41 #include <anna/comm/Message.hpp>
42 #include <anna/comm/functions.hpp>
43
44 using namespace anna;
45
46 comm::TransportFactoryImpl <comm::SureTransport> comm::SureTransport::st_transportFactory;
47
48 comm::SureTransport::SureTransport() :
49   comm::Transport(),
50   a_precodec(true) {
51   setInputMessage(new Message());
52   char aux [sizeof(short int)];
53   a_precodec.append(functions::codeShort(aux, initTag), sizeof(short int));
54 }
55
56 comm::SureTransport::~SureTransport() {
57   delete getInputMessage();
58 }
59
60 int comm::SureTransport::calculeSize(const DataBlock& dataBlock)
61 throw(RuntimeException) {
62   const char* buffer = dataBlock.getData();
63   int result = -1;
64
65   if(dataBlock.getSize() >= headerSize)
66     if(buffer [0] == (char) 0xaa  && buffer [1] == (char) 0xaa)
67       result = functions::decodeInteger(buffer + sizeof(short int));
68
69   return result;
70 }
71
72 //-------------------------------------------------------------------------------------------
73 // Recordar que 'message' esta alojado en la memoria intermedia que el comm::ClientSocket
74 // tiene reservada para ir guardando los mensajes en proceso => no es una copia si no una
75 // referencia un puntero.
76 //
77 // (1) Transfiere la referencia de message al cuerpo del mensaje. Ojo!! tampoco hace copia.
78 //-------------------------------------------------------------------------------------------
79 const comm::Message* comm::SureTransport::decode(const DataBlock& message)
80 throw(RuntimeException) {
81   const int totalSize = message.getSize();
82
83   if(totalSize < headerSize)
84     throw RuntimeException("Invalid message to decode", ANNA_FILE_LOCATION);
85
86   const char* buffer = message.getData();
87
88   if(buffer [0] != (char) 0xaa || buffer [1] != (char) 0xaa)
89     throw RuntimeException("Missing start label on message (0xaaaa)", ANNA_FILE_LOCATION);
90
91   const int bodySize = totalSize - headerSize;
92
93   if(bodySize < 0)
94     throw RuntimeException("Invalid message size to decode", ANNA_FILE_LOCATION);
95
96   return getInputMessage()->setBody(buffer + headerSize, bodySize);             // (1)
97 }
98
99 //---------------------------------------------------------------------------
100 // Los componentes del SureTransport:
101 // Etiquta de comienzo 0xaaaa + short (<longitud ttal>) + datos
102 //---------------------------------------------------------------------------
103 const DataBlock& comm::SureTransport::code(comm::Message& message)
104 throw(RuntimeException) {
105   char aux [sizeof(int)];
106   const DataBlock& data = message.code();
107   a_forCode = a_precodec;
108   a_forCode.append(functions::codeInteger(aux, headerSize + data.getSize()), sizeof(int));
109   return a_forCode += data;
110 }
111