Fawkes API  Fawkes Development Version
time_source.cpp
00001 
00002 /***************************************************************************
00003  *  timesource.cpp - Fawkes TimeSourceAspect initializer/finalizer
00004  *
00005  *  Created: Wed Nov 24 00:39:37 2010
00006  *  Copyright  2006-2010  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <aspect/inifins/time_source.h>
00025 #include <aspect/time_source.h>
00026 #include <utils/time/clock.h>
00027 #include <core/threading/thread_finalizer.h>
00028 
00029 namespace fawkes {
00030 #if 0 /* just to make Emacs auto-indent happy */
00031 }
00032 #endif
00033 
00034 /** @class TimeSourceAspectIniFin <aspect/inifins/time_source.h>
00035  * Initializer/finalizer for the TimeSourceAspect.
00036  * @author Tim Niemueller
00037  */
00038 
00039 /** Constructor.
00040  * @param clock clock to register time source to
00041  */
00042 TimeSourceAspectIniFin::TimeSourceAspectIniFin(Clock *clock)
00043   : AspectIniFin("TimeSourceAspect")
00044 {
00045   __clock = clock;
00046 }
00047 
00048 
00049 void
00050 TimeSourceAspectIniFin::init(Thread *thread)
00051 {
00052   TimeSourceAspect *timesource_thread;
00053   timesource_thread = dynamic_cast<TimeSourceAspect *>(thread);
00054   if (timesource_thread == NULL) {
00055     throw CannotInitializeThreadException("Thread '%s' claims to have the "
00056                                           "TimeSourceAspect, but RTTI says it "
00057                                           "has not. ", thread->name());
00058   }
00059 
00060   try {
00061     __timesource_uc.add(timesource_thread->get_timesource());
00062     __clock->register_ext_timesource(timesource_thread->get_timesource(),
00063                                      /* make default */ true);
00064   } catch (Exception &e) {
00065     throw CannotInitializeThreadException("Thread has TimeSourceAspect but there "
00066                                           "is already another time provider.");
00067   }
00068 }
00069 
00070 
00071 void
00072 TimeSourceAspectIniFin::finalize(Thread *thread)
00073 {
00074   TimeSourceAspect *timesource_thread;
00075   timesource_thread = dynamic_cast<TimeSourceAspect *>(thread);
00076   if (timesource_thread == NULL) {
00077     throw CannotInitializeThreadException("Thread '%s' claims to have the "
00078                                           "TimeSourceAspect, but RTTI says it "
00079                                           "has not. ", thread->name());
00080   }
00081 
00082   try {
00083     __clock->remove_ext_timesource(timesource_thread->get_timesource());
00084     __timesource_uc.remove(timesource_thread->get_timesource());
00085   } catch (Exception &e) {
00086     CannotFinalizeThreadException ce("Failed to remove time source");
00087     ce.append(e);
00088     throw;
00089   }
00090 }
00091 
00092 
00093 } // end namespace fawkes