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 <netcomm/fawkes/client.h>
25 #include <blackboard/remote.h>
26 #include <utils/system/argparser.h>
27 #include <utils/system/signal.h>
28 #include <core/threading/thread.h>
29 #include <netcomm/fawkes/client_handler.h>
30 
31 #include <cstring>
32 #include <cstdlib>
33 #include <cstdio>
34 #include <unistd.h>
35 #include <csignal>
36 #include <string>
37 
38 #include <readline/readline.h>
39 #include <readline/history.h>
40 
41 #include <interfaces/TestInterface.h>
42 
43 using namespace fawkes;
44 
45 void
46 print_usage(const char *program_name)
47 {
48  printf("Usage: %s [-h] [-r host[:port]]\n"
49  " -h This help message\n"
50  " -r host[:port] Remote host (and optionally port) to connect to\n",
51  program_name);
52 }
53 
54 static int
55 event_hook()
56 {
57  return 0;
58 }
59 
60 
61 /** Skill shell thread.
62  * This thread opens a network connection to a host and uses a RemoteBlackBoard
63  * connection to send skill strings for execution. It also shows Skiller log messages
64  * and uses the skiller network protocol.
65  * @author Tim Niemueller
66  */
68 {
69  public:
70  /** Constructor.
71  * @param argp argument parser
72  */
74  : Thread("SkillShellThread", Thread::OPMODE_CONTINUOUS)
75  {
76  this->argp = argp;
77  prompt = "-# ";
78  just_connected = true;
79  connection_died_recently = false;
80 
81  testif = NULL;
82  using_history();
83  // this is needed to get rl_done working
84  rl_event_hook = event_hook;
85 
86  char *host = (char *)"localhost";
87  unsigned short int port = 1910;
88  bool free_host = argp->parse_hostport("r", &host, &port);
89 
90  c = new FawkesNetworkClient(host, port);
91 
92  if ( free_host ) free(host);
93 
94  c->register_handler(this, FAWKES_CID_SKILLER_PLUGIN);
95  c->connect();
96  }
97 
98  /** Destructor. */
100  {
101  printf("Finalizing\n");
102 
103  //SkillerInterface::ReleaseControlMessage *rcm = new SkillerInterface::ReleaseControlMessage();
104  //sif->msgq_enqueue(rcm);
105 
106  usleep(500000);
107 
108  rbb->close(testif);
109  delete rbb;
110  rbb = NULL;
111 
112  c->deregister_handler(FAWKES_CID_SKILLER_PLUGIN);
113  c->disconnect();
114  delete c;
115  }
116 
117 
118  virtual void loop()
119  {
120  if ( c->connected() ) {
121  if ( just_connected ) {
122  just_connected = false;
123  try {
124  rbb = new RemoteBlackBoard(c);
125  testif = rbb->open_for_reading<TestInterface>("eclipse_clp_skillexec");
126  usleep(100000);
127  } catch (Exception &e) {
128  e.print_trace();
129  return;
130  }
131  }
132 
133  if ( argp->num_items() > 0 ) {
134  std::string sks = "";
135  const std::vector< const char * > & items = argp->items();
136 
137  std::vector< const char * >::const_iterator i = items.begin();
138  sks = *i;
139  ++i;
140  for (; i != items.end(); ++i) {
141  sks += " ";
142  sks += *i;
143  }
144 
146  testif->msgq_enqueue(tsm);
147 
148  usleep(100000);
149  exit();
150  } else {
151  char *line = NULL;
152 
153  line = readline(prompt);
154  if ( line ) {
155  if (strcmp(line, "") != 0) {
157  testif->msgq_enqueue(tsm);
158  add_history(line);
159  }
160  } else {
161  if ( ! connection_died_recently ) {
162  exit();
163  }
164  }
165  }
166  } else {
167  if ( connection_died_recently ) {
168  connection_died_recently = false;
169  printf("Connection died\n");
170  c->disconnect();
171  }
172  try {
173  c->connect();
174  } catch (Exception &e) {
175  printf(".");
176  fflush(stdout);
177  sleep(1);
178  }
179  }
180  }
181 
182 
183  virtual void deregistered(unsigned int id) throw()
184  {
185  }
186 
187 
189  unsigned int id) throw()
190  {
191  }
192 
193 
194  virtual void connection_died(unsigned int id) throw()
195  {
196  prompt = "-# ";
197 
198  rbb->close(testif);
199  delete rbb;
200  rbb = NULL;
201  testif = NULL;
202 
203  connection_died_recently = true;
204 
205  //fprintf(stdin, "\n");
206  //kill(SIGINT);
207  rl_done = 1;
208  }
209 
210 
211  virtual void connection_established(unsigned int id) throw()
212  {
213  printf("Connection established\n");
214  just_connected = true;
215  prompt = "+# ";
216  }
217 
218 
219  private:
220  ArgumentParser *argp;
222  BlackBoard *rbb;
223  TestInterface *testif;
224  const char *prompt;
225  bool just_connected;
226  bool connection_died_recently;
227 };
228 
229 
230 /** Config tool main.
231  * @param argc argument count
232  * @param argv arguments
233  */
234 int
235 main(int argc, char **argv)
236 {
237  ArgumentParser argp(argc, argv, "hr:");
238 
239  if ( argp.has_arg("h") ) {
240  print_usage(argv[0]);
241  exit(0);
242  }
243 
244  SkillShellThread sst(&argp);
245  sst.start();
246  sst.join();
247 
248  return 0;
249 }
Message handler for FawkesNetworkClient.
SkillShellThread(ArgumentParser *argp)
Constructor.
Definition: skilltester.cpp:73
virtual void deregistered(unsigned int id)
This handler has been deregistered.
bool parse_hostport(const char *argn, char **host, unsigned short int *port)
Parse host:port string.
Definition: argparser.cpp:231
Simple Fawkes network client.
Definition: client.h:52
Skill shell thread.
Definition: skilltester.cpp:67
Fawkes library namespace.
Representation of a message that is sent over the network.
Definition: message.h:75
Parse command line arguments.
Definition: argparser.h:66
Thread class encapsulation of pthreads.
Definition: thread.h:42
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void connection_established(unsigned int id)
Client has established a connection.
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
virtual void loop()
Code to execute in the thread.
Remote BlackBoard.
Definition: remote.h:48
virtual void connection_died(unsigned int id)
Client connection died.
void join()
Join the thread.
Definition: thread.cpp:610
bool has_arg(const char *argn)
Check if argument has been supplied.
Definition: argparser.cpp:169
The BlackBoard abstract class.
Definition: blackboard.h:48
~SkillShellThread()
Destructor.
Definition: skilltester.cpp:99
SetTestStringMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:94
virtual void inbound_received(FawkesNetworkMessage *m, unsigned int id)
Called for incoming messages.
void start(bool wait=true)
Call this method to start the thread.
Definition: thread.cpp:511
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:33