Exemplo n.º 1
0
/** Get a constrained list of interfaces.
 * @param type_pattern tyoe pattern, may contain shell-like wildcards * (any number
 * of characters) and ? (one character), cf. man fnmatch().
 * @param id_pattern ID pattern, may contain shell-like wildcards * (any number
 * of characters) and ? (one character), cf. man fnmatch().
 * @return list of currently existing interfaces matching the given type and
 * ID patterns. List may be outdated on return since there maybe concurrent
 * actions.
 */
InterfaceInfoList *
BlackBoardInterfaceManager::list(const char *type_pattern,
				 const char *id_pattern) const
{
  InterfaceInfoList *infl = new InterfaceInfoList();

  memmgr->lock();
  interface_header_t *ih;
  BlackBoardMemoryManager::ChunkIterator cit;
  for ( cit = memmgr->begin(); cit != memmgr->end(); ++cit ) {
    ih = (interface_header_t *)*cit;
    Interface::interface_data_ts_t *data_ts =
      (Interface::interface_data_ts_t *)((char *)*cit + sizeof(interface_header_t));
    char type[__INTERFACE_TYPE_SIZE + 1];
    char id[__INTERFACE_ID_SIZE + 1];
    // ensure NULL-termination
    type[__INTERFACE_TYPE_SIZE] = 0;
    id[__INTERFACE_ID_SIZE] = 0;
    strncpy(type, ih->type, __INTERFACE_TYPE_SIZE);
    strncpy(id, ih->id, __INTERFACE_ID_SIZE);
    if ((fnmatch(type_pattern, type, FNM_NOESCAPE) == 0) &&
	(fnmatch(id_pattern, id, FNM_NOESCAPE) == 0))
    {
      std::string uid = std::string(type) + "::" + id;
      infl->append(ih->type, ih->id, ih->hash, ih->serial,
		   ih->flag_writer_active, ih->num_readers,
		   readers(uid), writer(uid),
		   fawkes::Time(data_ts->timestamp_sec, data_ts->timestamp_usec));
    }
  }

  memmgr->unlock();

  return infl;
}
/** Get a constrained list of interfaces.
 * @param type_pattern tyoe pattern, may contain shell-like wildcards * (any number
 * of characters) and ? (one character), cf. man fnmatch().
 * @param id_pattern ID pattern, may contain shell-like wildcards * (any number
 * of characters) and ? (one character), cf. man fnmatch().
 * @return list of currently existing interfaces matching the given type and
 * ID patterns. List may be outdated on return since there maybe concurrent
 * actions.
 */
InterfaceInfoList *
BlackBoardInterfaceManager::list(const char *type_pattern,
				 const char *id_pattern) const
{
  InterfaceInfoList *infl = new InterfaceInfoList();

  memmgr->lock();
  interface_header_t *ih;
  BlackBoardMemoryManager::ChunkIterator cit;
  for ( cit = memmgr->begin(); cit != memmgr->end(); ++cit ) {
    ih = (interface_header_t *)*cit;
    char type[__INTERFACE_TYPE_SIZE + 1];
    char id[__INTERFACE_ID_SIZE + 1];
    // ensure NULL-termination
    type[__INTERFACE_TYPE_SIZE] = 0;
    id[__INTERFACE_ID_SIZE] = 0;
    strncpy(type, ih->type, __INTERFACE_TYPE_SIZE);
    strncpy(id, ih->id, __INTERFACE_ID_SIZE);
    if ((fnmatch(type_pattern, type, FNM_NOESCAPE) == 0) &&
	(fnmatch(id_pattern, id, FNM_NOESCAPE) == 0))
    {
      infl->append(ih->type, ih->id, ih->hash, ih->serial,
		   ih->flag_writer_active, ih->num_readers);
    }
  }

  memmgr->unlock();

  return infl;
}
/** Get a list of interfaces.
 * @return list of currently existing interfaces. List may be outdated on
 * return since there maybe concurrent actions.
 */
