Remove dynamic exceptions
[anna.git] / source / comm / transport / SureTransport.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 <anna/config/defines.hpp>
10
11 #include <anna/comm/SureTransport.hpp>
12 #include <anna/comm/Socket.hpp>
13 #include <anna/comm/Message.hpp>
14 #include <anna/comm/functions.hpp>
15
16 using namespace anna;
17
18 comm::TransportFactoryImpl <comm::SureTransport> comm::SureTransport::st_transportFactory;
19
20 comm::SureTransport::SureTransport() :
21   comm::Transport(),
22   a_precodec(true) {
23   setInputMessage(new Message());
24   char aux [sizeof(short int)];
25   a_precodec.append(functions::codeShort(aux, initTag), sizeof(short int));
26 }
27
28 comm::SureTransport::~SureTransport() {
29   delete getInputMessage();
30 }
31
32 int comm::SureTransport::calculeSize(const DataBlock& dataBlock)
33 noexcept(false) {
34   const char* buffer = dataBlock.getData();
35   int result = -1;
36
37   if(dataBlock.getSize() >= headerSize)
38     if(buffer [0] == (char) 0xaa  && buffer [1] == (char) 0xaa)
39       result = functions::decodeInteger(buffer + sizeof(short int));
40
41   return result;
42 }
43
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.
48 //
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)
52 noexcept(false) {
53   const int totalSize = message.getSize();
54
55   if(totalSize < headerSize)
56     throw RuntimeException("Invalid message to decode", ANNA_FILE_LOCATION);
57
58   const char* buffer = message.getData();
59
60   if(buffer [0] != (char) 0xaa || buffer [1] != (char) 0xaa)
61     throw RuntimeException("Missing start label on message (0xaaaa)", ANNA_FILE_LOCATION);
62
63   const int bodySize = totalSize - headerSize;
64
65   if(bodySize < 0)
66     throw RuntimeException("Invalid message size to decode", ANNA_FILE_LOCATION);
67
68   return getInputMessage()->setBody(buffer + headerSize, bodySize);             // (1)
69 }
70
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)
76 noexcept(false) {
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;
82 }
83