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 #include <plugins/gossip/gossip/gossip_group.h>
24 
25 #include "TestMessage.pb.h"
26 
27 using namespace fawkes;
28 
29 /** @class GossipExampleSenderThread "clips-protobuf-thread.h"
30  * Gossip Example Plugin Thread - Sender.
31  * @author Tim Niemueller
32  */
33 
34 /** Constructor. */
36  : Thread("GossipExampleSenderThread", Thread::OPMODE_WAITFORWAKEUP),
37  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_ACT),
38  GossipAspect("example")
39 {
40 }
41 
42 
43 /** Destructor. */
45 {
46 }
47 
48 
49 void
51 {
52  last_sent_ = new Time(clock);
53  counter_ = 0;
54 }
55 
56 
57 void
59 {
60  delete last_sent_;
61 }
62 
63 
64 void
66 {
67  fawkes::Time now(clock);
68  if (now - last_sent_ >= 2.0) {
69  *last_sent_ = now;
70 
71  logger->log_debug(name(), "Sending");
72 
73  gossip_example::TestMessage m;
74  m.set_counter(++counter_);
75  m.set_sec(now.get_sec());
76  m.set_nsec(now.get_nsec());
77  gossip_group->broadcast(m);
78  }
79 }
Fawkes library namespace.
A class for handling time.
Definition: time.h:91
Thread class encapsulation of pthreads.
Definition: thread.h:42
Thread aspect to communicate with a group of robots.
Definition: gossip.h:39
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
Clock * clock
By means of this member access to the clock is given.
Definition: clock.h:45
Thread aspect to use blocked timing.
RefPtr< GossipGroup > gossip_group
Gossip group to communicate with other robots.
Definition: gossip.h:48
long get_nsec() const
Get nanoseconds.
Definition: time.h:113
virtual void init()
Initialize the thread.
const char * name() const
Get name of thread.
Definition: thread.h:95
long get_sec() const
Get seconds.
Definition: time.h:110
virtual ~GossipExampleSenderThread()
Destructor.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void loop()
Code to execute in the thread.
GossipExampleSenderThread()
Constructor.
virtual void finalize()
Finalize the thread.