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