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 
41 using namespace fawkes;
42 
43 void
44 print_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 
52 static int
53 event_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 {
66 public:
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 
143  new TestInterface::SetTestStringMessage(sks.c_str());
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) throw()
181  {
182  }
183 
184  virtual void
185  inbound_received(FawkesNetworkMessage *m, unsigned int id) throw()
186  {
187  }
188 
189  virtual void
190  connection_died(unsigned int id) throw()
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) throw()
208  {
209  printf("Connection established\n");
210  just_connected = true;
211  prompt = "+# ";
212  }
213 
214 private:
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  */
228 int
229 main(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 }
Message handler for FawkesNetworkClient.
SkillShellThread(ArgumentParser *argp)
Constructor.
Definition: skilltester.cpp:70
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:224
Simple Fawkes network client.
Definition: client.h:51
Skill shell thread.
Definition: skilltester.cpp:64
Fawkes library namespace.
Representation of a message that is sent over the network.
Definition: message.h:76
Parse command line arguments.
Definition: argparser.h:63
Thread class encapsulation of pthreads.
Definition: thread.h:45
Base class for exceptions in Fawkes.
Definition: exception.h:35
virtual void connection_established(unsigned int id)
Client has established a connection.
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:601
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.
The BlackBoard abstract class.
Definition: blackboard.h:45
~SkillShellThread()
Destructor.
Definition: skilltester.cpp:97
SetTestStringMessage Fawkes BlackBoard Interface Message.
Definition: TestInterface.h:94
virtual void inbound_received(FawkesNetworkMessage *m, unsigned int id)
Called for incoming messages.
TestInterface Fawkes BlackBoard Interface.
Definition: TestInterface.h:33