コード例 #1
0
ファイル: qa_bb_remote.cpp プロジェクト: fuxiang90/fawkes
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;
}