Fawkes API Fawkes Development Version
time_source.cpp
1
2/***************************************************************************
3 * timesource.cpp - Fawkes TimeSourceAspect initializer/finalizer
4 *
5 * Created: Wed Nov 24 00:39:37 2010
6 * Copyright 2006-2010 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 <aspect/inifins/time_source.h>
25#include <aspect/time_source.h>
26#include <core/threading/thread_finalizer.h>
27#include <utils/time/clock.h>
28
29namespace fawkes {
30
31/** @class TimeSourceAspectIniFin <aspect/inifins/time_source.h>
32 * Initializer/finalizer for the TimeSourceAspect.
33 * @author Tim Niemueller
34 */
35
36/** Constructor.
37 * @param clock clock to register time source to
38 */
40{
41 clock_ = clock;
42}
43
44void
46{
47 TimeSourceAspect *timesource_thread;
48 timesource_thread = dynamic_cast<TimeSourceAspect *>(thread);
49 if (timesource_thread == NULL) {
50 throw CannotInitializeThreadException("Thread '%s' claims to have the "
51 "TimeSourceAspect, but RTTI says it "
52 "has not. ",
53 thread->name());
54 }
55
56 try {
57 timesource_uc_.add(timesource_thread->get_timesource());
58 clock_->register_ext_timesource(timesource_thread->get_timesource(),
59 /* make default */ true);
60 } catch (Exception &e) {
61 throw CannotInitializeThreadException("Thread has TimeSourceAspect but there "
62 "is already another time provider.");
63 }
64}
65
66void
68{
69 TimeSourceAspect *timesource_thread;
70 timesource_thread = dynamic_cast<TimeSourceAspect *>(thread);
71 if (timesource_thread == NULL) {
72 throw CannotInitializeThreadException("Thread '%s' claims to have the "
73 "TimeSourceAspect, but RTTI says it "
74 "has not. ",
75 thread->name());
76 }
77
78 try {
79 clock_->remove_ext_timesource(timesource_thread->get_timesource());
80 timesource_uc_.remove(timesource_thread->get_timesource());
81 } catch (Exception &e) {
82 CannotFinalizeThreadException ce("Failed to remove time source");
83 ce.append(e);
84 throw;
85 }
86}
87
88} // end namespace fawkes
Aspect initializer/finalizer base class.
Definition: inifin.h:34
Thread cannot be finalized.
This is supposed to be the central clock in Fawkes.
Definition: clock.h:35
void remove_ext_timesource(TimeSource *ts=0)
Remove external time source.
Definition: clock.cpp:103
void register_ext_timesource(TimeSource *ts, bool make_default=false)
Register an external time source.
Definition: clock.cpp:88
Base class for exceptions in Fawkes.
Definition: exception.h:36
void append(const char *format,...) noexcept
Append messages to the message list.
Definition: exception.cpp:333
Thread class encapsulation of pthreads.
Definition: thread.h:46
const char * name() const
Get name of thread.
Definition: thread.h:100
TimeSourceAspectIniFin(Clock *clock)
Constructor.
Definition: time_source.cpp:39
virtual void finalize(Thread *thread)
Finalize thread.
Definition: time_source.cpp:67
virtual void init(Thread *thread)
Initialize thread.
Definition: time_source.cpp:45
Thread aspect that allows to provide a time source to the Fawkes clock.
Definition: time_source.h:34
TimeSource * get_timesource() const
Get time source.
Definition: time_source.cpp:61
Fawkes library namespace.