1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
9 #include <anna/config/defines.hpp>
11 #include <anna/comm/SureTransport.hpp>
12 #include <anna/comm/Socket.hpp>
13 #include <anna/comm/Message.hpp>
14 #include <anna/comm/functions.hpp>
18 comm::TransportFactoryImpl <comm::SureTransport> comm::SureTransport::st_transportFactory;
20 comm::SureTransport::SureTransport() :
23 setInputMessage(new Message());
24 char aux [sizeof(short int)];
25 a_precodec.append(functions::codeShort(aux, initTag), sizeof(short int));
28 comm::SureTransport::~SureTransport() {
29 delete getInputMessage();
32 int comm::SureTransport::calculeSize(const DataBlock& dataBlock)
34 const char* buffer = dataBlock.getData();
37 if(dataBlock.getSize() >= headerSize)
38 if(buffer [0] == (char) 0xaa && buffer [1] == (char) 0xaa)
39 result = functions::decodeInteger(buffer + sizeof(short int));
44 //-------------------------------------------------------------------------------------------
45 // Recordar que 'message' esta alojado en la memoria intermedia que el comm::ClientSocket
46 // tiene reservada para ir guardando los mensajes en proceso => no es una copia si no una
47 // referencia un puntero.
49 // (1) Transfiere la referencia de message al cuerpo del mensaje. Ojo!! tampoco hace copia.
50 //-------------------------------------------------------------------------------------------
51 const comm::Message* comm::SureTransport::decode(const DataBlock& message)
53 const int totalSize = message.getSize();
55 if(totalSize < headerSize)
56 throw RuntimeException("Invalid message to decode", ANNA_FILE_LOCATION);
58 const char* buffer = message.getData();
60 if(buffer [0] != (char) 0xaa || buffer [1] != (char) 0xaa)
61 throw RuntimeException("Missing start label on message (0xaaaa)", ANNA_FILE_LOCATION);
63 const int bodySize = totalSize - headerSize;
66 throw RuntimeException("Invalid message size to decode", ANNA_FILE_LOCATION);
68 return getInputMessage()->setBody(buffer + headerSize, bodySize); // (1)
71 //---------------------------------------------------------------------------
72 // Los componentes del SureTransport:
73 // Etiquta de comienzo 0xaaaa + short (<longitud ttal>) + datos
74 //---------------------------------------------------------------------------
75 const DataBlock& comm::SureTransport::code(comm::Message& message)
77 char aux [sizeof(int)];
78 const DataBlock& data = message.code();
79 a_forCode = a_precodec;
80 a_forCode.append(functions::codeInteger(aux, headerSize + data.getSize()), sizeof(int));
81 return a_forCode += data;