3 #include <base-logging/Logging.hpp> 8 namespace conversation {
31 boost::unique_lock<boost::mutex> lock(mEventsMutex);
32 return !mEvents.empty();
37 boost::unique_lock<boost::mutex> lock(mEventsMutex);
38 mEvents.push_back(event);
39 mCondition.notify_all();
46 boost::unique_lock<boost::mutex> lock(mutex);
49 mCondition.wait(lock);
54 assert(getNextEvent(event));
61 boost::unique_lock<boost::mutex> lock(mEventsMutex);
67 event = mEvents.front();
68 mEvents.erase(mEvents.begin());
70 LOG_INFO(
"Retrieve event: type: '%s', msg content: '%s'", conversation::EventTypeTxt[event.
type].c_str(),
event.msg.getContent().c_str());
76 std::vector<fipa::acl::ConversationID> conversationIds;
78 boost::unique_lock<boost::mutex> lock(mObservablesMutex);
79 std::vector<ConversationObservablePtr>::const_iterator it = mObservables.begin();
80 for(; it != mObservables.end(); ++it)
82 conversationIds.push_back((*it)->getConversationId());
85 return conversationIds;
90 boost::unique_lock<boost::mutex> lock(mObservablesMutex);
92 std::vector<ConversationObservablePtr>::const_iterator it = std::find(mObservables.begin(), mObservables.end(), observable);
93 if(it == mObservables.end())
95 mObservables.push_back(observable);
102 , mNumberOfSubConversations(0)
108 LOG_DEBUG(
"Conversation created with id: %s\n", conversationId.c_str());
114 , mNumberOfSubConversations(0)
117 LOG_DEBUG(
"Conversation created with id: %s\n", initiator.
getConversationID().c_str());
123 , mOwner(other.mOwner)
124 , mProtocol(other.mProtocol)
125 , mContentLanguage(other.mContentLanguage)
126 , mNumberOfSubConversations(other.mNumberOfSubConversations)
127 , mMessages(other.mMessages)
128 , mStateMachine(other.mStateMachine)
138 return mContentLanguage;
143 boost::unique_lock<boost::mutex> lock(mMutex);
149 bool newConversation =
false;
153 if(mProtocol.empty())
157 if(!protocol.empty())
162 mProtocol = protocol;
167 LOG_ERROR(
"Protocol not set");
168 throw std::runtime_error(
"Protocol not set");
170 }
catch(
const std::runtime_error& e)
172 LOG_FATAL(
"Conversation could not retrieve statemachine for protocol '%s'. Check if protocol specification was loaded -- '%s'", protocol.c_str(), e.what());
176 newConversation =
true;
183 }
catch(
const std::runtime_error& e)
185 std::string errorMsg =
"Conversation: unexpected message with performative '" + msg.
getPerformative() +
"' for the protocol '" + msg.
getProtocol() +
"' ";
188 errorMsg +=
" -- " + std::string(e.what()) +
"\n";
192 notifyAll(msg,
false);
196 LOG_WARN(
"Conversation: received message has no protocol being set. Current conversation using '%s'", mProtocol.c_str());
201 LOG_INFO(
"Conversation: received message has not language being set. Current conversation using '%s'", mContentLanguage.c_str());
204 LOG_INFO(
"Conversation: message with different content language being inserted: current '%s' - to be inserted '%s'", mContentLanguage.c_str(), msg.
getLanguage().c_str());
210 }
catch(
const std::runtime_error& e)
212 std::string errorMsg =
"Conversation: unexpected message with performative '" + msg.
getPerformative() +
"' for the protocol '" + msg.
getProtocol() +
"' ";
215 errorMsg +=
" -- " + std::string(e.what()) +
"\n";
219 notifyAll(msg, newConversation);
230 mMessages.push_back(msg);
237 }
else if(newConversation) {
246 boost::unique_lock<boost::mutex> lock(mMutex);
247 return mMessages.back();
256 LOG_DEBUG(
"Conversation did not end");
260 LOG_DEBUG(
"Conversation ended");
263 catch(
const std::runtime_error& e)
268 LOG_WARN_S <<
"Runtime error when testing if conversation ended. Therefore not ended. Message: " << e.what();
275 boost::unique_lock<boost::mutex> lock(mMutex);
276 return !mMessages.empty();
289 char conversationId[37];
290 uuid_unparse(uuid, conversationId);
291 return std::string(conversationId) +
"--" + topic +
"--" + base::Time::now().toString();
296 std::stringstream ss;
297 std::vector<fipa::acl::ACLMessage>::const_iterator cit = messages.begin();
298 for(; cit != messages.end(); ++cit)
300 ss << cit->toString() << std::endl;
307 std::stringstream ss;
308 ss <<
"Conversation: " << std::endl;
309 if(!mMessages.empty())
311 ss <<
" id: " << mMessages.front().getConversationID();
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;
332 , mStatus(other.mStatus)
333 , mObservers(other.mObservers)
348 boost::unique_lock<boost::mutex> lock(mObserverMutex);
349 return !mObservers.empty();
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())
358 mObservers.push_back(observer);
365 boost::unique_lock<boost::mutex> lock(mObserverMutex);
366 ConversationObserverList::iterator it = std::find(mObservers.begin(), mObservers.end(), observer);
367 if(it != mObservers.end())
369 mObservers.erase(it);
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)
416 boost::unique_lock<boost::mutex> lock(mObserverMutex);
std::string ConversationID
std::string getLanguage() const
void consumeMessage(const ACLMessage &msg)
std::vector< fipa::acl::ConversationID > getConversationIdsOfObservables() const
boost::shared_ptr< ConversationObservable > ConversationObservablePtr
std::string getConversationID() const
StateId getCurrentStateId() const
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
boost::shared_ptr< ConversationObserver > ConversationObserverPtr
std::string getContent() const
bool getNextEvent(conversation::Event &msg)
void notify(const fipa::acl::ACLMessage &msg, conversation::EventType eventType)
bool inFailureState() const
std::string getOwner() const
void setSelf(const AgentID &self)
conversation::Status getStatus() const
const RoleMapping & getRoleMapping() const
std::string toString() const
bool inFinalState() const
bool hasObservers() const
std::string toString() const
std::string getPerformative() const
void addObserver(const ConversationObserverPtr &observer)
This class provides a representation of a message conforming to the FIPA specification SC00061...
Conversation(const std::string &owner, const fipa::acl::ConversationID &conversationId=fipa::acl::ConversationID())
fipa::acl::ConversationID mConversationId
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
Implements the general AgentID functionality, which is present throughout the fipa specifications(FIP...
Foundation of Physical Intelligent Agents.
fipa::acl::ACLMessage msg