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 <core/threading/thread_finalizer.h>
25 #include <plugins/gossip/aspect/gossip_inifin.h>
26 #include <plugins/gossip/gossip/gossip_group.h>
27 #include <plugins/gossip/gossip/gossip_group_manager.h>
28 
29 namespace fawkes {
30 
31 /** @class GossipAspectIniFin <plugins/gossip/aspect/gossip_inifin.h>
32  * GossipAspect initializer/finalizer.
33  * This initializer/finalizer will join and leave gossip groups on
34  * behalf of threads with the GossipAspect.
35  * @author Tim Niemueller
36  */
37 
38 /** Constructor. */
40 {
41 }
42 
43 /** Destructor. */
45 {
46 }
47 
48 void
50 {
51  GossipAspect *gossip_thread;
52  gossip_thread = dynamic_cast<GossipAspect *>(thread);
53  if (gossip_thread == NULL) {
54  throw CannotInitializeThreadException("Thread '%s' claims to have the "
55  "GossipAspect, but RTTI says it "
56  "has not. ",
57  thread->name());
58  }
59 
60  RefPtr<GossipGroup> group =
61  gossip_group_mgr_->join_group(gossip_thread->GossipAspect_group_name_);
62 
63  gossip_thread->gossip_group = group;
64 }
65 
66 void
68 {
69  GossipAspect *gossip_thread;
70  gossip_thread = dynamic_cast<GossipAspect *>(thread);
71  if (gossip_thread == NULL) {
72  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
73  "GossipAspect, but RTTI says it "
74  "has not. ",
75  thread->name());
76  }
77 
78  gossip_group_mgr_->leave_group(gossip_thread->gossip_group);
79 }
80 
81 /** Set gossip group manger.
82  * @param gossip_group_mgr Gossip group manager
83  */
84 void
86 {
87  gossip_group_mgr_ = gossip_group_mgr;
88 }
89 
90 } // end namespace fawkes
fawkes::GossipAspectIniFin::finalize
virtual void finalize(Thread *thread)
Finalize thread.
Definition: gossip_inifin.cpp:67
fawkes::RefPtr
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
fawkes::GossipGroupManager
Abstract class for a Gossip group manager.
Definition: gossip_group_manager.h:63
fawkes::GossipGroupManager::join_group
virtual RefPtr< GossipGroup > join_group(const std::string &name)
Join a group.
Definition: gossip_group_manager.cpp:136
fawkes::GossipAspectIniFin::~GossipAspectIniFin
~GossipAspectIniFin()
Destructor.
Definition: gossip_inifin.cpp:44
fawkes::GossipAspect
Thread aspect to communicate with a group of robots.
Definition: gossip.h:38
fawkes::GossipGroupManager::leave_group
virtual void leave_group(RefPtr< GossipGroup > &group)
Leave a gossip group.
Definition: gossip_group_manager.cpp:154
fawkes::CannotInitializeThreadException
Thread cannot be initialized.
Definition: thread_initializer.h:34
fawkes::Thread::name
const char * name() const
Get name of thread.
Definition: thread.h:100
fawkes::AspectIniFin
Aspect initializer/finalizer base class.
Definition: inifin.h:34
fawkes::GossipAspectIniFin::GossipAspectIniFin
GossipAspectIniFin()
Constructor.
Definition: gossip_inifin.cpp:39
fawkes::GossipAspectIniFin::init
virtual void init(Thread *thread)
Initialize thread.
Definition: gossip_inifin.cpp:49
fawkes
Fawkes library namespace.
fawkes::GossipAspect::gossip_group
RefPtr< GossipGroup > gossip_group
Gossip group to communicate with other robots.
Definition: gossip.h:46
fawkes::GossipAspectIniFin::set_manager
void set_manager(GossipGroupManager *gossip_group_mgr)
Set gossip group manger.
Definition: gossip_inifin.cpp:85
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
fawkes::CannotFinalizeThreadException
Thread cannot be finalized.
Definition: thread_finalizer.h:34