Fawkes API  Fawkes Development Version
control_thread.cpp
1 
2 /***************************************************************************
3  * control_thread.cpp - Fawkes Readylog Agent 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 >("readylog_test");
57 }
58 
59 bool
61 {
62  m_eclipse_thread->post_event( "terminate" );
63 
64  return true;
65 }
66 
67 void
69 {
70  // close interfaces
71  blackboard->close( m_test_iface );
72 }
73 
74 void
76 {
77  m_test_iface->read();
78 
79  while ( !m_test_iface->msgq_empty() )
80  {
81  if ( m_test_iface->msgq_first_is< TestInterface::CalculateMessage >() )
82  {
84 
85  m_test_iface->set_result( msg->summand() + msg->addend() );
86  }
87 
88  m_test_iface->msgq_pop();
89  }
90 
91  m_test_iface->write();
92 
93  m_eclipse_thread->read_interfaces();
94  m_eclipse_thread->post_event( "update" );
95  m_eclipse_thread->write_interfaces();
96 }
virtual void loop()
Code to execute in the thread.
bool msgq_empty()
Check if queue is empty.
Definition: interface.cpp:972
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:495
virtual Interface * open_for_writing(const char *interface_type, const char *identifier)=0
Open interface for writing.
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:1118
Message * msgq_first()
Get the first message from the message queue.
Definition: interface.cpp:1104
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:472
void read_interfaces()
Read all registered interfaces.
CalculateMessage Fawkes BlackBoard Interface Message.
bool msgq_first_is()
Check if first message has desired type.
Definition: interface.h:305
virtual void init()
Initialize the thread.
This thread creates an ECLiPSe context in which the Readylog interpreter and the program are loaded...
int32_t addend() const
Get addend value.
int32_t summand() const
Get summand value.
void write_interfaces()
Write the registered interface that have been opened for writing.
void set_result(const int32_t new_result)
Set result value.
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:43
virtual void close(Interface *interface)=0
Close interface.