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 
29 namespace 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  */
39 TimeSourceAspectIniFin::TimeSourceAspectIniFin(Clock *clock) : AspectIniFin("TimeSourceAspect")
40 {
41  clock_ = clock;
42 }
43 
44 void
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 
66 void
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
fawkes::Clock::remove_ext_timesource
void remove_ext_timesource(TimeSource *ts=0)
Remove external time source.
Definition: clock.cpp:108
fawkes::CannotInitializeThreadException
Definition: thread_initializer.h:37
fawkes::Thread::name
const char * name() const
Definition: thread.h:99
fawkes::TimeSourceAspectIniFin::init
virtual void init(Thread *thread)
Definition: time_source.cpp:49
fawkes::TimeSourceAspectIniFin::finalize
virtual void finalize(Thread *thread)
Definition: time_source.cpp:71
fawkes
fawkes::Clock::register_ext_timesource
void register_ext_timesource(TimeSource *ts, bool make_default=false)
Register an external time source.
Definition: clock.cpp:93
fawkes::TimeSourceAspect::get_timesource
TimeSource * get_timesource() const
Get time source.
Definition: time_source.cpp:65
fawkes::TimeSourceAspectIniFin::TimeSourceAspectIniFin
TimeSourceAspectIniFin(Clock *clock)
Constructor.
Definition: time_source.cpp:43
fawkes::Thread
Definition: thread.h:44
fawkes::TimeSourceAspect
Definition: time_source.h:37