InterfaceInfoList *
BlackBoardInterfaceManager::list_all() const
{
  InterfaceInfoList *infl = new InterfaceInfoList();

  memmgr->lock();
  interface_header_t *ih;
  BlackBoardMemoryManager::ChunkIterator cit;
  for ( cit = memmgr->begin(); cit != memmgr->end(); ++cit ) {
    ih = (interface_header_t *)*cit;
    infl->append(ih->type, ih->id, ih->hash, ih->serial,
		 ih->flag_writer_active, ih->num_readers);
  }

  memmgr->unlock();

  return infl;
}
Exemplo n.º 4
0
int
main(int argc, char **argv)
{
  signal(SIGINT, signal_handler);

  LocalBlackBoard *llbb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);
  BlackBoard *lbb = llbb;

  FawkesNetworkServerThread  *fns = new FawkesNetworkServerThread(1910);
  fns->start();

  llbb->start_nethandler(fns);

  BlackBoard *rbb = new RemoteBlackBoard("localhost", 1910);

  InterfaceInfoList *infl = rbb->list_all();
  for (InterfaceInfoList::iterator i = infl->begin(); i != infl->end(); ++i) {
    const unsigned char *hash = (*i).hash();
    char phash[__INTERFACE_HASH_SIZE * 2 + 1];
    memset(phash, 0, sizeof(phash));
    for (unsigned int j = 0; j < __INTERFACE_HASH_SIZE; ++j) {
      sprintf(&phash[j * 2], "%02x", hash[j]);
    }
    printf("%s::%s (%s), w:%i  r:%u  s:%u\n",
	   (*i).type(), (*i).id(), phash, (*i).has_writer(),
	   (*i).num_readers(), (*i).serial());
  }
  delete infl;

  //TestInterface *ti_writer;
  TestInterface *ti_reader;
  TestInterface *ti_writer;
  try {
    cout << "Opening interfaces.. " << flush;
    ti_writer = rbb->open_for_writing<TestInterface>("SomeID");
    ti_reader = rbb->open_for_reading<TestInterface>("SomeID");
    cout << "success, "
	 << "writer hash=" << ti_writer->hash_printable()
	 << "  reader hash=" << ti_reader->hash_printable()
	 << endl;
  } catch (Exception &e) {
    cout << "failed! Aborting" << endl;
    e.print_trace();
    exit(1);
  }

  try {
    cout << "Trying to open second writer.. " << flush;
    TestInterface *ti_writer_two;
    ti_writer_two = rbb->open_for_writing<TestInterface>("SomeID");
    cout << "BUG: Detection of second writer did NOT work!" << endl;
    exit(2);
  } catch (BlackBoardWriterActiveException &e) {
    cout << "exception caught as expected, detected and prevented second writer!" << endl;
  }

  try {
    cout << "Trying to open third writer.. " << flush;
    TestInterface *ti_writer_three;
    ti_writer_three = rbb->open_for_writing<TestInterface>("AnotherID");
    cout << "No exception as expected, different ID ok!" << endl;
    rbb->close(ti_writer_three);
  } catch (BlackBoardWriterActiveException &e) {
    cout << "BUG: Third writer with different ID detected as another writer!" << endl;
    exit(3);
  }

  cout << endl << endl
       << "Running data tests ==================================================" << endl;

  cout << "Writing initial value ("
       << TestInterface::TEST_CONSTANT << ") into interface as TestInt" << endl;
  ti_writer->set_test_int( TestInterface::TEST_CONSTANT );
  try {
    ti_writer->write();
  } catch (InterfaceWriteDeniedException &e) {
    cout << "BUG: caught write denied exception" << endl;
    e.print_trace();
  }

  cout << "Giving some time to have value processed" << endl;
  usleep(100000);

  cout << "Reading value from reader interface.. " << flush;
  ti_reader->read();
  int val = ti_reader->test_int();
  if ( val == TestInterface::TEST_CONSTANT ) {
    cout << " success, value is " << ti_reader->test_int() << " as expected" << endl;
  } else {
    cout << " failure, value is " << ti_reader->test_int() << ", expected "
	 << TestInterface::TEST_CONSTANT << endl;
  }

  cout << "Closing interfaces.. " << flush;
  try {
    rbb->close(ti_reader);
    rbb->close(ti_writer);
    cout << "done" << endl;
  } catch (Exception &e) {
    cout << "failed" << endl;
    e.print_trace();
  }

  cout << endl << endl << "Starting MESSAGING tests" << endl 
       << "Press Ctrl-C to continue with next test" << endl << endl;

  ti_writer = lbb->open_for_writing<TestInterface>("Messaging");
  ti_reader = rbb->open_for_reading<TestInterface>("Messaging");

  printf("Writer serial: %u  shifted: %u\n", ti_writer->serial(), ti_writer->serial() << 16);
  printf("Reader serial: %u  shifted: %u\n", ti_reader->serial(), ti_reader->serial() << 16);

  test_messaging(ti_reader, ti_writer);

  rbb->close(ti_reader);
  lbb->close(ti_writer);

  cout << endl << endl << "Starting MESSAGING tests, doing repeater scenario" << endl 
       << "Press Ctrl-C to continue with next test" << endl << endl;
  quit = false;

  delete rbb;

  LocalBlackBoard *repllbb = new LocalBlackBoard(BLACKBOARD_MEMSIZE);

  FawkesNetworkServerThread  *repfns = new FawkesNetworkServerThread(1911);
  repfns->start();

  repllbb->start_nethandler(repfns);

  BlackBoard *rep_rbb = new RemoteBlackBoard("localhost", 1911);
  rbb = new RemoteBlackBoard("localhost", 1911);

  TestInterface *rep_reader;
  TestInterface *rep_writer;

  ti_writer = rbb->open_for_writing<TestInterface>("Messaging");
  ti_reader = lbb->open_for_reading<TestInterface>("Messaging");

  rep_reader = rep_rbb->open_for_reading<TestInterface>("Messaging");
  rep_writer = lbb->open_for_writing<TestInterface>("Messaging");

  printf("Writer serial: %u  shifted: %u\n", ti_writer->serial(), ti_writer->serial() << 16);
  printf("Reader serial: %u  shifted: %u\n", ti_reader->serial(), ti_reader->serial() << 16);

  SyncInterfaceListener *sil = new SyncInterfaceListener(rep_reader, rep_writer, rep_rbb, lbb);

  test_messaging(ti_reader, ti_writer);

  delete sil;
  lbb->close(ti_reader);
  rbb->close(ti_writer);
  rep_rbb->close(rep_reader);
  lbb->close(rep_writer);
  delete repllbb;
  delete rep_rbb;

  cout << "Tests done" << endl;

  delete rbb;
  delete llbb;
  delete fns;
}