Fawkes API  Fawkes Development Version
mainloop.cpp
00001 
00002 /***************************************************************************
00003  *  mainloop.cpp - Fawkes MainLoopAspect initializer/finalizer
00004  *
00005  *  Created: Wed Nov 24 00:44:55 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/mainloop.h>
00025 #include <aspect/mainloop.h>
00026 #include <aspect/mainloop/employer.h>
00027 #include <aspect/blocked_timing/executor.h>
00028 #include <core/threading/thread_finalizer.h>
00029 
00030 namespace fawkes {
00031 #if 0 /* just to make Emacs auto-indent happy */
00032 }
00033 #endif
00034 
00035 /** @class MainLoopAspectIniFin <aspect/inifins/mainloop.h>
00036  * Initializer/finalizer for the MainLoopAspect.
00037  * @author Tim Niemueller
00038  */
00039 
00040 /** Constructor.
00041  * @param employer main loop employer to register main loop to
00042  * @param btexec blocked timing executor to pass to thread
00043  */
00044 MainLoopAspectIniFin::MainLoopAspectIniFin(MainLoopEmployer *employer,
00045                                            BlockedTimingExecutor *btexec)
00046   : AspectIniFin("MainLoopAspect")
00047 {
00048   __employer = employer;
00049   __btexec   = btexec;
00050 }
00051 
00052 
00053 void
00054 MainLoopAspectIniFin::init(Thread *thread)
00055 {
00056   MainLoopAspect *mainloop_thread;
00057   mainloop_thread = dynamic_cast<MainLoopAspect *>(thread);
00058   if (mainloop_thread == NULL) {
00059     throw CannotInitializeThreadException("Thread '%s' claims to have the "
00060                                           "MainLoopAspect, but RTTI says it "
00061                                           "has not. ", thread->name());
00062   }
00063 
00064   if (thread->opmode() != Thread::OPMODE_WAITFORWAKEUP) {
00065     throw CannotInitializeThreadException("MainLoopAspect thread must operate "
00066                                           "in wait-for-wakeup mode.");  
00067   }
00068 
00069   try {
00070     __mainloop_uc.add(mainloop_thread);
00071     mainloop_thread->init_MainLoopAspect(__btexec);
00072     thread->add_notification_listener(this);
00073   } catch (Exception &e) {
00074     CannotInitializeThreadException ce("Main loop thread failed to initialize");
00075     ce.append(e);
00076     throw ce;
00077   }
00078 }
00079 
00080 
00081 void
00082 MainLoopAspectIniFin::finalize(Thread *thread)
00083 {
00084   MainLoopAspect *mainloop_thread;
00085   mainloop_thread = dynamic_cast<MainLoopAspect *>(thread);
00086   if (mainloop_thread == NULL) {
00087     throw CannotInitializeThreadException("Thread '%s' claims to have the "
00088                                           "MainLoopAspect, but RTTI says it "
00089                                           "has not. ", thread->name());
00090   }
00091 
00092   try {
00093     __employer->set_mainloop_thread(NULL);
00094     __mainloop_uc.remove(mainloop_thread);
00095   } catch (Exception &e) {
00096     CannotFinalizeThreadException ce("Failed to remove time source");
00097     ce.append(e);
00098     throw;
00099   }
00100 }
00101 
00102 
00103 bool
00104 MainLoopAspectIniFin::thread_started(Thread *thread) throw()
00105 {
00106   MainLoopAspect *mainloop_thread;
00107   if ( (mainloop_thread = dynamic_cast<MainLoopAspect *>(thread)) != NULL ) {
00108     try {
00109       __employer->set_mainloop_thread(thread);
00110     } catch (Exception &e) {
00111       //__logger->log_error("AspectIniFin", "Main loop thread started successfully "
00112       //                  "but could not add main loop thread's main loop");
00113     }
00114   }
00115 
00116   return false;
00117 }
00118 
00119 
00120 bool
00121 MainLoopAspectIniFin::thread_init_failed(Thread *thread) throw()
00122 {
00123   MainLoopAspect *mainloop_thread;
00124   if ( (mainloop_thread = dynamic_cast<MainLoopAspect *>(thread)) != NULL ) {
00125     try {
00126       __mainloop_uc.remove(mainloop_thread);
00127     } catch (Exception &e) {
00128       //__logger->log_error("AspectIniFin", "Failed to remove main loop from "
00129       //                  "uniqueness constraint on thread init fail of %s",
00130       //                  thread->name());
00131     }
00132   }
00133 
00134   try {
00135     finalize(thread);
00136   } catch (Exception &e) {
00137     /*
00138     __logger->log_error("AspectIniFin", "Initialization of thread '%s' failed, but "
00139                         "the thread thread could not be internally finalized",
00140                         thread->name());
00141     __logger->log_error("AspectIniFin", e);
00142     */
00143   }
00144 
00145   return false;
00146 }
00147 
00148 
00149 } // end namespace fawkes