fipa_acl  1.4
conversation.cpp
Go to the documentation of this file.
1 #include "conversation.h"
2 #include <uuid/uuid.h>
3 #include <base-logging/Logging.hpp>
4 
5 namespace fipa {
6 namespace acl {
7 
8 namespace conversation {
9 
11  : msg()
12  , type(UNKNOWN)
13  , timestamp()
14 {}
15 
17  : msg(_msg)
18  , type(_type)
19  , timestamp(base::Time::now())
20 {}
21 
22 } // end namespace conversation
23 
24 fipa::acl::StateMachineFactory Conversation::msStateMachineFactory;
25 
27 {}
28 
30 {
31  boost::unique_lock<boost::mutex> lock(mEventsMutex);
32  return !mEvents.empty();
33 }
34 
35 void ConversationObserver::update(const conversation::Event& event)
36 {
37  boost::unique_lock<boost::mutex> lock(mEventsMutex);
38  mEvents.push_back(event);
39  mCondition.notify_all();
40 }
41 
43 {
44  boost::mutex mutex;
45  {
46  boost::unique_lock<boost::mutex> lock(mutex);
47  while(!hasEvents())
48  {
49  mCondition.wait(lock);
50  }
51  }
52 
53  conversation::Event event;
54  assert(getNextEvent(event));
55 
56  return event;
57 }
58 
60 {
61  boost::unique_lock<boost::mutex> lock(mEventsMutex);
62  if(mEvents.empty())
63  {
64  return false;
65  }
66 
67  event = mEvents.front();
68  mEvents.erase(mEvents.begin());
69 
70  LOG_INFO("Retrieve event: type: '%s', msg content: '%s'", conversation::EventTypeTxt[event.type].c_str(), event.msg.getContent().c_str());
71  return true;
72 }
73 
74 std::vector<fipa::acl::ConversationID> ConversationObserver::getConversationIdsOfObservables() const
75 {
76  std::vector<fipa::acl::ConversationID> conversationIds;
77 
78  boost::unique_lock<boost::mutex> lock(mObservablesMutex);
79  std::vector<ConversationObservablePtr>::const_iterator it = mObservables.begin();
80  for(; it != mObservables.end(); ++it)
81  {
82  conversationIds.push_back((*it)->getConversationId());
83  }
84 
85  return conversationIds;
86 }
87 
88 void ConversationObserver::registerObservable(const ConversationObservablePtr& observable)
89 {
90  boost::unique_lock<boost::mutex> lock(mObservablesMutex);
91 
92  std::vector<ConversationObservablePtr>::const_iterator it = std::find(mObservables.begin(), mObservables.end(), observable);
93  if(it == mObservables.end())
94  {
95  mObservables.push_back(observable);
96  }
97 }
98 
99 Conversation::Conversation(const std::string& owner, const fipa::acl::ConversationID& conversationId)
100  : ConversationObservable(conversationId)
101  , mOwner(owner)
102  , mNumberOfSubConversations(0)
103 {
104  if(mConversationId.empty())
105  {
107  }
108  LOG_DEBUG("Conversation created with id: %s\n", conversationId.c_str());
109 }
110 
111 Conversation::Conversation(const std::string& owner, const fipa::acl::ACLMessage& initiator)
113  , mOwner(owner)
114  , mNumberOfSubConversations(0)
115 {
116  update(initiator);
117  LOG_DEBUG("Conversation created with id: %s\n", initiator.getConversationID().c_str());
118  assert(!mConversationId.empty());
119 }
120 
122  : ConversationObservable(other)
123  , mOwner(other.mOwner)
124  , mProtocol(other.mProtocol)
125  , mContentLanguage(other.mContentLanguage)
126  , mNumberOfSubConversations(other.mNumberOfSubConversations)
127  , mMessages(other.mMessages)
128  , mStateMachine(other.mStateMachine)
129 {
130 }
131 
133 {
134 }
135 
137 {
138  return mContentLanguage;
139 }
140 
142 {
143  boost::unique_lock<boost::mutex> lock(mMutex);
144 
145  LOG_INFO("Update conversation: id '%s'", msg.getConversationID().c_str());
146  LOG_INFO("Update message: performative: '%s' content: '%s'", msg.getPerformative().c_str(), msg.getContent().c_str());
147 
148 
149  bool newConversation = false;
150  try {
151  // If protocol is unset we don't have a valid message
152  // Thus at this point the conversation is not yet initialized
153  if(mProtocol.empty())
154  {
155  std::string protocol = msg.getProtocol();
156  try {
157  if(!protocol.empty())
158  {
159  mStateMachine = msStateMachineFactory.getStateMachine(protocol);
160  mStateMachine.setSelf( fipa::acl::AgentID(mOwner) );
161 
162  mProtocol = protocol;
163  mContentLanguage = msg.getLanguage();
165 
166  } else {
167  LOG_ERROR("Protocol not set");
168  throw std::runtime_error("Protocol not set");
169  }
170  } catch(const std::runtime_error& e)
171  {
172  LOG_FATAL("Conversation could not retrieve statemachine for protocol '%s'. Check if protocol specification was loaded -- '%s'", protocol.c_str(), e.what());
173  throw;
174  }
175 
176  newConversation = true;
177 
178  } else if( mProtocol != msg.getProtocol())
179  {
180  // This probably means, it's a subProtocol message
181  try {
182  mStateMachine.consumeSubStateMachineMessage(msg, msStateMachineFactory.getStateMachine(msg.getProtocol()), mNumberOfSubConversations);
183  } catch(const std::runtime_error& e)
184  {
185  std::string errorMsg = "Conversation: unexpected message with performative '" + msg.getPerformative() + "' for the protocol '" + msg.getProtocol() + "' ";
186  errorMsg += " current state: '" + mStateMachine.getCurrentStateId() + "',";
187  errorMsg += " role mapping -- " + mStateMachine.getRoleMapping().toString();
188  errorMsg += " -- " + std::string(e.what()) + "\n";
189  throw conversation::ProtocolException(errorMsg);
190  }
191 
192  notifyAll(msg, false);
193  return;
194  } else if( msg.getProtocol().empty())
195  {
196  LOG_WARN("Conversation: received message has no protocol being set. Current conversation using '%s'", mProtocol.c_str());
197  }
198 
199  if( msg.getLanguage().empty())
200  {
201  LOG_INFO("Conversation: received message has not language being set. Current conversation using '%s'", mContentLanguage.c_str());
202  } else if(mContentLanguage != msg.getLanguage())
203  {
204  LOG_INFO("Conversation: message with different content language being inserted: current '%s' - to be inserted '%s'", mContentLanguage.c_str(), msg.getLanguage().c_str());
205  }
206 
207  // update the message state machine
208  try {
209  mStateMachine.consumeMessage(msg);
210  } catch(const std::runtime_error& e)
211  {
212  std::string errorMsg = "Conversation: unexpected message with performative '" + msg.getPerformative() + "' for the protocol '" + msg.getProtocol() + "' ";
213  errorMsg += " current state: '" + mStateMachine.getCurrentStateId() + "',";
214  errorMsg += " role mapping -- " + mStateMachine.getRoleMapping().toString();
215  errorMsg += " -- " + std::string(e.what()) + "\n";
216  throw conversation::ProtocolException(errorMsg);
217  }
218 
219  notifyAll(msg, newConversation);
220 
221  } catch(...)
222  {
223  // Notify only to handle internally
224  throw;
225  }
226 }
227 
228 void Conversation::notifyAll(const fipa::acl::ACLMessage& msg, bool newConversation)
229 {
230  mMessages.push_back(msg);
231 
232  if(mStateMachine.inFailureState())
233  {
235  } else if(hasEnded()) {
237  } else if(newConversation) {
239  } else {
241  }
242 }
243 
245 {
246  boost::unique_lock<boost::mutex> lock(mMutex);
247  return mMessages.back();
248 }
249 
251 {
252  try
253  {
254  if(!mStateMachine.inFinalState())
255  {
256  LOG_DEBUG("Conversation did not end");
257  return false;
258 
259  }
260  LOG_DEBUG("Conversation ended");
261  return true;
262  }
263  catch(const std::runtime_error& e)
264  {
265  // This very probably means the state machine has not been initialized properly,
266  // as there was no message yet to know the protocol. Therefore, technically
267  // the conversation did not end!
268  LOG_WARN_S << "Runtime error when testing if conversation ended. Therefore not ended. Message: " << e.what();
269  return false;
270  }
271 }
272 
274 {
275  boost::unique_lock<boost::mutex> lock(mMutex);
276  return !mMessages.empty();
277 }
278 
279 std::string Conversation::getOwner() const
280 {
281  return mOwner;
282 }
283 
285 {
286  uuid_t uuid;
287  uuid_generate(uuid);
288 
289  char conversationId[37];
290  uuid_unparse(uuid, conversationId);
291  return std::string(conversationId) + "--" + topic + "--" + base::Time::now().toString();
292 }
293 
294 std::string Conversation::toString(const std::vector<fipa::acl::ACLMessage>& messages)
295 {
296  std::stringstream ss;
297  std::vector<fipa::acl::ACLMessage>::const_iterator cit = messages.begin();
298  for(; cit != messages.end(); ++cit)
299  {
300  ss << cit->toString() << std::endl;
301  }
302  return ss.str();
303 }
304 
305 std::string Conversation::toString() const
306 {
307  std::stringstream ss;
308  ss << "Conversation: " << std::endl;
309  if(!mMessages.empty())
310  {
311  ss << " id: " << mMessages.front().getConversationID();
312  }
313  ss << std::endl;
314  ss << " owner: " << mOwner << std::endl;
315  ss << " protocol: " << mProtocol << std::endl;
316  ss << " language: " << mContentLanguage << std::endl;
317  ss << " # subconversations: " << mNumberOfSubConversations << std::endl;
318  ss << " # messages " << mMessages.size() << std::endl;
319  ss << "BEGIN " << std::endl;
320  ss << toString(mMessages) << std::endl;
321  ss << "END" << std::endl;
322 
323  return ss.str();
324 }
325 
327  : mStatus(conversation::UNINITIALIZED)
328 {}
329 
332  , mStatus(other.mStatus)
333  , mObservers(other.mObservers)
334 {}
335 
337  : mConversationId(conversationId)
338  , mStatus(conversation::UNINITIALIZED)
339 {}
340 
342 {
343  return mConversationId;
344 }
345 
347 {
348  boost::unique_lock<boost::mutex> lock(mObserverMutex);
349  return !mObservers.empty();
350 }
351 
353 {
354  boost::unique_lock<boost::mutex> lock(mObserverMutex);
355  ConversationObserverList::const_iterator it = std::find(mObservers.begin(), mObservers.end(), observer);
356  if(it == mObservers.end())
357  {
358  mObservers.push_back(observer);
359  observer->registerObservable(ConversationObservablePtr(this, NullDeleter()));
360  }
361 }
362 
364 {
365  boost::unique_lock<boost::mutex> lock(mObserverMutex);
366  ConversationObserverList::iterator it = std::find(mObservers.begin(), mObservers.end(), observer);
367  if(it != mObservers.end())
368  {
369  mObservers.erase(it);
370  }
371 }
372 
374 {
375  boost::unique_lock<boost::mutex> lock(mObserverMutex);
376  LOG_INFO("notify: message event: '%s', message content: '%s'", conversation::EventTypeTxt[eventType].c_str(), msg.getContent().c_str());
377  ConversationObserverList::iterator it = mObservers.begin();
378  for(; it != mObservers.end(); ++it)
379  {
380  (*it)->update( conversation::Event(msg, eventType) );
381  }
382 
383  switch(eventType)
384  {
386  assert(mStatus == conversation::UNINITIALIZED);
387  mStatus = conversation::RUNNING;
388  break;
390  mStatus = conversation::ENDED;
391  break;
393  mStatus = conversation::RUNNING;
394  break;
396  mStatus = conversation::FAILED;
397  break;
398  default:
399  break;
400  }
401 
402 }
403 
405 {
406  return mStatus;
407 }
408 
410 {
411  mObservers.clear();
412 }
413 
415 {
416  boost::unique_lock<boost::mutex> lock(mObserverMutex);
417  return mObservers;
418 }
419 
420 } // end namespace acl
421 } // end namespace fipa
std::string ConversationID
Definition: acl_message.h:28
std::string getLanguage() const
Definition: acl_message.h:268
void consumeMessage(const ACLMessage &msg)
std::vector< fipa::acl::ConversationID > getConversationIdsOfObservables() const
boost::shared_ptr< ConversationObservable > ConversationObservablePtr
Definition: conversation.h:21
std::string getConversationID() const
Definition: acl_message.h:227
StateId getCurrentStateId() const
Definition: statemachine.h:129
static fipa::acl::ConversationID generateConversationID(const std::string &topic="")
fipa::acl::ConversationID getConversationId() const
static StateMachine getStateMachine(const std::string &protocol)
std::vector< ConversationObserverPtr > ConversationObserverList
Definition: conversation.h:22
boost::shared_ptr< ConversationObserver > ConversationObserverPtr
Definition: conversation.h:18
std::string getContent() const
Definition: acl_message.h:294
bool getNextEvent(conversation::Event &msg)
void notify(const fipa::acl::ACLMessage &msg, conversation::EventType eventType)
std::string getOwner() const
void setSelf(const AgentID &self)
conversation::Status getStatus() const
const RoleMapping & getRoleMapping() const
Definition: statemachine.h:109
std::string toString() const
std::string toString() const
Definition: role.cpp:165
std::string getPerformative() const
Definition: acl_message.h:139
void addObserver(const ConversationObserverPtr &observer)
This class provides a representation of a message conforming to the FIPA specification SC00061...
Definition: acl_message.h:55
Conversation(const std::string &owner, const fipa::acl::ConversationID &conversationId=fipa::acl::ConversationID())
fipa::acl::ConversationID mConversationId
Definition: conversation.h:231
conversation::Event waitForNextEvent()
void consumeSubStateMachineMessage(const ACLMessage &msg, const fipa::acl::StateMachine &stateMachine, int numberOfSubConversations)
void removeObserver(const ConversationObserverPtr &observer)
ConversationObserverList getObservers() const
fipa::acl::ContentLanguage getContentLanguage() const
void update(const fipa::acl::ACLMessage &msg)
fipa::acl::ACLMessage getLastMessage() const
std::string getProtocol() const
Definition: acl_message.h:238
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:5
fipa::acl::ACLMessage msg
Definition: conversation.h:51