Пример #1
0
TEST_F(PubSubTest, PubSub)
{
  typedef darc::pubsub::publisher<int> MyPub;
  typedef darc::pubsub::subscriber<int> MySub;

  boost::asio::io_service io_service;

  darc::pubsub::message_service my_service1(peer1, io_service, ns1);
  MyPub test_pub(io_service, my_service1);

  darc::pubsub::message_service my_service2(peer2, io_service, ns2);
  MySub test_sub(io_service, my_service2);

  test_pub.attach("id1");
  test_sub.attach("id1");

  test_sub.addCallback(boost::bind(&handler, _1));

  boost::shared_ptr<int> data = boost::make_shared<int>(5);

  test_pub.publish(data);
  sleep(1);
  io_service.run();
  sleep(1);
  test_pub.publish(data);
  io_service.reset();
  io_service.run();
  sleep(1);
  test_pub.publish(data);
  io_service.reset();
  io_service.run();
  sleep(1);
  test_pub.publish(data);
  io_service.reset();
  io_service.run();

};
int main (int argc, char **argv)
{
	static const char opts[] = "c:i:t:n:x:u:";

	int pub_count = 1;
	int option;

	while (1) {
		option = getopt(argc, argv, opts);
		if (option == -1) 
			break;

		switch (option) {
		case 'u': {
			int fd;
			int sz;

			strcpy(user_data_file, optarg);
			fd = open(user_data_file, O_RDONLY);
			if (fd < 0) {
				printf("Can't open user data file %s\n",
						user_data_file);
				exit(1);
			}
			sz = read(fd, user_data, 65536);
			if (sz < 0) {
				perror("subscription\n");
				exit(1);
			}
			close(fd);
			user_data_size = sz;
			break;
		}

		case 'c':
			strcpy(channel, optarg);
			break;
		case 'n':
			strcpy(pubname, optarg);
			break;
		case 'i':
			subscription_id = 
				(unsigned int)strtoul(optarg, NULL, 0);
			break;
		case 't':
			ret_time = strtoull(optarg, NULL, 0);
			ret_time *= 1000000000;
			break;
		case 'x':
			pub_count = strtoul(optarg, NULL, 0);
			break;
		default:
			printf("invalid arg: \"%s\"\n", optarg);
			return 1;
		}
	}

	while (pub_count--) {
		test_pub();
	}

	return 0;
}