fipa_acl  1.4
Public Member Functions | Static Public Member Functions | List of all members
fipa::acl::Conversation Class Reference

#include <conversation.h>

Inherits fipa::acl::ConversationObservable.

Public Member Functions

 Conversation (const std::string &owner, const fipa::acl::ConversationID &conversationId=fipa::acl::ConversationID())
 
 Conversation (const std::string &owner, const fipa::acl::ACLMessage &initiator)
 
 Conversation (const Conversation &other)
 
virtual ~Conversation ()
 
void setProtocol (const fipa::acl::Protocol &protocol)
 
fipa::acl::Protocol getProtocol () const
 
void setContentLanguage (const fipa::acl::ContentLanguage &language)
 
fipa::acl::ContentLanguage getContentLanguage () const
 
void setNumberOfSubConversations (int amount)
 
void update (const fipa::acl::ACLMessage &msg)
 
std::vector< fipa::acl::ACLMessage > getMessages () const
 
fipa::acl::ACLMessage getLastMessage () const
 
bool hasEnded () const
 
bool hasMessages () const
 
std::string getOwner () const
 
std::string toString () const
 
- Public Member Functions inherited from fipa::acl::ConversationObservable
 ConversationObservable ()
 
 ConversationObservable (const ConversationObservable &other)
 
 ConversationObservable (const fipa::acl::ConversationID &conversationId)
 
virtual ~ConversationObservable ()
 
void setConversationId (const fipa::acl::ConversationID &conversationId)
 
fipa::acl::ConversationID getConversationId () const
 
void addObserver (const ConversationObserverPtr &observer)
 
void removeObserver (const ConversationObserverPtr &observer)
 
void notify (const fipa::acl::ACLMessage &msg, conversation::EventType eventType)
 
conversation::Status getStatus () const
 
ConversationObserverList getObservers () const
 
void detachObservers ()
 
bool hasObservers () const
 

Static Public Member Functions

static fipa::acl::ConversationID generateConversationID (const std::string &topic="")
 
static std::string toString (const std::vector< fipa::acl::ACLMessage > &messages)
 

Additional Inherited Members

- Protected Attributes inherited from fipa::acl::ConversationObservable
fipa::acl::ConversationID mConversationId
 

Detailed Description

A Conversation represents a flow of messages which have the conversation id and (commonly) the protocol in common

A conversation can be observed and observers will be notified when the conversation is updated

Definition at line 257 of file conversation.h.

Constructor & Destructor Documentation

◆ Conversation() [1/3]

fipa::acl::Conversation::Conversation ( const std::string &  owner,
const fipa::acl::ConversationID &  conversationId = fipa::acl::ConversationID() 
)

Constructor of an empty conversation Owner and conversation id need to be provided

Parameters
ownerOwner of the conversation will be the current agent
conversationIdConversation id - If no conversation id is provided, one will automatically be generated

Definition at line 99 of file conversation.cpp.

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 }
static fipa::acl::ConversationID generateConversationID(const std::string &topic="")
fipa::acl::ConversationID mConversationId
Definition: conversation.h:231

◆ Conversation() [2/3]

fipa::acl::Conversation::Conversation ( const std::string &  owner,
const fipa::acl::ACLMessage &  initiator 
)

Constructor of a Conversation that requires an iniating message, i.e. includes a call to update

Definition at line 111 of file conversation.cpp.

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 }
std::string getConversationID() const
Definition: acl_message.h:227
fipa::acl::ConversationID mConversationId
Definition: conversation.h:231
void update(const fipa::acl::ACLMessage &msg)

◆ Conversation() [3/3]

fipa::acl::Conversation::Conversation ( const Conversation &  other)

Definition at line 121 of file conversation.cpp.

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 }

◆ ~Conversation()

fipa::acl::Conversation::~Conversation ( )
virtual

Conversation destructor cleans also all related commands in the command controller

Definition at line 132 of file conversation.cpp.

133 {
134 }

Member Function Documentation

◆ generateConversationID()

fipa::acl::ConversationID fipa::acl::Conversation::generateConversationID ( const std::string &  topic = "")
static

Create a new ConversationID

Definition at line 284 of file conversation.cpp.

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 }

◆ getContentLanguage()

std::string fipa::acl::Conversation::getContentLanguage ( ) const

Get content language (which is set by the initiating message)

Returns
ContentLanguage

