fipa_acl  1.4
conversation.h
Go to the documentation of this file.
1 #ifndef FIPA_ACL_CONVERSATION_H
2 #define FIPA_ACL_CONVERSATION_H
3 
4 #include <vector>
5 #include <list>
6 #include <boost/shared_ptr.hpp>
7 #include <boost/thread/mutex.hpp>
8 #include <boost/thread/condition.hpp>
9 #include <fipa_acl/message_generator/acl_message.h>
10 #include <fipa_acl/conversation_monitor/transition.h>
11 #include <fipa_acl/conversation_monitor/statemachine_factory.h>
12 #include <base/Time.hpp>
13 
14 namespace fipa {
15 namespace acl {
16 
17 class ConversationObserver;
19 
20 typedef boost::shared_ptr<ConversationObserver> ConversationObserverPtr;
21 typedef boost::shared_ptr<ConversationObservable> ConversationObservablePtr;
22 typedef std::vector<ConversationObserverPtr> ConversationObserverList;
23 
27 namespace conversation {
32 
37 
38  static const std::string EventTypeTxt[] = { "UNKNOWN", "START_OF_CONVERSATION", "INTERMEDIATE_UPDATE", "END_OF_CONVERSATION", "FAILURE" };
39  static const std::string StatusTxt[] = { "UNINITIALIZED", "RUNNING", "ENDED", "FAILED" };
40 
45  struct Event
46  {
47  Event();
48 
49  Event(const fipa::acl::ACLMessage& _msg, EventType _type);
50 
53  base::Time timestamp;
54  };
55 
56  struct ProtocolException : public std::runtime_error
57  {
59  : std::runtime_error("fipa::acl::conversation::ProtocolException")
60  {}
61 
62  ProtocolException(const std::string& msg)
63  : std::runtime_error("fipa::acl::conversation::ProtocolException: '" + msg + "'" )
64  {}
65 
66  };
67 
68  struct InvalidOperation : public std::runtime_error
69  {
71  : std::runtime_error("fipa::acl::conversation::InvalidOperation")
72  {}
73 
74  InvalidOperation(const std::string& msg)
75  : std::runtime_error("fipa::acl::conversation::InvalidOperation: '" + msg + "'" )
76  {}
77 
78  };
79 }
80 
86 {
87  void operator()(void const *) const {}
88 };
89 
96 {
97  friend class ConversationObservable;
98 public:
100 
107 
111  bool hasEvents() const;
112 
117  conversation::Event waitForNextEvent();
118 
122  bool getNextEvent(conversation::Event& msg);
123 
127  std::vector<fipa::acl::ConversationID> getConversationIdsOfObservables() const;
128 
129 private:
133  void update(const conversation::Event& event);
134 
140  void registerObservable(const ConversationObservablePtr& observable);
141 
142  // Condition to wait for new event, when operating in a blocking fashion
143  boost::condition mCondition;
144  mutable boost::mutex mEventsMutex;
145 
146  // List of events this observer has been notified on
147  // events are stored in incoming order (however conversation::Event also contains
148  // a timestamp)
149  std::vector<conversation::Event> mEvents;
150 
151  mutable boost::mutex mObservablesMutex;
152  // List of observables this observer is attached to
153  std::vector<ConversationObservablePtr> mObservables;
154 };
155 
160 {
161 public:
166 
168 
172  ConversationObservable(const fipa::acl::ConversationID& conversationId);
173 
178 
182  void setConversationId(const fipa::acl::ConversationID& conversationId) { mConversationId = conversationId; }
183 
188  fipa::acl::ConversationID getConversationId() const;
189 
193  void addObserver(const ConversationObserverPtr& observer);
194 
198  void removeObserver(const ConversationObserverPtr& observer);
199 
203  void notify(const fipa::acl::ACLMessage& msg, conversation::EventType eventType);
204 
208  conversation::Status getStatus() const;
209 
213  ConversationObserverList getObservers() const;
214 
219  void detachObservers();
220 
225  bool hasObservers() const;
226 
227 protected:
232 
233 private:
237  mutable boost::mutex mObserverMutex;
241  conversation::Status mStatus;
242 
246  ConversationObserverList mObservers;
247 };
248 
249 
258 {
259 public:
260 
267  Conversation(const std::string& owner, const fipa::acl::ConversationID& conversationId = fipa::acl::ConversationID());
268 
273  Conversation(const std::string& owner, const fipa::acl::ACLMessage& initiator);
274 
275  Conversation(const Conversation& other);
276 
280  virtual ~Conversation();
281 
285  void setProtocol(const fipa::acl::Protocol& protocol) { mProtocol = protocol; }
286 
291  fipa::acl::Protocol getProtocol() const { return mProtocol; }
292 
296  void setContentLanguage(const fipa::acl::ContentLanguage& language) { mContentLanguage = language; }
297 
302  fipa::acl::ContentLanguage getContentLanguage() const;
303 
311  void setNumberOfSubConversations(int amount) { mNumberOfSubConversations = amount; }
312 
317  void update(const fipa::acl::ACLMessage& msg);
318 
322  std::vector<fipa::acl::ACLMessage> getMessages() const { return mMessages; }
323 
328  fipa::acl::ACLMessage getLastMessage() const;
329 
337  bool hasEnded() const;
338 
343  bool hasMessages() const;
344 
349  std::string getOwner() const;
350 
354  static fipa::acl::ConversationID generateConversationID(const std::string& topic = "");
355 
359  static std::string toString(const std::vector<fipa::acl::ACLMessage>& messages);
360 
364  std::string toString() const;
365 
366 private:
367 
371  void notifyAll(const fipa::acl::ACLMessage& msg, bool newConversation);
372 
376  std::string mOwner;
377 
379  fipa::acl::Protocol mProtocol;
380 
382  fipa::acl::ContentLanguage mContentLanguage;
383 
385  int mNumberOfSubConversations;
386 
390  std::vector<fipa::acl::ACLMessage> mMessages;
391 
392  mutable boost::mutex mMutex;
393 
398  fipa::acl::StateMachine mStateMachine;
399 
404  static fipa::acl::StateMachineFactory msStateMachineFactory;
405 
406 };
407 
408 typedef boost::shared_ptr<Conversation> ConversationPtr;
409 
410 } // end namespace acl
411 } // end namespace fipa
412 #endif //FIPA_ACL_CONVERSATION_H
std::string ConversationID
Definition: acl_message.h:28
std::string Protocol
Definition: acl_message.h:29
boost::shared_ptr< ConversationObservable > ConversationObservablePtr
Definition: conversation.h:21
void setConversationId(const fipa::acl::ConversationID &conversationId)
Definition: conversation.h:182
std::vector< ConversationObserverPtr > ConversationObserverList
Definition: conversation.h:22
boost::shared_ptr< Conversation > ConversationPtr
Definition: conversation.h:408
std::vector< fipa::acl::ACLMessage > getMessages() const
Definition: conversation.h:322
boost::shared_ptr< ConversationObserver > ConversationObserverPtr
Definition: conversation.h:18
Definition: debug.h:28
void operator()(void const *) const
Definition: conversation.h:87
fipa::acl::Protocol getProtocol() const
Definition: conversation.h:291
void setNumberOfSubConversations(int amount)
Definition: conversation.h:311
std::string ContentLanguage
Definition: acl_message.h:30
ProtocolException(const std::string &msg)
Definition: conversation.h:62
This class provides a representation of a message conforming to the FIPA specification SC00061...
Definition: acl_message.h:55
fipa::acl::ConversationID mConversationId
Definition: conversation.h:231
void setProtocol(const fipa::acl::Protocol &protocol)
Definition: conversation.h:285
Foundation of Physical Intelligent Agents.
Definition: conversation.cpp:6
InvalidOperation(const std::string &msg)
Definition: conversation.h:74
fipa::acl::ACLMessage msg
Definition: conversation.h:51
void setContentLanguage(const fipa::acl::ContentLanguage &language)
Definition: conversation.h:296