diff options
Diffstat (limited to 'cpp/demo/IceStorm/clock/Subscriber.cpp')
-rw-r--r-- | cpp/demo/IceStorm/clock/Subscriber.cpp | 121 |
1 files changed, 43 insertions, 78 deletions
diff --git a/cpp/demo/IceStorm/clock/Subscriber.cpp b/cpp/demo/IceStorm/clock/Subscriber.cpp index a75bc7563a4..788eb485e22 100644 --- a/cpp/demo/IceStorm/clock/Subscriber.cpp +++ b/cpp/demo/IceStorm/clock/Subscriber.cpp @@ -9,15 +9,25 @@ #include <Ice/Application.h> #include <IceStorm/IceStorm.h> -#include <IceUtil/UUID.h> -#include <ClockI.h> +#include <Clock.h> #include <map> using namespace std; using namespace Demo; +class ClockI : public Clock +{ +public: + + virtual void + tick(const string& time, const Ice::Current&) + { + cout << time << endl; + } +}; + class Subscriber : public Ice::Application { public: @@ -45,106 +55,61 @@ Subscriber::run(int argc, char* argv[]) return EXIT_FAILURE; } - Ice::ObjectPrx base = communicator()->stringToProxy(proxy); - IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::checkedCast(base); + IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::checkedCast( + communicator()->stringToProxy(proxy)); if(!manager) { cerr << appName() << ": invalid proxy" << endl; return EXIT_FAILURE; } - // - // Gather the set of topics to which to subscribe. It is either - // the set provided on the command line, or the topic "time". - // - Ice::StringSeq topics; - if(argc > 1) + string topicName = "time"; + if(argc != 1) { - for(int i = 1; i < argc; ++i) - { - topics.push_back(argv[i]); - } + topicName = argv[1]; } - else + + IceStorm::TopicPrx topic; + try { - topics.push_back("time"); + topic = manager->retrieve(topicName); } - - // - // Set the requested quality of service "reliability" = - // "batch". This tells IceStorm to send events to the subscriber - // in batches at regular intervals. - // - IceStorm::QoS qos; - //qos["reliability"] = "batch"; - - // - // Create the servant to receive the events. - // - Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Clock.Subscriber"); - Ice::ObjectPtr clock = new ClockI(); - - // - // List of all subscribers. - // - map<string, Ice::ObjectPrx> subscribers; - - // - // Add the servant to the adapter for each topic. A ServantLocator - // could have been used for the same purpose. - // - for(Ice::StringSeq::iterator p = topics.begin(); p != topics.end(); ++p) + catch(const IceStorm::NoSuchTopic& e) { - // - // Add a Servant for the Ice Object. - // - Ice::ObjectPrx object = adapter->addWithUUID(clock); try { - IceStorm::TopicPrx topic = manager->retrieve(*p); - topic->subscribe(qos, object); + topic = manager->create(topicName); } - catch(const IceStorm::NoSuchTopic& e) + catch(const IceStorm::TopicExists& e) { - cerr << appName() << ": " << e << " name: " << e.name << endl; - break; + cerr << appName() << ": temporary failure. try again." << endl; + return EXIT_FAILURE; } - - // - // Add to the set of subscribers _after_ subscribing. This - // ensures that only subscribed subscribers are unsubscribed - // in the case of an error. - // - subscribers[*p] = object; } + Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Clock.Subscriber"); + // - // Unless there is a subscriber per topic then there was some - // problem. If there was an error the application should terminate - // without accepting any events. + // Add a Servant for the Ice Object. // - if(subscribers.size() == topics.size()) - { - adapter->activate(); - shutdownOnInterrupt(); - communicator()->waitForShutdown(); - } + Ice::ObjectPrx subscriber = adapter->addWithUUID(new ClockI); + + // + // This demo requires no quality of service, so it will use + // the defaults. + // + IceStorm::QoS qos; + + topic->subscribe(qos, subscriber); + adapter->activate(); + + shutdownOnInterrupt(); + communicator()->waitForShutdown(); // // Unsubscribe all subscribed objects. // - for(map<string,Ice::ObjectPrx>::const_iterator q = subscribers.begin(); q != subscribers.end(); ++q) - { - try - { - IceStorm::TopicPrx topic = manager->retrieve(q->first); - topic->unsubscribe(q->second); - } - catch(const IceStorm::NoSuchTopic& e) - { - cerr << appName() << ": " << e << " name: " << e.name << endl; - } - } + topic->unsubscribe(subscriber); return EXIT_SUCCESS; } |