diff options
Diffstat (limited to 'cpp/demo/IceStorm/clock/Subscriber.cpp')
-rw-r--r-- | cpp/demo/IceStorm/clock/Subscriber.cpp | 182 |
1 files changed, 112 insertions, 70 deletions
diff --git a/cpp/demo/IceStorm/clock/Subscriber.cpp b/cpp/demo/IceStorm/clock/Subscriber.cpp index 04ebe98a927..1d7faf18673 100644 --- a/cpp/demo/IceStorm/clock/Subscriber.cpp +++ b/cpp/demo/IceStorm/clock/Subscriber.cpp @@ -13,8 +13,6 @@ #include <Clock.h> -#include <map> - using namespace std; using namespace Demo; @@ -46,50 +44,30 @@ main(int argc, char* argv[]) void usage(const string& n) { - cerr << "Usage: " << n << " [--batch] [--datagram|--twoway|--ordered|--oneway] [topic]" << endl; + cerr << "Usage: " << n + << " [--batch] [--datagram|--twoway|--ordered|--oneway] [--retryCount count] [--id id] [topic]" << endl; } int Subscriber::run(int argc, char* argv[]) { - bool batch = false; - - enum Option { Datagram, Twoway, Oneway, Ordered}; - Option option = Oneway; + Ice::StringSeq args = Ice::argsToStringSeq(argc, argv); + args = communicator()->getProperties()->parseCommandLineOptions("Clock", args); + Ice::stringSeqToArgs(args, argc, argv); + bool batch = false; + enum Option { None, Datagram, Twoway, Oneway, Ordered}; + Option option = None; string topicName = "time"; + string id; + string retryCount; + int i; - if(argc > 4) - { - cerr << appName() << ": too many arguments" << endl; - usage(appName()); - return EXIT_FAILURE; - } - - if(argc >= 2) + for(i = 1; i < argc; ++i) { - int argIndex = 1; - string optionString = argv[argIndex]; - - if(optionString == "--batch") - { - batch = true; - if(argc >= 3) - { - argIndex++; - optionString = argv[argIndex]; - } - else - { - optionString = ""; - } - } - - if(optionString == "") - { - // done - } - else if(optionString == "--datagram") + string optionString = argv[i]; + Option oldoption = option; + if(optionString == "--datagram") { option = Datagram; } @@ -105,33 +83,75 @@ Subscriber::run(int argc, char* argv[]) { option = Ordered; } - else if(argIndex == argc - 2) + else if(optionString == "--batch") { - cerr << appName() << ": too many arguments" << endl; - usage(appName()); + batch = true; + } + else if(optionString == "--id") + { + ++i; + if(i >= argc) + { + usage(argv[0]); + return EXIT_FAILURE; + } + id = argv[i]; + } + else if(optionString == "--retryCount") + { + ++i; + if(i >= argc) + { + usage(argv[0]); + return EXIT_FAILURE; + } + retryCount = argv[i]; + } + else if(optionString.substr(0, 2) == "--") + { + usage(argv[0]); return EXIT_FAILURE; } else { - topicName = optionString; + topicName = argv[i++]; + break; } - argIndex++; - if(argIndex < argc) + if(oldoption != option && oldoption != None) { - topicName = argv[argIndex]; + usage(argv[0]); + return EXIT_FAILURE; } + } - if(topicName[0] == '-') + if(i != argc) + { + usage(argv[0]); + return EXIT_FAILURE; + } + + if(!retryCount.empty()) + { + if(option == None) { - cerr << appName() << ": invalid topic name" << endl; - usage(appName()); + option = Twoway; + } + else if(option != Twoway && option != Ordered) + { + cerr << argv[0] << ": retryCount requires a twoway proxy" << endl; return EXIT_FAILURE; } } + if(batch && (option == Twoway || option == Ordered)) + { + cerr << argv[0] << ": batch can only be set with oneway or datagram" << endl; + return EXIT_FAILURE; + } + IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::checkedCast( - communicator()->propertyToProxy("IceStorm.TopicManager.Proxy")); + communicator()->propertyToProxy("TopicManager.Proxy")); if(!manager) { cerr << appName() << ": invalid proxy" << endl; @@ -159,52 +179,74 @@ Subscriber::run(int argc, char* argv[]) Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Clock.Subscriber"); // - // Add a Servant for the Ice Object. + // Add a servant for the Ice object. If --id is used the identity + // comes from the command line, otherwise a UUID is used. // + // id is not directly altered since it is used below to detect + // whether subscribeAndGetPublisher can raise AlreadySubscribed. + // + Ice::Identity subId; + subId.name = id; + if(subId.name.empty()) + { + subId.name = IceUtil::generateUUID(); + } + Ice::ObjectPrx subscriber = adapter->add(new ClockI, subId); + IceStorm::QoS qos; - Ice::ObjectPrx subscriber = adapter->addWithUUID(new ClockI); + if(!retryCount.empty()) + { + qos["retryCount"] = retryCount; + } + // // Set up the proxy. // - if(option == Datagram) { - subscriber = subscriber->ice_datagram(); + if(batch) + { + subscriber = subscriber->ice_batchDatagram(); + } + else + { + subscriber = subscriber->ice_datagram(); + } } else if(option == Twoway) { // Do nothing to the subscriber proxy. Its already twoway. - } else if(option == Ordered) { - qos["reliability"] = "ordered"; // Do nothing to the subscriber proxy. Its already twoway. - - } - else if(option == Oneway) - { - subscriber = subscriber->ice_oneway(); + qos["reliability"] = "ordered"; } - - if(batch) + else if(option == Oneway || option == None) { - if(option == Twoway || option == Ordered) + if(batch) { - cerr << appName() << ": batch can only be set with oneway or datagram" << endl; - return EXIT_FAILURE; - } - if(option == Datagram) - { - subscriber = subscriber->ice_batchDatagram(); + subscriber = subscriber->ice_batchOneway(); } else { - subscriber = subscriber->ice_batchOneway(); + subscriber = subscriber->ice_oneway(); } } - topic->subscribeAndGetPublisher(qos, subscriber); + try + { + topic->subscribeAndGetPublisher(qos, subscriber); + } + catch(const IceStorm::AlreadySubscribed&) + { + // If we're manually setting the subscriber id ignore. + if(id.empty()) + { + throw; + } + cout << "reactivating persistent subscriber" << endl; + } adapter->activate(); shutdownOnInterrupt(); |