Fawkes API  Fawkes Development Version
gossip_inifin.cpp
1 
2 /***************************************************************************
3  * gossip_inifin.cpp - Fawkes GossipAspect initializer/finalizer
4  *
5  * Created: Sat Jun 16 14:34:27 2012
6  * Copyright 2006-2012 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <plugins/gossip/aspect/gossip_inifin.h>
25 #include <plugins/gossip/gossip/gossip_group_manager.h>
26 #include <plugins/gossip/gossip/gossip_group.h>
27 #include <core/threading/thread_finalizer.h>
28 
29 namespace fawkes {
30 #if 0 /* just to make Emacs auto-indent happy */
31 }
32 #endif
33 
34 /** @class GossipAspectIniFin <plugins/gossip/aspect/gossip_inifin.h>
35  * GossipAspect initializer/finalizer.
36  * This initializer/finalizer will join and leave gossip groups on
37  * behalf of threads with the GossipAspect.
38  * @author Tim Niemueller
39  */
40 
41 /** Constructor. */
43  : AspectIniFin("GossipAspect")
44 {
45 }
46 
47 /** Destructor. */
49 {
50 }
51 
52 
53 
54 void
56 {
57  GossipAspect *gossip_thread;
58  gossip_thread = dynamic_cast<GossipAspect *>(thread);
59  if (gossip_thread == NULL) {
60  throw CannotInitializeThreadException("Thread '%s' claims to have the "
61  "GossipAspect, but RTTI says it "
62  "has not. ", thread->name());
63  }
64 
65  RefPtr<GossipGroup> group =
66  gossip_group_mgr_->join_group(gossip_thread->GossipAspect_group_name_);
67 
68  gossip_thread->gossip_group = group;
69 }
70 
71 void
73 {
74  GossipAspect *gossip_thread;
75  gossip_thread = dynamic_cast<GossipAspect *>(thread);
76  if (gossip_thread == NULL) {
77  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
78  "GossipAspect, but RTTI says it "
79  "has not. ", thread->name());
80  }
81 
82  gossip_group_mgr_->leave_group(gossip_thread->gossip_group);
83 }
84 
85 
86 
87 /** Set gossip group manger.
88  * @param gossip_group_mgr Gossip group manager
89  */
90 void
92 {
93  gossip_group_mgr_ = gossip_group_mgr;
94 }
95 
96 } // end namespace fawkes
virtual void leave_group(RefPtr< GossipGroup > &group)
Leave a gossip group.
GossipAspectIniFin()
Constructor.
Fawkes library namespace.
virtual RefPtr< GossipGroup > join_group(const std::string &name)
Join a group.
Thread class encapsulation of pthreads.
Definition: thread.h:42
virtual void finalize(Thread *thread)
Finalize thread.
Thread aspect to communicate with a group of robots.
Definition: gossip.h:39
Thread cannot be initialized.
RefPtr< GossipGroup > gossip_group
Gossip group to communicate with other robots.
Definition: gossip.h:48
~GossipAspectIniFin()
Destructor.
Abstract class for a Gossip group manager.
const char * name() const
Get name of thread.
Definition: thread.h:95
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
Thread cannot be finalized.
virtual void init(Thread *thread)
Initialize thread.
Aspect initializer/finalizer base class.
Definition: inifin.h:36
void set_manager(GossipGroupManager *gossip_group_mgr)
Set gossip group manger.