Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
blackboard.cpp
1 
2 /***************************************************************************
3  * blackboard.h - External predicates to remotely access the Fawkes
4  * blackboard
5  *
6  * Created: Wed Mar 09 17:10:54 2011
7  * Copyright 2011 Daniel Beck
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.h"
25 
26 #include <blackboard/remote.h>
27 
28 #include <eclipseclass.h>
29 
30 #include <vector>
31 
32 #include <cstdio>
33 #include <cstring>
34 #include <cstdlib>
35 
36 /** @class fawkes::EclExternalBlackBoard
37  * Wrapper class for using the blackboard in the implementation of the external
38  * predicates.
39  * @author Daniel Beck
40  */
41 namespace fawkes
42 {
44 {
45 public:
46  /** Constructor. */
47  EclExternalBlackBoard() : m_blackboard( 0 ) {}
48  /** Destructor. */
50  for ( std::vector< Interface* >::iterator iit = m_interfaces.begin();
51  iit != m_interfaces.end();
52  ++iit )
53  { m_blackboard->close( *iit ); }
54  delete m_blackboard;
55  }
56 
57  /** Open remote blackboard connection.
58  * @param host the host running Fawkes
59  */
60  void connect( const char* host )
61  {
62  m_blackboard = new RemoteBlackBoard( host, 1910 );
63  }
64 
65  /** Query connection status.
66  * @return true if connected; false otherwise
67  */
68  bool connected()
69  {
70  return m_blackboard ? true : false;
71  }
72 
73  /** Disconnect remote blackboard connection. */
74  void disconnect()
75  {
76  for ( std::vector< Interface* >::iterator iit = m_interfaces.begin();
77  iit != m_interfaces.end();
78  ++iit )
79  { m_blackboard->close( *iit ); }
80  delete m_blackboard;
81  m_blackboard = 0;
82  }
83 
84  /** Access the BlackBoard instance.
85  * @return the blackboard instance
86  */
88  {
89  return m_blackboard;
90  }
91 
92  /** Obtain the list of opened interfaces.
93  * @return list of opened interfaces
94  */
95  std::vector< Interface* >& interfaces()
96  {
97  return m_interfaces;
98  }
99 
100 private:
101  BlackBoard* m_blackboard;
102  std::vector< Interface* > m_interfaces;
103 };
104 
105 }
106 
107 using namespace std;
108 using namespace fawkes;
109 
110 EclExternalBlackBoard g_blackboard;
111 
112 bool process_message_args(Message* msg, EC_word arg_list);
113 
114 int
115 p_connect_to_blackboard()
116 {
117  if ( g_blackboard.connected() )
118  {
119  printf( "p_connect_to_blackboard(): already connected\n" );
120  return EC_fail;
121  }
122 
123  // get hostname
124  char* hostname;
125 
126  if ( EC_succeed != EC_arg( 1 ).is_string( &hostname ) )
127  {
128  printf( "p_connect_to_blackboard(): first argument is not a string\n" );
129  return EC_fail;
130  }
131 
132  try
133  {
134  g_blackboard.connect( hostname );
135  }
136  catch ( Exception& e )
137  {
138  e.print_trace();
139  return EC_fail;
140  }
141 
142  return EC_succeed;
143 }
144 
145 
146 int
147 p_disconnect_from_blackboard()
148 {
149  if ( !g_blackboard.connected() )
150  {
151  printf( "p_disconnect_from_blackboard(): not connected\n" );
152  return EC_fail;
153  }
154 
155  g_blackboard.disconnect();
156 
157  return EC_succeed;
158 }
159 
160 
161 int
162 p_is_alive()
163 {
164  if ( !g_blackboard.connected() )
165  {
166  printf( "p_is_alive(): not connected\n" );
167  return EC_fail;
168  }
169 
170  if ( g_blackboard.instance()->is_alive() )
171  { return EC_succeed; }
172  else
173  { return EC_fail; }
174 }
175 
176 
177 int
178 p_open_interface()
179 {
180  if ( !g_blackboard.connected() )
181  {
182  printf("p_open_interface(): not connected\n" );
183  return EC_fail;
184  }
185 
186  EC_atom mode;
187  char* interface_type;
188  char* interface_id;
189 
190  if ( EC_succeed != EC_arg( 1 ).is_atom( &mode) )
191  {
192  printf( "p_open_interface(): no mode given\n" );
193  return EC_fail;
194  }
195 
196  if ( EC_succeed != EC_arg( 2 ).is_string( &interface_type ) )
197  {
198  printf( "p_open_interface(): no type given\n" );
199  return EC_fail;
200  }
201 
202  if ( EC_succeed != EC_arg ( 3 ).is_string( &interface_id ) )
203  {
204  printf( "p_open_interface(): no id given\n" );
205  return EC_fail;
206  }
207 
208  try
209  {
210  Interface* iface;
211 
212  if ( 0 == strcmp( "w", mode.name() ) )
213  { iface = g_blackboard.instance()->open_for_writing( interface_type, interface_id ); }
214  else
215  { iface = g_blackboard.instance()->open_for_reading( interface_type, interface_id ); }
216 
217  g_blackboard.interfaces().push_back( iface );
218  }
219  catch (Exception& e)
220  {
221  e.print_trace();
222  return EC_fail;
223  }
224 
225  return EC_succeed;
226 }
227 
228 
229 int
230 p_close_interface()
231 {
232  if ( !g_blackboard.connected() )
233  {
234  printf("p_close_interface(): not connected\n" );
235  return EC_fail;
236  }
237 
238  // char* interface_type;
239  char* interface_id;
240 
241  if ( EC_succeed != EC_arg( 1 ).is_string( &interface_id ) )
242  {
243  printf( "p_close_interface(): no id given\n" );
244  return EC_fail;
245  }
246 
247  bool iface_found = false;
248 
249  for ( vector< Interface* >::iterator it = g_blackboard.interfaces().begin();
250  it != g_blackboard.interfaces().end();
251  ++it )
252  {
253  if ( 0 == strcmp( (*it)->id(), interface_id ) )
254 
255  {
256  iface_found = true;
257  g_blackboard.instance()->close( *it );
258  g_blackboard.interfaces().erase( it );
259  break;
260  }
261  }
262 
263  if ( iface_found )
264  { return EC_succeed; }
265  else
266  { return EC_fail; }
267 }
268 
269 
270 int
271 p_has_writer()
272 {
273  char* interface_id;
274 
275  if ( EC_succeed != EC_arg( 1 ).is_string( &interface_id ) )
276  {
277  printf( "p_has_writer(): no id given\n" );
278  return EC_fail;
279  }
280 
281  for ( vector< Interface* >::iterator it = g_blackboard.interfaces().begin();
282  it != g_blackboard.interfaces().end();
283  ++it )
284  {
285  if ( 0 == strcmp( (*it)->id(), interface_id ) )
286  {
287  if ( (*it)->has_writer() )
288  { return EC_succeed; }
289  else
290  { return EC_fail; }
291 
292  break;
293  }
294  }
295 
296  return EC_fail;
297 }
298 
299 
300 int
301 p_instance_serial()
302 {
303  if ( !g_blackboard.connected() )
304  {
305  printf( "p_instance_serial(): not connected to blackboard\n" );
306  return EC_fail;
307  }
308 
309  char* interface_id;
310  if ( EC_succeed != EC_arg( 1 ).is_string( &interface_id ) )
311  {
312  printf( "p_instance_serial(): no interface id given\n" );
313  return EC_fail;
314  }
315 
316  for ( vector< Interface* >::iterator iit = g_blackboard.interfaces().begin();
317  iit != g_blackboard.interfaces().end();
318  ++iit )
319  {
320  if ( 0 == strcmp( (*iit)->id(), interface_id ) )
321  {
322  if ( EC_succeed != EC_arg( 2 ).unify( (*iit)->serial() ) )
323  {
324  printf( "p_instance_serial(): could not bind return value\n" );
325  return EC_fail;
326  }
327 
328  return EC_succeed;
329  }
330  }
331 
332  return EC_fail;
333 }
334 
335 
336 int
337 p_read_interfaces()
338 {
339  for ( vector< Interface* >::iterator it = g_blackboard.interfaces().begin();
340  it != g_blackboard.interfaces().end();
341  ++it )
342  {
343  (*it)->read();
344  }
345 
346  return EC_succeed;
347 }
348 
349 int
350 p_write_interfaces()
351 {
352  for ( vector< Interface* >::iterator it = g_blackboard.interfaces().begin();
353  it != g_blackboard.interfaces().end();
354  ++it )
355  {
356  if ( (*it)->is_writer() )
357  { (*it)->write(); }
358  }
359 
360  return EC_succeed;
361 }
362 
363 int
364 p_read_from_interface()
365 {
366  char* interface_id;
367  char* field;
368 
369  if ( EC_succeed != EC_arg( 1 ).is_string( &interface_id ) )
370  {
371  printf( "p_read_from_interface(): no interface id given\n" );
372  return EC_fail;
373  }
374 
375  if ( EC_succeed != EC_arg( 2 ).is_string( &field ) )
376  {
377  printf( "p_read_from_interface(): no field given\n" );
378  return EC_fail;
379  }
380 
381  vector< Interface* >::iterator it;
382  for ( it = g_blackboard.interfaces().begin();
383  it != g_blackboard.interfaces().end();
384  ++it )
385  {
386  if ( 0 == strcmp( interface_id, (*it)->id() ) )
387  {
389  for ( fit = (*it)->fields();
390  fit != (*it)->fields_end();
391  ++fit )
392  {
393  if ( 0 == strcmp( field, fit.get_name() ) )
394  {
395  switch ( fit.get_type() ) {
396  case IFT_BOOL:
397  if ( fit.get_bool() )
398  {
399  if ( EC_succeed != EC_arg( 3 ).unify( EC_atom( (char*) "true" ) ) )
400  {
401  printf( "p_read_from_interface(): could not bind return value\n" );
402  return EC_fail;
403  }
404  }
405  else
406  {
407  if ( EC_succeed != EC_arg( 3 ).unify( EC_atom( (char*) "false" ) ) )
408  {
409  printf( "p_read_from_interface(): could not bind return value\n" );
410  return EC_fail;
411  }
412  }
413 
414  break;
415 
416  case IFT_INT8:
417  if ( EC_succeed != EC_arg( 3 ).unify( EC_word( (long) fit.get_int8() ) ) )
418  {
419  printf( "p_read_from_interface(): could not bind return value\n" );
420  return EC_fail;
421  }
422 
423  break;
424 
425  case IFT_UINT8:
426  if ( EC_succeed != EC_arg( 3 ).unify( EC_word( (long) fit.get_uint8() ) ) )
427  {
428  printf( "p_read_from_interface(): could not bind return value\n" );
429  return EC_fail;
430  }
431 
432  break;
433 
434  case IFT_INT16:
435  if ( EC_succeed != EC_arg( 3 ).unify( EC_word( (long) fit.get_int16() ) ) )
436  {
437  printf( "p_read_from_interface(): could not bind return value\n" );
438  return EC_fail;
439  }
440 
441  break;
442 
443  case IFT_UINT16:
444  if ( EC_succeed != EC_arg( 3 ).unify( EC_word( (long) fit.get_uint16() ) ) )
445  {
446  printf( "p_read_from_interface(): could not bind return value\n" );
447  return EC_fail;
448  }
449 
450  break;
451 
452  case IFT_INT32:
453  if ( EC_succeed != EC_arg( 3 ).unify( EC_word( (long) fit.get_int32() ) ) )
454  {
455  printf( "bb_read_interface/3: could not bind value\n" );
456  return EC_fail;
457  }
458 
459  break;
460 
461  case IFT_UINT32:
462  if ( EC_succeed != EC_arg( 3 ).unify( EC_word( (long) fit.get_uint32() ) ) )
463  {
464  printf( "p_read_from_interface(): could not bind return value\n" );
465  return EC_fail;
466  }
467 
468  break;
469 
470  case IFT_INT64:
471  if ( EC_succeed != EC_arg( 3 ).unify( EC_word( (long) fit.get_int64() ) ) )
472  {
473  printf( "p_read_from_interface(): could not bind return value\n" );
474  return EC_fail;
475  }
476 
477  break;
478 
479  case IFT_UINT64:
480  if ( EC_succeed != EC_arg( 3 ).unify( EC_word( (long) fit.get_uint64() ) ) )
481  {
482  printf( "p_read_from_interface(): could not bind return value\n" );
483  return EC_fail;
484  }
485 
486  break;
487 
488  case IFT_FLOAT:
489  if ( EC_succeed != EC_arg( 3 ).unify( EC_word( (double) fit.get_float() ) ) )
490  {
491  printf( "p_read_from_interface(): could not bind return value\n" );
492  return EC_fail;
493  }
494 
495  break;
496 
497  case IFT_STRING:
498  if ( EC_succeed != EC_arg( 3 ).unify( EC_word( fit.get_string() ) ) )
499  {
500  printf( "p_read_from_interface(): could not bind return value\n" );
501  return EC_fail;
502  }
503 
504  break;
505 
506  case IFT_BYTE:
507  printf( "p_read_from_interface(): NOT YET IMPLEMENTED\n" );
508  break;
509 
510  case IFT_ENUM:
511  if ( EC_succeed != EC_arg( 3 ).unify( fit.get_value_string() ) )
512  {
513  printf( "p_read_from_interface(): could not bind return value\n" );
514  return EC_fail;
515  }
516 
517  default:
518  break;
519  }
520 
521  break;
522  }
523 
524  }
525 
526  if ( fit == (*it)->fields_end() )
527  {
528  printf( "p_read_from_interface(): interface %s has no field %s\n",
529  interface_id, field );
530 
531  return EC_fail;
532  }
533 
534  break;
535  }
536  }
537 
538  if ( it == g_blackboard.interfaces().end() )
539  {
540  printf( "p_read_from_interface(): no interface with id %s found\n",
541  interface_id );
542 
543  return EC_fail;
544  }
545 
546  return EC_succeed;
547 }
548 
549 int
550 p_write_to_interface()
551 {
552  char* interface_id;
553  char* field;
554 
555  if ( EC_succeed != EC_arg( 1 ).is_string( &interface_id ) )
556  {
557  printf( "p_write_to_interface(): no interface id given\n" );
558  return EC_fail;
559  }
560 
561  if ( EC_succeed != EC_arg( 2 ).is_string( &field ) )
562  {
563  printf( "p_write_to_interface(): no field given\n" );
564  return EC_fail;
565  }
566 
567  vector< Interface* >::iterator it;
568  for ( it = g_blackboard.interfaces().begin();
569  it != g_blackboard.interfaces().end();
570  ++it )
571  {
572  if ( 0 == strcmp( interface_id, (*it)->id() ) )
573  {
574  if ( !(*it)->is_writer() )
575  {
576  printf( "p_write_to_interface(): not a writer\n" );
577  return EC_fail;
578  }
579 
581  for ( fit = (*it)->fields();
582  fit != (*it)->fields_end();
583  ++fit )
584  {
585  if ( 0 == strcmp( field, fit.get_name() ) )
586  {
587  switch ( fit.get_type() ) {
588  case IFT_BOOL:
589  {
590  EC_atom val;
591  if ( EC_succeed != EC_arg( 3 ).is_atom( &val ) )
592  {
593  printf( "p_write_to_interface(): no value_given\n" );
594  return EC_fail;
595  }
596 
597  if ( 0 == strcmp( "true", val.name() ) )
598  { fit.set_bool( true ); }
599  else if ( 0 == strcmp( "false", val.name() ) )
600  { fit.set_bool( false ); }
601  else
602  {
603  printf( "p_write_to_interface(): boolean value neither true nor false\n" );
604  return EC_fail;
605  }
606  }
607 
608  break;
609 
610  case IFT_INT8:
611  {
612  long val;
613  if ( EC_succeed != EC_arg( 3 ).is_long( &val ) )
614  {
615  printf( "p_write_to_interface(): no value given\n" );
616  return EC_fail;
617  }
618 
619  fit.set_int8( (int8_t) val );
620  }
621 
622  break;
623 
624  case IFT_UINT8:
625  {
626  long val;
627  if ( EC_succeed != EC_arg( 3 ).is_long( &val ) )
628  {
629  printf( "p_write_to_interface(): no value given\n" );
630  return EC_fail;
631  }
632 
633  fit.set_uint8( (uint8_t) val );
634  }
635 
636  break;
637 
638  case IFT_INT16:
639  {
640  long val;
641  if ( EC_succeed != EC_arg( 3 ).is_long( &val ) )
642  {
643  printf( "p_write_to_interface(): no value given\n" );
644  return EC_fail;
645  }
646 
647  fit.set_int16( (int16_t) val );
648  }
649 
650  break;
651 
652  case IFT_UINT16:
653  {
654  long val;
655  if ( EC_succeed != EC_arg( 3 ).is_long( &val ) )
656  {
657  printf( "p_write_to_interface(): no value given\n" );
658  return EC_fail;
659  }
660 
661  fit.set_uint16( (uint16_t) val );
662  }
663 
664  break;
665 
666  case IFT_INT32:
667  {
668  long val;
669  if ( EC_succeed != EC_arg( 3 ).is_long( &val ) )
670  {
671  printf( "p_write_to_interface(): no value given\n" );
672  return EC_fail;
673  }
674 
675  fit.set_int32( (int32_t) val );
676  }
677 
678  break;
679 
680  case IFT_UINT32:
681  {
682  long val;
683  if ( EC_succeed != EC_arg( 3 ).is_long( &val ) )
684  {
685  printf( "p_write_to_interface(): no value given\n" );
686  return EC_fail;
687  }
688 
689  fit.set_uint32( (uint32_t) val );
690  }
691 
692  break;
693 
694  case IFT_INT64:
695  {
696  long val;
697  if ( EC_succeed != EC_arg( 3 ).is_long( &val ) )
698  {
699  printf( "p_write_to_interface(): no value given\n" );
700  return EC_fail;
701  }
702 
703  fit.set_int64( (int64_t) val );
704  }
705 
706  break;
707 
708  case IFT_UINT64:
709  {
710  long val;
711  if ( EC_succeed != EC_arg( 3 ).is_long( &val ) )
712  {
713  printf( "p_write_to_interface(): no value given\n" );
714  return EC_fail;
715  }
716 
717  fit.set_uint64( (uint64_t) val );
718  }
719 
720  break;
721 
722  case IFT_FLOAT:
723  {
724  double val;
725  if ( EC_succeed != EC_arg( 3 ).is_double( &val ) )
726  {
727  printf( "p_write_to_interface(): no value given\n" );
728  return EC_fail;
729  }
730 
731  fit.set_float( (float) val );
732  }
733 
734  break;
735 
736  case IFT_STRING:
737  {
738  char* val;
739  if ( EC_succeed != EC_arg( 3 ).is_string( &val ) )
740  {
741  printf( "p_write_to_interface(): no value given\n" );
742  return EC_fail;
743  }
744 
745  fit.set_string( val );
746  }
747 
748  break;
749 
750  case IFT_BYTE:
751  case IFT_ENUM:
752  printf( "p_write_to_interface(): NOT YET IMPLEMENTET\n" );
753  break;
754 
755  default:
756  break;
757  }
758 
759  break;
760  }
761 
762  }
763 
764  if ( fit == (*it)->fields_end() )
765  {
766  printf( "p_write_to_interface(): interface %s has no field %s\n",
767  interface_id, field );
768 
769  return EC_fail;
770  }
771 
772  break;
773  }
774  }
775 
776  if ( it == g_blackboard.interfaces().end() )
777  {
778  printf( "p_write_to_interface(): no interface with id %s found\n",
779  interface_id );
780 
781  return EC_fail;
782  }
783 
784  return EC_succeed;
785 
786 }
787 
788 
789 int
790 p_send_message()
791 {
792  char* interface_id;
793  char* message_type;
794 
795  if ( EC_succeed != EC_arg( 1 ).is_string( &interface_id ) )
796  {
797  printf( "p_send_message(): no interface id given\n" );
798  return EC_fail;
799  }
800 
801  if ( EC_succeed != EC_arg( 2 ).is_string( &message_type ) )
802  {
803  printf( "p_send_message(): no message type given\n" );
804  return EC_fail;
805  }
806 
807  vector< Interface* >::iterator it;
808  for ( it = g_blackboard.interfaces().begin();
809  it != g_blackboard.interfaces().end();
810  ++it )
811  {
812  if ( 0 == strcmp( interface_id, (*it)->id() ) )
813  {
814  if ( (*it)->is_writer() )
815  {
816  printf( "p_send_message(): interface with id %s is a writer\n", interface_id );
817  return EC_fail;
818  }
819 
820  try
821  {
822  Message* msg = (*it)->create_message( message_type );
823 
824  EC_word head;
825  EC_word tail;
826  if ( EC_succeed == EC_arg( 3 ).is_list( head, tail ) )
827  {
828  if ( !process_message_args(msg, ::list(head, tail)) )
829  {
830  return EC_fail;
831  };
832  }
833 
834  (*it)->msgq_enqueue( msg );
835  }
836  catch (Exception& e)
837  {
838  e.print_trace();
839  return EC_fail;
840  }
841 
842  break;
843  }
844  }
845 
846  if ( it == g_blackboard.interfaces().end() )
847  {
848  printf( "p_send_message(): no interface with name %s\n", interface_id );
849  return EC_fail;
850  }
851 
852  return EC_succeed;
853 }
854 
855 
856 int
857 p_recv_messages()
858 {
859  char* interface_id;
860 
861  if ( EC_succeed != EC_arg( 1 ).is_string( &interface_id ) )
862  {
863  printf( "p_recv_messages(): no interface id given\n" );
864  return EC_fail;
865  }
866 
867  vector< Interface* >::iterator it;
868 
869  for ( it = g_blackboard.interfaces().begin();
870  it != g_blackboard.interfaces().end();
871  ++it )
872  {
873  if ( 0 == strcmp( interface_id, (*it)->id() ) )
874  {
875  EC_word msg_list = nil();
876 
877  while ( !(*it)->msgq_empty() )
878  {
879  Message* msg = (*it)->msgq_first();
880 
881  // construct list of key-value pairs: [[field1, val1], [field2, val2], ...]
882  EC_word args = nil();
883  for ( InterfaceFieldIterator fit = msg->fields();
884  fit != msg->fields_end();
885  ++fit )
886  {
887  EC_word value;
888 
889  switch ( fit.get_type() )
890  {
891  case IFT_BOOL:
892  if ( fit.get_bool() )
893  { value = EC_atom( (char*) "true" ); }
894  else
895  { value = EC_atom( (char*) "false" ); }
896 
897  break;
898 
899  case IFT_INT8:
900  value = EC_word( (long) fit.get_int8() );
901  break;
902 
903  case IFT_UINT8:
904  value = EC_word( (long) fit.get_uint8() );
905  break;
906 
907  case IFT_INT16:
908  value = EC_word( (long) fit.get_int16() );
909  break;
910 
911  case IFT_UINT16:
912  value = EC_word( (long) fit.get_uint16() );
913  break;
914 
915  case IFT_INT32:
916  value = EC_word( (long) fit.get_int32() );
917  break;
918 
919  case IFT_UINT32:
920  value = EC_word( (long) fit.get_uint32() );
921  break;
922 
923  case IFT_INT64:
924  value = EC_word( (long) fit.get_int64() );
925  break;
926 
927  case IFT_UINT64:
928  value = EC_word( (long) fit.get_uint64() );
929  break;
930 
931  case IFT_FLOAT:
932  value = EC_word( (double) fit.get_float() );
933  break;
934 
935  case IFT_STRING:
936  value = EC_word( fit.get_string() );
937  break;
938 
939  case IFT_BYTE:
940  case IFT_ENUM:
941  printf( "p_recv_messages(): NOT YET IMPLEMENTED\n" );
942  break;
943 
944  default:
945  printf( "p_recv_messages(): unknown field type\n" );
946  }
947 
948  EC_word field = ::list( EC_word( fit.get_name() ),
949  ::list( value, nil() ) );
950  args = ::list(field, args);
951  }
952 
953  // construct list of messages: [[MsgType, [[Key1, Val1], ...]], ... ]
954  msg_list = ::list( ::list( EC_word( msg->type() ),
955  ::list(args, nil() ) ),
956  msg_list );
957 
958  (*it)->msgq_pop();
959  }
960 
961  if ( EC_succeed != EC_arg( 2 ).unify( msg_list ) )
962  {
963  printf( "p_recv_messages(): could not bind return value\n" );
964  return EC_fail;
965  }
966 
967  break;
968  }
969  }
970 
971  if ( it == g_blackboard.interfaces().end() )
972  {
973  printf( "p_recv_messages(): no interface with id %s found\n", interface_id );
974  return EC_fail;
975  }
976 
977  return EC_succeed;
978 }
979 
980 
981 bool
982 process_message_args(Message* msg, EC_word arg_list)
983 {
984  EC_word head;
985  EC_word tail;
986 
987  for ( ; EC_succeed == arg_list.is_list(head, tail) ; arg_list = tail )
988  {
989  // [field, value]
990  EC_word field;
991  EC_word value;
992  EC_word t1;
993  EC_word t2;
994 
995  if ( EC_succeed != head.is_list( field, t1 ) ||
996  EC_succeed != t1.is_list( value, t2 ) )
997  {
998  printf( "p_send_messge(): could not parse argument list\n" );
999  return false;
1000  }
1001 
1002  char* field_name;
1003  if ( EC_succeed != field.is_string( &field_name ) )
1004  {
1005  printf( "p_send_message(): malformed argument list\n" );
1006  return false;
1007  }
1008 
1010  for ( fit = msg->fields();
1011  fit != msg->fields_end();
1012  ++fit )
1013  {
1014  if ( 0 == strcmp( fit.get_name(), field_name ) )
1015  {
1016  switch( fit.get_type() )
1017  {
1018  case IFT_BOOL:
1019  {
1020  EC_atom val;
1021  if ( EC_succeed != value.is_atom( &val ) )
1022  {
1023  printf( "p_send_message(): no value_given (bool)\n" );
1024  return false;
1025  }
1026 
1027  if ( 0 == strcmp( "true", val.name() ) )
1028  { fit.set_bool( true ); }
1029  else if ( 0 == strcmp( "false", val.name() ) )
1030  { fit.set_bool( false ); }
1031  else
1032  {
1033  printf( "p_send_message(): boolean value neither true nor false\n" );
1034  return false;
1035  }
1036  }
1037 
1038  break;
1039 
1040  case IFT_INT8:
1041  {
1042  long val;
1043  if ( EC_succeed != value.is_long( &val ) )
1044  {
1045  printf( "p_send_message(): no value given (int8)\n" );
1046  return false;
1047  }
1048 
1049  fit.set_int8( (int8_t) val );
1050  }
1051 
1052  break;
1053 
1054  case IFT_UINT8:
1055  {
1056  long val;
1057  if ( EC_succeed != value.is_long( &val ) )
1058  {
1059  printf( "p_send_message(): no value given (uint8)\n" );
1060  return false;
1061  }
1062 
1063  fit.set_uint8( (uint8_t) val );
1064  }
1065 
1066  break;
1067 
1068  case IFT_INT16:
1069  {
1070  long val;
1071  if ( EC_succeed != value.is_long( &val ) )
1072  {
1073  printf( "p_send_message(): no value given (int16)\n" );
1074  return false;
1075  }
1076 
1077  fit.set_int16( (int16_t) val );
1078  }
1079 
1080  break;
1081 
1082  case IFT_UINT16:
1083  {
1084  long val;
1085  if ( EC_succeed != value.is_long( &val ) )
1086  {
1087  printf( "p_send_message(): no value given (uint16)\n" );
1088  return false;
1089  }
1090 
1091  fit.set_uint16( (uint16_t) val );
1092  }
1093 
1094  break;
1095 
1096  case IFT_INT32:
1097  {
1098  long val;
1099  if ( EC_succeed != value.is_long( &val ) )
1100  {
1101  printf( "p_send_message(): no value given (int32)\n" );
1102  return false;
1103  }
1104 
1105  fit.set_int32( (int32_t) val );
1106  }
1107 
1108  break;
1109 
1110  case IFT_UINT32:
1111  {
1112  long val;
1113  if ( EC_succeed != value.is_long( &val ) )
1114  {
1115  printf( "p_send_message(): no value given (uint32)\n" );
1116  return false;
1117  }
1118 
1119  fit.set_uint32( (uint32_t) val );
1120  }
1121 
1122  break;
1123 
1124  case IFT_INT64:
1125  {
1126  long val;
1127  if ( EC_succeed != value.is_long( &val ) )
1128  {
1129  printf( "p_send_message(): no value given (int64)\n" );
1130  return false;
1131  }
1132 
1133  fit.set_int64( (int64_t) val );
1134  }
1135 
1136  break;
1137 
1138  case IFT_UINT64:
1139  {
1140  long val;
1141  if ( EC_succeed != value.is_long( &val ) )
1142  {
1143  printf( "p_send_message(): no value given (uint64)\n" );
1144  return false;
1145  }
1146 
1147  fit.set_uint64( (uint64_t) val );
1148  }
1149 
1150  break;
1151 
1152  case IFT_FLOAT:
1153  {
1154  double val;
1155  if ( EC_succeed != value.is_double( &val ) )
1156  {
1157  printf( "p_send_message(): no value given (float)\n" );
1158  return false;
1159  }
1160 
1161  fit.set_float( (float) val );
1162  }
1163 
1164  break;
1165 
1166  case IFT_STRING:
1167  {
1168  char* val;
1169  if ( EC_succeed != value.is_string( &val ) )
1170  {
1171  printf( "p_send_message(): no value given (string)\n" );
1172  return false;
1173  }
1174 
1175  fit.set_string( val );
1176  }
1177 
1178  break;
1179 
1180  case IFT_BYTE:
1181  case IFT_ENUM:
1182  printf( "p_send_message(): NOT YET IMPLEMENTET\n" );
1183  break;
1184 
1185  default:
1186  break;
1187  }
1188 
1189  break;
1190  }
1191  }
1192 
1193  if ( fit == msg->fields_end() )
1194  {
1195  printf( "p_send_message(): message has no field with name %s\n",
1196  field_name );
1197  return false;
1198  }
1199  }
1200 
1201  return true;
1202 }