Fawkes API  Fawkes Development Version
control_thread.cpp
1 
2 /***************************************************************************
3  * control_thread.cpp - Fawkes ECLiPSe Control Thread
4  *
5  * Created: Wed Jul 15 15:09:09 2009
6  * Copyright 2009 Daniel Beck
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.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "control_thread.h"
24 #include "eclipse_thread.h"
25 #include <core/exception.h>
26 #include <interfaces/TestInterface.h>
27 #include <cstring>
28 #include <cstdlib>
29 
30 /** @class AgentControlThread "control_thread.h"
31  * This thread controls the agent thread by sending signals.
32  * @author Daniel Beck
33  */
34 
35 using namespace fawkes;
36 
37 /** Constructor.
38  * @param eclipse_thread the ECLiPSe agent thread
39  */
41  : Thread( "AgentControlThread", Thread::OPMODE_WAITFORWAKEUP ),
42  BlockedTimingAspect( BlockedTimingAspect::WAKEUP_HOOK_THINK ),
43  m_eclipse_thread( eclipse_thread )
44 {
45 }
46 
47 /** Destructor. */
49 {
50 }
51 
52 void
54 {
55  // open & register interfaces
56  m_test_iface = blackboard->open_for_writing< TestInterface >("eclipse_clp_test");
57  m_debug_iface = blackboard->open_for_reading< EclipseDebuggerInterface >("eclipse_clp_connect");
58 }
59 
60 bool
62 {
63  m_eclipse_thread->post_event( "terminate" );
64 
65  return true;
66 }
67 
68 void
70 {
71  // close interfaces
72  blackboard->close( m_test_iface );
73  blackboard->close( m_debug_iface );
74 }
75 
76 void
78 {
79 
80  //if the debug interface has a writer (so tktools module is loaded),
81  //then post event to check for tktool connection request within eclipse
82  if (m_debug_iface->has_writer()){
83  m_eclipse_thread->post_event( "check_debug_msg" );
84  }
85 
86  // this is only used by the dummy agent
87  while ( !m_test_iface->msgq_empty() )
88  {
89  if ( m_test_iface->msgq_first_is< TestInterface::CalculateMessage >() )
90  {
92 
93  m_test_iface->set_result( msg->summand() + msg->addend() );
94  }
95 
96  m_test_iface->msgq_pop();
97  }
98  m_test_iface->write();
99 
100 
101  //m_eclipse_thread->post_event( "update" );
102 
103  /* call eclipse thread (that thread has no blocked timing aspect
104  * and is therefore not called by the mainapp, so this has to be
105  * done here)
106  */
107  //m_eclipse_thread->loop();
108 }
virtual void loop()
Code to execute in the thread.
bool msgq_empty()
Check if queue is empty.
Definition: interface.cpp:1048
Fawkes library namespace.
virtual ~AgentControlThread()
Destructor.
AgentControlThread(EclipseAgentThread *eclipse_thread)
Constructor.
virtual bool prepare_finalize_user()
Prepare finalization user implementation.
Thread class encapsulation of pthreads.
Definition: thread.h:42
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:500
int32_t addend() const
Get addend value.
virtual void finalize()
Finalize the thread.
void post_event(const char *)
Post an event to the ECLiPSe context.
Thread aspect to use blocked timing.
void msgq_pop()
Erase first message from queue.
Definition: interface.cpp:1193
Message * msgq_first()
Get the first message from the message queue.
Definition: interface.cpp:1180
EclipseDebuggerInterface Fawkes BlackBoard Interface.
bool has_writer() const
Check if there is a writer for the interface.
Definition: interface.cpp:834
CalculateMessage Fawkes BlackBoard Interface Message.
bool msgq_first_is()
Check if first message has desired type.
Definition: interface.h:314
virtual void init()
Initialize the thread.
This thread creates an ECLiPSe context in which the ECLiPSe interpreter and the program are loaded...
int32_t summand() const
Get summand value.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
void set_result(const int32_t new_result)
Set result value.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:33
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
virtual void close(Interface *interface)=0
Close interface.