fipa_acl  1.4
bitefficient_format.cpp
Go to the documentation of this file.
1 #include "bitefficient_format.h"
2 #include <boost/algorithm/string.hpp>
3 #include <sstream>
4 #include <base-logging/Logging.hpp>
5 
6 namespace fipa {
7 namespace acl {
8 
9 std::string BitefficientFormat::getString(const std::string& sword)
10 {
11  // StringLiteral | ByteLengthEncodedString
12  return getByteLengthEncodedString(sword);
13 }
14 
15 std::string BitefficientFormat::getByteLengthEncodedString(const std::string& sword)
16 {
17  char digitString[100];
18  int digit = sword.size();
19  snprintf(digitString,100, "#%d%c",digit,'\"');
20 
21  return digitString + sword;
22 }
23 
24 std::string BitefficientFormat::getNullTerminatedString(const std::string& string)
25 {
26  return getByteLengthEncodedString(string) + char(0x00);
27 }
28 
29 std::string BitefficientFormat::getBinDateTimeToken(const std::string& date1)
30 {
31  return char(0x20) + getBinDate(date1);
32 }
33 
34 std::string BitefficientFormat::getBinDateTimeToken(const base::Time& time)
35 {
36  return char(0x20) + getBinDate(time);
37 }
38 
39 std::string BitefficientFormat::getBinDate(const base::Time& baseTime)
40 {
41 
42  std::string time = baseTime.toString(base::Time::Milliseconds);
43 
44  // Input format should be "%Y%m%d-%H:%M:%S"
45  // Strip ':' and '-' to allow from_iso_string to work
46  boost::erase_all(time,":");
47  boost::erase_all(time,"-");
48 
49  // extend millisecond field to 4 digits
50  time.insert(14,sizeof(char),'0');
51 
52  return getBinDate(time);
53 }
54 
55 std::string BitefficientFormat::getBinDate(const std::string& date1)
56 {
57  std::string retstr;
58  unsigned int i;
59  for (i = 0; i < date1.length(); i = i + 2)
60  {
61  retstr = retstr + getCodedNaturalNumber(date1.substr(i,2));
62  }
63 
64  return retstr;
65 }
66 
67 std::string BitefficientFormat::getCodedNumber(const std::string& cn)
68 {
69  std::string retstr;
70  unsigned int i;
71  char code(0x00);
72  size_t decimal_size = cn.length();
73 
74  for(i = 0; i < decimal_size; i++)
75  {
76  if ((cn[i]>='0') && (cn[i]<='9')) code = char(code + 1 + int(cn[i]) - 48);
77  if (cn[i] == '+') code = char(code + 12);
78  if ((cn[i] == 'E') || (cn[i] == 'e')) code = char(code + 13);
79  if (cn[i] == '-') code = char(code + 14);
80  if (cn[i] == '.') code = char(code + 15);
81 
82  if (i%2 == 0)
83  {
84  code = code<<4;
85  } else {
86  retstr += code;
87  code = char(0x00);
88  }
89  }
90 
91  if (decimal_size % 2 != 0)
92  {
93  retstr += code;
94  } else {
95  retstr += char(0x00);
96  }
97 
98  return retstr;
99 }
100 
101 std::string BitefficientFormat::getCodedNaturalNumber(const std::string& cn)
102 {
103  std::string retstr;
104 
105  unsigned int i;
106  char code(0x00);
107  size_t decimal_size = cn.length();
108 
109  for(i = 0; i < decimal_size; ++i)
110  {
111  switch(cn[i])
112  {
113  // Ignoring leading zeros here,
114  case '0': code += char(0x01); break;
115  case '1': code += char(0x02); break;
116  case '2': code += char(0x03); break;
117  case '3': code += char(0x04); break;
118  case '4': code += char(0x05); break;
119  case '5': code += char(0x06); break;
120  case '6': code += char(0x07); break;
121  case '7': code += char(0x08); break;
122  case '8': code += char(0x09); break;
123  case '9': code += char(0x0a); break;
124  //case '+': code += char(0x0c); break;
125  //case 'e':
126  //case 'E': code += char(0x0d); break;
127  //case '-': code += char(0x0e); break;
128  //case '.': code += char(0x0f); break;
129  default:
130  assert(false);
131  }
132 
133 
134  if (i%2 == 0)
135  {
136  code = code<<4;
137  } else {
138  retstr += code;
139  code = char(0x00);
140  }
141  }
142 
143  if (i%2 != 0)
144  {
145  retstr += code;
146  }
147 
148  return retstr;
149 }
150 
151 std::string BitefficientFormat::getBinNumber(uint32_t number, bool asHex)
152 {
153  std::string encoded;
154  if(asHex)
155  {
156  encoded += char(0x13);
157  } else {
158  encoded += char(0x12);
159  }
160 
161  std::stringstream ss;
162  ss << number;
163 
164  encoded += getDigits(ss.str());
165  return encoded;
166 }
167 
168 std::string BitefficientFormat::getDigits(uint32_t number)
169 {
170  std::stringstream ss;
171  ss << number;
172 
173  return getDigits(ss.str());
174 }
175 
177 {
178  std::string msg = "";
179  switch(type)
180  {
182  msg += char(0x10); break;
184  msg += char(0x11); break;
185  case representation::XML:
186  msg += char(0x12); break;
187  default:
188  throw std::runtime_error("BitefficientFormat: Cannot encode unknown acl representation");
189  }
190 
191  return msg;
192 }
193 
194 std::string BitefficientFormat::getReceivedObject(const ReceivedObject& receivedObject)
195 {
196  // by
197  std::string receivedObjectString = getNullTerminatedString(receivedObject.getBy());
198  // date
199  receivedObjectString += getBinDateTimeToken(receivedObject.getDate());
200  // from
201  std::string from = receivedObject.getFrom();
202  if(!from.empty())
203  {
204  receivedObjectString += char(0x02) + getNullTerminatedString(from);
205  }
206  // id
207  std::string id = receivedObject.getId();
208  if(!id.empty())
209  {
210  receivedObjectString += char(0x03) + getNullTerminatedString(id);
211  }
212  // via
213  std::string via = receivedObject.getVia();
214  if(!via.empty())
215  {
216  receivedObjectString += char(0x04) + getNullTerminatedString(via);
217  }
218 
219  const UserdefinedParameterList& list = receivedObject.getUserdefinedParameters();
220  std::vector<UserdefParam>::const_iterator cit = list.begin();
221  for(; cit != list.end(); ++cit)
222  {
223  receivedObjectString += char(0x00) + getNullTerminatedString(cit->getName()) + getNullTerminatedString(cit->getValue());
224  }
225 
226  receivedObjectString += getEOFCollection();
227 
228  return receivedObjectString;
229 }
230 
231 std::string BitefficientFormat::getAgentID(const AgentID& agentId)
232 {
233  std::string agentIdString = char(0x02) + getNullTerminatedString(agentId.getName());
234  // addresses (optional)
235  {
236  const Addresses& addresses = agentId.getAddresses();
237  if(!addresses.empty())
238  {
239  Addresses::const_iterator cit = addresses.begin();
240  agentIdString += char(0x02);
241  for(; cit != addresses.end(); ++cit)
242  {
243  agentIdString += getNullTerminatedString(*cit);
244  }
245  agentIdString += getEOFCollection();
246  }
247  }
248  // resolvers(optional)
249  {
250  const Resolvers& resolvers = agentId.getResolvers();
251  if(!resolvers.empty())
252  {
253  Resolvers::const_iterator cit = resolvers.begin();
254  agentIdString += char(0x03);
255  for(; cit != resolvers.end(); ++cit)
256  {
257  agentIdString += getAgentID(*cit);
258  }
259  agentIdString += getEOFCollection();
260  }
261  }
262  // userdefined parameters
263  {
264  const UserdefinedParameterList& userdefParams = agentId.getUserdefParams();
265  if(!userdefParams.empty())
266  {
267  UserdefinedParameterList::const_iterator cit = userdefParams.begin();
268  for(; cit != userdefParams.end(); ++cit)
269  {
270  agentIdString += char(0x05);
271  agentIdString += getNullTerminatedString(cit->getName());
272  agentIdString += getBinStringNoCodetable(cit->getValue());
273  }
274  }
275  }
276  agentIdString += getEOFCollection();
277  return agentIdString;
278 }
279 
280 std::string BitefficientFormat::getAgentIDSequence(const std::vector<AgentID>& agentIds)
281 {
282  std::vector<AgentID>::const_iterator cit = agentIds.begin();
283  std::string sequence;
284  for(;cit != agentIds.end(); ++cit)
285  {
286  sequence += getAgentID(*cit);
287  }
288  sequence += getEOFCollection();
289  return sequence;
290 }
291 
292 } // end namespace acl
293 } // end namespace fipa
static std::string getCodedNaturalNumber(const std::string &cn)
Allow proper generation of natural numbers, will remove any leading zeros.
static std::string getString(const std::string &sword)
const UserdefinedParameterList & getUserdefinedParameters() const
const Resolvers & getResolvers() const
Get list of resolvers.
Definition: agent_id.h:127
static std::string getACLRepresentation(const representation::Type &type)
static std::string getBinDate(const base::Time &time)
static std::string getNullTerminatedString(const std::string &sword)
const URL & getFrom() const
const std::string & getName() const
setter and getter methods for all fields; they do not result in deep-copies assignments/retreivals, but this can be easily changed if needed through the overloaded operator which do
Definition: agent_id.h:90
static std::string getDigits(const std::string &numberString)
std::vector< UserdefParam > UserdefinedParameterList
Definition: userdef_param.h:94
const URL & getBy() const
const base::Time & getDate() const
const std::vector< std::string > & getAddresses() const
Retrieve list of addresses.
Definition: agent_id.h:110
static std::string getCodedNumber(const std::string &cn)
implements a coded number passed as a string it goes through it digit by digit(char by char) ...
AgentIDList Resolvers
Definition: agent_id.h:27
static std::string getByteLengthEncodedString(const std::string &sword)
static std::string getAgentID(const AgentID &agentId)
std::vector< std::string > Addresses
Definition: agent_id.h:28
const Via & getVia() const
static char getEOFCollection()
returns the very used end-of-collection marker
const ID & getId() const
const std::vector< UserdefParam > & getUserdefParams() const
Get the all userdefined parameter.
Definition: agent_id.h:150
Implements the general AgentID functionality, which is present throughout the fipa specifications(FIP...
Definition: agent_id.h:37
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)
static std::string getBinStringNoCodetable(const std::string &sword)