22 #include <plugins/openprs/mod_utils.h> 24 #include <blackboard/remote.h> 25 #include <interfaces/SkillerInterface.h> 26 #include <utils/misc/string_conversions.h> 30 extern "C" void finalize();
36 std::string g_skill_string;
37 unsigned int g_skill_msgid = 0;
38 Thread_Intention_Block *g_skill_tib = NULL;
41 gen_skill_string(TermList terms)
43 int terms_len = sl_slist_length(terms);
44 Term *name = (Term *)get_list_pos(terms, 1);
45 std::string skill_string = std::string(name->u.string) +
"{";
46 for (
int i = 2; i < terms_len; i += 2) {
47 Term *key_t = (Term *)get_list_pos(terms, i);
48 Term *val_t = (Term *)get_list_pos(terms, i+1);
50 if (key_t->type != TT_ATOM && key_t->type != STRING) {
51 fprintf(stderr,
"Error: skill argument key neither of type " 52 "SYMBOL/ATOM nor STRING (%i)\n", key_t->type);
57 if (i > 2) skill_string +=
", ";
59 arg_key = (key_t->type == TT_ATOM) ? key_t->u.id : key_t->u.string;
60 skill_string += std::string(arg_key) +
"=";
62 switch (val_t->type) {
66 case STRING: skill_string += std::string(
"\"") + val_t->u.string +
"\"";
break;
67 case TT_ATOM: skill_string += val_t->u.id;
break;
71 sl_loop_through_slist(global_var_list, env, Envar *) {
72 if (strcmp(env->name, val_t->u.var->name) == 0) {
73 skill_string += env->value->u.string;
75 skill_string +=
"nil";
81 fprintf(stderr,
"Warning: unknown variable type for skill %s argument %s, using nil\n",
82 name->u.string, arg_key);
83 skill_string +=
"nil";
93 assert_exclusive_controller(
unsigned int num_tries,
unsigned int delay_msec)
98 for (
unsigned int i = 0; i < num_tries; ++i) {
106 usleep(delay_msec * 1000);
117 action_skill_call(TermList terms)
119 int terms_len = sl_slist_length(terms);
120 if (terms_len == 0) {
121 fprintf(stderr,
"Error: no arguments to skill call\n");
126 ACTION_SET_AND_ASSERT_ARG_TYPE(
"skill-call", name, terms, 1, STRING);
128 if (terms_len % 2 == 0) {
129 fprintf(stderr,
"Error: invalid number of arguments (%i) to skill call for %s\n",
130 terms_len, name->u.string);
137 if (action_first_call()) {
140 fprintf(stderr,
"Cannot send skill, interface has no writer\n");
143 if (! assert_exclusive_controller(20, 100)) {
144 fprintf(stderr,
"Cannot send skill, not exclusive controller\n");
148 std::string skill_string = gen_skill_string(terms);
149 if (skill_string.empty()) {
150 fprintf(stderr,
"Error: failed to generate skill string\n");
154 printf(
"Calling skill %s\n", skill_string.c_str());
162 g_skill_msgid = msg->id();
163 g_skill_string = skill_string;
167 g_skill_tib = current_tib;
171 if (current_tib != g_skill_tib) {
172 fprintf(stderr,
"Skill preempted by another skill, returning fail");
178 if (skiller_if->
msgid() > g_skill_msgid) {
179 fprintf(stderr,
"Fail: a more recent message is being processed by the skiller (%u > %u)\n",
180 skiller_if->
msgid(), g_skill_msgid);
182 }
else if (skiller_if->
msgid() < g_skill_msgid) {
187 switch (skiller_if->
status()) {
189 printf(
"Skill %s is FINAL\n", name->u.string);
194 printf(
"Skill %s has FAILED\n", name->u.string);
197 default: ACTION_WAIT();
209 printf(
"*** LOADING mod_skiller\n");
211 std::string fawkes_host;
212 unsigned short fawkes_port = 0;
213 get_fawkes_host_port(fawkes_host, fawkes_port);
215 printf(
"Connecting to Fawkes at %s:%u\n", fawkes_host.c_str(), fawkes_port);
219 fprintf(stderr,
"Error: cannot establish blackboard connection: %s\n",
225 printf(
"Acquiring exclusive skiller control\n");
228 skiller_if->msgq_enqueue(msg);
230 declare_atom(
"true");
231 declare_atom(
"false");
232 make_and_declare_action(
"skill-call", action_skill_call, -1);
233 add_user_end_kernel_hook(finalize);
240 printf(
"*** DESTROYING mod_skiller\n");
241 if (skiller_if->has_writer()) {
244 skiller_if->msgq_enqueue(msg);
248 blackboard->
close(skiller_if);
The skill string has been successfully processed.
Fawkes library namespace.
ReleaseControlMessage Fawkes BlackBoard Interface Message.
uint32_t exclusive_controller() const
Get exclusive_controller value.
AcquireControlMessage Fawkes BlackBoard Interface Message.
Base class for exceptions in Fawkes.
unsigned short serial() const
Get instance serial of interface.
void read()
Read from BlackBoard into local copy.
void ref()
Increment reference count.
bool has_writer() const
Check if there is a writer for the interface.
SkillStatusEnum status() const
Get status value.
ExecSkillMessage Fawkes BlackBoard Interface Message.
virtual const char * what_no_backtrace() const
Get primary string (does not implicitly print the back trace).
unsigned int msgq_enqueue(Message *message)
Enqueue message at end of queue.
The execution failed and cannot succeed anymore.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
uint32_t msgid() const
Get msgid value.
SkillerInterface Fawkes BlackBoard Interface.
The BlackBoard abstract class.
static std::string to_string(unsigned int i)
Convert unsigned int value to a string.
virtual void close(Interface *interface)=0
Close interface.