Definition at line 136 of file conversation.cpp.

137 {
138  return mContentLanguage;
139 }

◆ getLastMessage()

fipa::acl::ACLMessage fipa::acl::Conversation::getLastMessage ( ) const

Get the last message of a conversation

Returns
Last message of the conversation

Definition at line 244 of file conversation.cpp.

245 {
246  boost::unique_lock<boost::mutex> lock(mMutex);
247  return mMessages.back();
248 }

◆ getMessages()

std::vector<fipa::acl::ACLMessage> fipa::acl::Conversation::getMessages ( ) const
inline

Get all messages in order of this conversation

Definition at line 322 of file conversation.h.

322 { return mMessages; }

◆ getOwner()

std::string fipa::acl::Conversation::getOwner ( ) const

Get the name of the owner of this conversation, this is equal to the agent name of the system

Definition at line 279 of file conversation.cpp.

280 {
281  return mOwner;
282 }

◆ getProtocol()

fipa::acl::Protocol fipa::acl::Conversation::getProtocol ( ) const
inline

Get protocol (which is set by the initiating message)

Returns
Protocol

Definition at line 291 of file conversation.h.

291 { return mProtocol; }

◆ hasEnded()

bool fipa::acl::Conversation::hasEnded ( ) const

Check if conversation has ended. This is closely related to the interaction protocol flow, i.e. only when the protocol has been validated the conversation will end

Returns
true if conversation ended, false otherwise

Definition at line 250 of file conversation.cpp.

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 }

◆ hasMessages()

bool fipa::acl::Conversation::hasMessages ( ) const

Check if this is an active conversation

Returns
true if conversation is active and has messages, false if is not active, i.e. has not started (yet)

Definition at line 273 of file conversation.cpp.

274 {
275  boost::unique_lock<boost::mutex> lock(mMutex);
276  return !mMessages.empty();
277 }

◆ setContentLanguage()

void fipa::acl::Conversation::setContentLanguage ( const fipa::acl::ContentLanguage &  language)
inline

Set content language

Definition at line 296 of file conversation.h.

296 { mContentLanguage = language; }

◆ setNumberOfSubConversations()

void fipa::acl::Conversation::setNumberOfSubConversations ( int  amount)
inline

For protocols requiring a proxy/propagate behaviour, the number of sub conversations to be started needs to be known in order to monitor the end of the conversation correctly. E.g. a broker would set this to m, as soon as he knows that m agents will be contacted.

This MUST be called BEFORE an actual message of a sub conversation is sent.

Definition at line 311 of file conversation.h.

311 { mNumberOfSubConversations = amount; }

◆ setProtocol()

void fipa::acl::Conversation::setProtocol ( const fipa::acl::Protocol &  protocol)
inline

Set the protocol for this conversation

Definition at line 285 of file conversation.h.

285 { mProtocol = protocol; }

◆ toString() [1/2]

std::string fipa::acl::Conversation::toString ( const std::vector< fipa::acl::ACLMessage > &  messages)
static

Convert message list to string

Definition at line 294 of file conversation.cpp.

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 }

◆ toString() [2/2]

std::string fipa::acl::Conversation::toString ( ) const

Convert conversation to string

Definition at line 305 of file conversation.cpp.

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 }
std::string toString() const

◆ update()

void fipa::acl::Conversation::update ( const fipa::acl::ACLMessage &  msg)

Update a conversation using an incoming message

Parameters
msgincoming or outgoing message

Definition at line 141 of file conversation.cpp.

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 }
std::string getLanguage() const
Definition: acl_message.h:268
void consumeMessage(const ACLMessage &msg)
std::string getConversationID() const
Definition: acl_message.h:227
StateId getCurrentStateId() const
Definition: statemachine.h:129
static StateMachine getStateMachine(const std::string &protocol)
std::string getContent() const
Definition: acl_message.h:294
void setSelf(const AgentID &self)
const RoleMapping & getRoleMapping() const
Definition: statemachine.h:109
std::string toString() const
Definition: role.cpp:165
std::string getPerformative() const
Definition: acl_message.h:139
fipa::acl::ConversationID mConversationId
Definition: conversation.h:231
void consumeSubStateMachineMessage(const ACLMessage &msg, const fipa::acl::StateMachine &stateMachine, int numberOfSubConversations)
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

The documentation for this class was generated from the following files: