Fix local server for multiple applications
[anna.git] / comm / transport / LiteTransport.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/LiteTransport.hpp>
12 #include <anna/comm/Message.hpp>
13 #include <anna/comm/functions.hpp>
14
15 using namespace anna;
16
17 comm::TransportFactoryImpl <comm::LiteTransport> comm::LiteTransport::st_transportFactory;
18
19 comm::LiteTransport::LiteTransport() :
20   comm::Transport() {
21   setInputMessage(new Message());
22 }
23
24 comm::LiteTransport::~LiteTransport() {
25   delete getInputMessage();
26 }
27
28 int comm::LiteTransport::calculeSize(const DataBlock& dataBlock)
29 throw(RuntimeException) {
30   const char* buffer = dataBlock.getData();
31   return (dataBlock.getSize() >= headerSize) ? functions::decodeShort(buffer) + headerSize : -1;
32 }
33
34 const comm::Message* comm::LiteTransport::decode(const DataBlock& message)
35 throw(RuntimeException) {
36   const int totalSize = message.getSize();
37
38   if(totalSize < headerSize)
39     throw RuntimeException("Invalid message to decode", ANNA_FILE_LOCATION);
40
41   const char* buffer = message.getData();
42   const int bodySize = totalSize - headerSize;
43
44   if(bodySize < 0)
45     throw RuntimeException("Invalid message size to decode", ANNA_FILE_LOCATION);
46
47   return getInputMessage()->setBody(buffer + headerSize, bodySize);
48 }
49
50 const DataBlock& comm::LiteTransport::code(comm::Message& message)
51 throw(RuntimeException) {
52   char aux [sizeof(int)];
53   const DataBlock& data = message.code();
54   a_forCode.clear();
55   a_forCode.append(functions::codeShort(aux, data.getSize()), headerSize);
56   return a_forCode += data;
57 }
58