00001 00002 /*************************************************************************** 00003 * control_thread.cpp - Fawkes Readylog Agent Thread 00004 * 00005 * Created: Wed Jul 15 15:09:09 2009 00006 * Copyright 2009 Daniel Beck 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. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include "control_thread.h" 00024 #include "eclipse_thread.h" 00025 #include <core/exception.h> 00026 #include <interfaces/TestInterface.h> 00027 #include <cstring> 00028 #include <cstdlib> 00029 00030 /** @class AgentControlThread "control_thread.h" 00031 * This thread controls the agent thread by sending signals. 00032 * @author Daniel Beck 00033 */ 00034 00035 using namespace fawkes; 00036 00037 /** Constructor. 00038 * @param eclipse_thread the ECLiPSe agent thread 00039 */ 00040 AgentControlThread::AgentControlThread( EclipseAgentThread* eclipse_thread ) 00041 : Thread( "AgentControlThread", Thread::OPMODE_WAITFORWAKEUP ), 00042 BlockedTimingAspect( BlockedTimingAspect::WAKEUP_HOOK_THINK ), 00043 m_eclipse_thread( eclipse_thread ) 00044 { 00045 } 00046 00047 /** Destructor. */ 00048 AgentControlThread::~AgentControlThread() 00049 { 00050 } 00051 00052 void 00053 AgentControlThread::init() 00054 { 00055 // open & register interfaces 00056 m_test_iface = blackboard->open_for_writing< TestInterface >("readylog_test"); 00057 } 00058 00059 bool 00060 AgentControlThread::prepare_finalize_user() 00061 { 00062 m_eclipse_thread->post_event( "terminate" ); 00063 00064 return true; 00065 } 00066 00067 void 00068 AgentControlThread::finalize() 00069 { 00070 // close interfaces 00071 blackboard->close( m_test_iface ); 00072 } 00073 00074 void 00075 AgentControlThread::loop() 00076 { 00077 m_test_iface->read(); 00078 00079 while ( !m_test_iface->msgq_empty() ) 00080 { 00081 if ( m_test_iface->msgq_first_is< TestInterface::CalculateMessage >() ) 00082 { 00083 TestInterface::CalculateMessage* msg = m_test_iface->msgq_first< TestInterface::CalculateMessage >(); 00084 00085 m_test_iface->set_result( msg->summand() + msg->addend() ); 00086 } 00087 00088 m_test_iface->msgq_pop(); 00089 } 00090 00091 m_test_iface->write(); 00092 00093 m_eclipse_thread->read_interfaces(); 00094 m_eclipse_thread->post_event( "update" ); 00095 m_eclipse_thread->write_interfaces(); 00096 }