fipa_acl  1.4
bitefficient_envelope_format.cpp
Go to the documentation of this file.
2 #include "bitefficient_format.h"
3 #include <arpa/inet.h>
4 #include <limits>
5 
6 #define FIPA_BASE_MSG_ID char(0xfe)
7 #define FIPA_EXT_MSG_ID char(0xfd)
8 
9 namespace fipa {
10 namespace acl {
11 
12 std::string BitefficientEnvelopeFormat::apply(const ACLEnvelope& envelope) const
13 {
14  std::string msg = getAllExternalEnvelopes(envelope) + getBaseEnvelope(envelope.getBaseEnvelope()) + envelope.getPayload();
15 
16  return msg;
17 }
18 
19 std::string BitefficientEnvelopeFormat::getAllExternalEnvelopes(const ACLEnvelope& envelope) const
20 {
21  std::string extEnvelopes;
22  const ACLBaseEnvelopeList& list = envelope.getExtraEnvelopes();
23  if(!list.empty())
24  {
25  ACLBaseEnvelopeList::const_iterator cit = list.begin();
26  for(; cit != list.end(); ++cit)
27  {
28  extEnvelopes += getExtEnvelope(*cit);
29  }
30  }
31  return extEnvelopes;
32 }
33 
34 std::string BitefficientEnvelopeFormat::getBaseEnvelope(const ACLBaseEnvelope& envelope) const
35 {
36  std::string baseEnvelope;
37  baseEnvelope += getBaseEnvelopeHeader(envelope);
38  baseEnvelope += getParameters(envelope);
39  baseEnvelope += BitefficientFormat::getEOFCollection();
40  return baseEnvelope;
41 }
42 
43 std::string BitefficientEnvelopeFormat::getParameters(const ACLBaseEnvelope& envelope) const
44 {
45  std::string baseEnvelope;
46  if(envelope.contains(envelope::TO))
47  {
48  baseEnvelope += char(0x02) + BitefficientFormat::getAgentIDSequence(envelope.getTo());
49  }
50 
51  if(envelope.contains(envelope::FROM))
52  {
53  baseEnvelope += char(0x03) + BitefficientFormat::getAgentID(envelope.getFrom());
54  }
55 
57  {
58  baseEnvelope += char(0x04) + BitefficientFormat::getACLRepresentation(envelope.getACLRepresentation());
59  }
60 
61  if(envelope.contains(envelope::COMMENTS))
62  {
63  baseEnvelope += char(0x05) + BitefficientFormat::getNullTerminatedString(envelope.getComments());
64  }
65 
67  {
68  baseEnvelope += char(0x06) + BitefficientFormat::getBinNumber(envelope.getPayloadLength());
69  }
70 
72  {
73  baseEnvelope += char(0x07) + BitefficientFormat::getNullTerminatedString(envelope.getPayloadEncoding());
74  }
75 
77  {
78  baseEnvelope += char(0x09) + BitefficientFormat::getAgentIDSequence(envelope.getIntendedReceivers());
79  }
80 
82  {
83  baseEnvelope += char(0x0a) + BitefficientFormat::getReceivedObject(envelope.getReceivedObject());
84  }
85 
87  {
88  baseEnvelope += char(0x0b) + BitefficientFormat::getBinStringNoCodetable(envelope.getTransportBehaviour());
89  }
90  return baseEnvelope;
91 }
92 
93 std::string BitefficientEnvelopeFormat::getBaseEnvelopeHeader(const ACLBaseEnvelope& envelope) const
94 {
95  std::string representation = BitefficientFormat::getACLRepresentation(envelope.getACLRepresentation());
96  std::string dateTimeToken = BitefficientFormat::getBinDateTimeToken(envelope.getDate());
97 
98  uint32_t length = 2 + 2 + 2 + dateTimeToken.size();
99  std::string header;
100  header += FIPA_BASE_MSG_ID;
101 
102  uint16_t envLen = htons( (uint16_t) length);
103  header.append(reinterpret_cast<const char*>(&envLen), sizeof(uint16_t));
104  header += representation;
105  header += dateTimeToken;
106 
107  return header;
108 }
109 
110 std::string BitefficientEnvelopeFormat::getExtEnvelope(const ACLBaseEnvelope& envelope) const
111 {
112  std::string parameters = getParameters(envelope);
113  std::string receivedObject = getReceivedObject(envelope);
114 
115  uint32_t length = 2 + 2 + parameters.size() + 1 ;
116  std::string extEnvelope = "";
117  extEnvelope += FIPA_EXT_MSG_ID;
118  if(length > std::numeric_limits<uint16_t>::max() )
119  {
120  length += 4; // for jumbo size
121  extEnvelope.append(reinterpret_cast<const char*>((uint16_t) 0), sizeof(uint16_t));
122  uint32_t envLen = htonl( length );
123  extEnvelope.append(reinterpret_cast<const char*>(&envLen), sizeof(uint32_t));
124  } else {
125  uint16_t envLen = htons( length );
126  extEnvelope.append(reinterpret_cast<const char*>(&envLen), sizeof(uint16_t));
127  }
128  extEnvelope += receivedObject;
129  extEnvelope += parameters;
130  extEnvelope += BitefficientFormat::getEOFCollection();
131  return extEnvelope;
132 }
133 
134 std::string BitefficientEnvelopeFormat::getReceivedObject(const ACLBaseEnvelope& envelope) const
135 {
136  const ReceivedObject& receivedObject = envelope.getReceivedObject();
137  std::string encoded;
138  // By
139  encoded += BitefficientFormat::getNullTerminatedString(receivedObject.getBy());
140  // Date
141  encoded += BitefficientFormat::getBinDateTimeToken(receivedObject.getDate());
142  // From
143  URL from = receivedObject.getFrom();
144  if(!from.empty())
145  {
146  encoded += char(0x02) + BitefficientFormat::getNullTerminatedString(from);
147  }
148  // Id
149  ID id = receivedObject.getId();
150  if(!id.empty())
151  {
152  encoded += char(0x03) + BitefficientFormat::getNullTerminatedString(id);
153  }
154  // Via
155  Via via = receivedObject.getVia();
156  if(!via.empty())
157  {
158  encoded += char(0x04) + BitefficientFormat::getNullTerminatedString(via);
159  }
160 
162  return encoded;
163 }
164 
165 
166 
167 } // end namespace acl
168 } // end namespace fipa
std::string ID
const ReceivedObject & getReceivedObject() const
Definition: acl_envelope.h:281
The ACLBaseEnvelope defines the layout for an envelope with which ACLMessage data can be wrapped...
Definition: acl_envelope.h:36
const ID & getId() const
ACLMessage envelope (also typed as fipa::acl::Letter) includes the base envelope and updated fields...
Definition: acl_envelope.h:349
#define FIPA_EXT_MSG_ID
std::string Via
static std::string getACLRepresentation(const representation::Type &type)
static std::string getNullTerminatedString(const std::string &sword)
const TransportBehaviour & getTransportBehaviour() const
Definition: acl_envelope.h:297
std::string URL
const Via & getVia() const
const ACLBaseEnvelopeList & getExtraEnvelopes() const
Definition: acl_envelope.h:411
const PayloadLength & getPayloadLength() const
Definition: acl_envelope.h:226
const PayloadEncoding & getPayloadEncoding() const
Definition: acl_envelope.h:236
const URL & getFrom() const
const AgentIDList & getIntendedReceivers() const
Definition: acl_envelope.h:256
std::vector< ACLBaseEnvelope > ACLBaseEnvelopeList
Definition: acl_envelope.h:19
static std::string getAgentID(const AgentID &agentId)
const base::Time & getDate() const
Definition: acl_envelope.h:246
static char getEOFCollection()
returns the very used end-of-collection marker
const base::Time & getDate() const
const std::string & getPayload() const
Definition: acl_envelope.h:473
bool contains(envelope::ParameterId id) const
Definition: acl_envelope.h:144
const representation::Type & getACLRepresentation() const
Definition: acl_envelope.h:203
const Comments & getComments() const
Definition: acl_envelope.h:191
std::string apply(const ACLEnvelope &envelope) const
const URL & getBy() const
const AgentIDList & getTo() const
Definition: acl_envelope.h:150
const ACLBaseEnvelope & getBaseEnvelope() const
Definition: acl_envelope.h:430
const AgentID & getFrom() const
Definition: acl_envelope.h:179
Foundation of Physical Intelligent Agents.
Definition: conversation.cpp:6
static std::string getAgentIDSequence(const std::vector< AgentID > &agentIds)
static std::string getBinDateTimeToken(const std::string &date1)
implements the date time token production of the grammar; not complete(w.r.t. the specification) in f...
static std::string getReceivedObject(const ReceivedObject &object)
static std::string getBinNumber(uint32_t number, bool asHex=false)
#define FIPA_BASE_MSG_ID
static std::string getBinStringNoCodetable(const std::string &sword)