Fawkes API  Fawkes Development Version
gex_sender_thread.cpp
1 
2 /***************************************************************************
3  * gex_sender_thread.cpp - Gossip Example Plugin - Sender
4  *
5  * Created: Wed Mar 05 14:29:19 2014
6  * Copyright 2006-2014 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include "gex_sender_thread.h"
23 
24 #include "TestMessage.pb.h"
25 
26 #include <plugins/gossip/gossip/gossip_group.h>
27 
28 using namespace fawkes;
29 
30 /** @class GossipExampleSenderThread "clips-protobuf-thread.h"
31  * Gossip Example Plugin Thread - Sender.
32  * @author Tim Niemueller
33  */
34 
35 /** Constructor. */
37 : Thread("GossipExampleSenderThread", Thread::OPMODE_WAITFORWAKEUP),
38  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_ACT),
39  GossipAspect("example")
40 {
41 }
42 
43 /** Destructor. */
45 {
46 }
47 
48 void
50 {
51  last_sent_ = new Time(clock);
52  counter_ = 0;
53 }
54 
55 void
57 {
58  delete last_sent_;
59 }
60 
61 void
63 {
64  fawkes::Time now(clock);
65  if (now - last_sent_ >= 2.0) {
66  *last_sent_ = now;
67 
68  logger->log_debug(name(), "Sending");
69 
70  gossip_example::TestMessage m;
71  m.set_counter(++counter_);
72  m.set_sec(now.get_sec());
73  m.set_nsec(now.get_nsec());
74  gossip_group->broadcast(m);
75  }
76 }
virtual void loop()
Code to execute in the thread.
virtual void finalize()
Finalize the thread.
virtual void init()
Initialize the thread.
virtual ~GossipExampleSenderThread()
Destructor.
GossipExampleSenderThread()
Constructor.
Thread aspect to use blocked timing.
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:42
Thread aspect to communicate with a group of robots.
Definition: gossip.h:38
RefPtr< GossipGroup > gossip_group
Gossip group to communicate with other robots.
Definition: gossip.h:46
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
A class for handling time.
Definition: time.h:93
long get_nsec() const
Get nanoseconds.
Definition: time.h:132
long get_sec() const
Get seconds.
Definition: time.h:117
Fawkes library namespace.