Fawkes API Fawkes Development Version
skilltester.cpp
1
2/***************************************************************************
3 * skilltester.cpp - Skilltester for eclipse-clp - console tool
4 *
5 * Created: Tue Apr 09 12:36:39 2013
6 * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7 2013 Gesche Gierse
8 *
9 ****************************************************************************/
10
11/* This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL file in the doc directory.
22 */
23
24#include <blackboard/remote.h>
25#include <core/threading/thread.h>
26#include <interfaces/TestInterface.h>
27#include <netcomm/fawkes/client.h>
28#include <netcomm/fawkes/client_handler.h>
29#include <readline/history.h>
30#include <readline/readline.h>
31#include <utils/system/argparser.h>
32#include <utils/system/signal.h>
33
34#include <csignal>
35#include <cstdio>
36#include <cstdlib>
37#include <cstring>
38#include <string>
39#include <unistd.h>
40
41using namespace fawkes;
42
43void
44print_usage(const char *program_name)
45{
46 printf("Usage: %s [-h] [-r host[:port]]\n"
47 " -h This help message\n"
48 " -r host[:port] Remote host (and optionally port) to connect to\n",
49 program_name);
50}
51
52static int
53event_hook()
54{
55 return 0;
56}
57
58/** Skill shell thread.
59 * This thread opens a network connection to a host and uses a RemoteBlackBoard
60 * connection to send skill strings for execution. It also shows Skiller log messages
61 * and uses the skiller network protocol.
62 * @author Tim Niemueller
63 */
65{
66public:
67 /** Constructor.
68 * @param argp argument parser
69 */
71 : Thread("SkillShellThread", Thread::OPMODE_CONTINUOUS)
72 {
73 this->argp = argp;
74 prompt = "-# ";
75 just_connected = true;
76 connection_died_recently = false;
77
78 testif = NULL;
79 using_history();
80 // this is needed to get rl_done working
81 rl_event_hook = event_hook;
82
83 char * host = (char *)"localhost";
84 unsigned short int port = 1910;
85 bool free_host = argp->parse_hostport("r", &host, &port);
86
87 c = new FawkesNetworkClient(host, port);
88
89 if (free_host)
90 free(host);
91
92 c->register_handler(this, FAWKES_CID_SKILLER_PLUGIN);
93 c->connect();
94 }
95
96 /** Destructor. */
98 {
99 printf("Finalizing\n");
100
101 //SkillerInterface::ReleaseControlMessage *rcm = new SkillerInterface::ReleaseControlMessage();
102 //sif->msgq_enqueue(rcm);
103
104 usleep(500000);
105
106 rbb->close(testif);
107 delete rbb;
108 rbb = NULL;
109
110 c->deregister_handler(FAWKES_CID_SKILLER_PLUGIN);
111 c->disconnect();
112 delete c;
113 }
114
115 virtual void
117 {
118 if (c->connected()) {
119 if (just_connected) {
120 just_connected = false;
121 try {
122 rbb = new RemoteBlackBoard(c);
123 testif = rbb->open_for_reading<TestInterface>("eclipse_clp_skillexec");
124 usleep(100000);
125 } catch (Exception &e) {
126 e.print_trace();
127 return;
128 }
129 }
130
131 if (argp->num_items() > 0) {
132 const std::vector<const char *> &items = argp->items();
133
134 std::vector<const char *>::const_iterator i = items.begin();
135 std::string sks = *i;
136 ++i;
137 for (; i != items.end(); ++i) {
138 sks += " ";
139 sks += *i;
140 }
141
144 testif->msgq_enqueue(tsm);
145
146 usleep(100000);
147 exit();
148 } else {
149 char *line = readline(prompt);
150 if (line) {
151 if (strcmp(line, "") != 0) {
154 testif->msgq_enqueue(tsm);
155 add_history(line);
156 }
157 } else {
158 if (!connection_died_recently) {
159 exit();
160 }
161 }
162 }
163 } else {
164 if (connection_died_recently) {
165 connection_died_recently = false;
166 printf("Connection died\n");
167 c->disconnect();
168 }
169 try {
170 c->connect();
171 } catch (Exception &e) {
172 printf(".");
173 fflush(stdout);
174 sleep(1);
175 }
176 }
177 }
178
179 virtual void
180 deregistered(unsigned int id) noexcept
181 {
182 }
183
184 virtual void
185 inbound_received(FawkesNetworkMessage *m, unsigned int id) noexcept
186 {
187 }
188
189 virtual void
190 connection_died(unsigned int id) noexcept
191 {
192 prompt = "-# ";
193
194 rbb->close(testif);
195 delete rbb;
196 rbb = NULL;
197 testif = NULL;
198
199 connection_died_recently = true;
200
201 //fprintf(stdin, "\n");
202 //kill(SIGINT);
203 rl_done = 1;
204 }
205
206 virtual void
207 connection_established(unsigned int id) noexcept
208 {
209 printf("Connection established\n");
210 just_connected = true;
211 prompt = "+# ";
212 }
213
214private:
215 ArgumentParser * argp;
217 BlackBoard * rbb;
218 TestInterface * testif;
219 const char * prompt;
220 bool just_connected;
221 bool connection_died_recently;
222};
223
224/** Config tool main.
225 * @param argc argument count
226 * @param argv arguments
227 */
228int
229main(int argc, char **argv)
230{
231 ArgumentParser argp(argc, argv, "hr:");
232
233 if (argp.has_arg("h")) {
234 print_usage(argv[0]);
235 exit(0);
236 }
237
238 SkillShellThread sst(&argp);
239 sst.start();
240 sst.join();
241
242 return 0;
243}
Skill shell thread.
Definition: skilltester.cpp:65
virtual void deregistered(unsigned int id) noexcept
This handler has been deregistered.
SkillShellThread(ArgumentParser *argp)
Constructor.
Definition: skilltester.cpp:70
virtual void loop()
Code to execute in the thread.
~SkillShellThread()
Destructor.
Definition: skilltester.cpp:97
virtual void inbound_received(FawkesNetworkMessage *m, unsigned int id) noexcept
Called for incoming messages.
virtual void connection_died(unsigned int id) noexcept
Client connection died.
virtual void connection_established(unsigned int id) noexcept
Client has established a connection.
Parse command line arguments.
Definition: argparser.h:64
const std::vector< const char * > & items() const
Get non-option items.
Definition: argparser.cpp:447
bool parse_hostport(const char *argn, char **host, unsigned short int *port)
Parse host:port string.
Definition: argparser.cpp:224
std::vector< constchar * >::size_type num_items() const
Get number of non-option items.
Definition: argparser.cpp:456
The BlackBoard abstract class.
Definition: blackboard.h:46
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
virtual void close(Interface *interface)=0
Close interface.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void print_trace() noexcept
Prints trace to stderr.
Definition: exception.cpp:601
Message handler for FawkesNetworkClient.
Simple Fawkes network client.
Definition: client.h:52
void register_handler(FawkesNetworkClientHandler *handler, unsigned int component_id)
Register handler.
Definition: client.cpp:658
void connect()
Connect to remote.
Definition: client.cpp:424
void disconnect()
Disconnect socket.
Definition: client.cpp:539
void deregister_handler(unsigned int component_id)
Deregister handler.
Definition: client.cpp:676
bool connected() const noexcept
Check if connection is alive.
Definition: client.cpp:828
Representation of a message that is sent over the network.
Definition: message.h:77
unsigned int msgq_enqueue(Message *message, bool proxy=false)
Enqueue message at end of queue.
Definition: interface.cpp:915
Remote BlackBoard.
Definition: remote.h:50
SetTestStringMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:95
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:34
Thread class encapsulation of pthreads.
Definition: thread.h:46
void exit()
Exit the thread.
Definition: thread.cpp:582
@ OPMODE_CONTINUOUS
operate in continuous mode (default)
Definition: thread.h:57
Fawkes library namespace.