summaryrefslogtreecommitdiff
path: root/cpp/demo/IceStorm/clock/Subscriber.cpp
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2006-11-15 13:31:33 +0000
committerMatthew Newhook <matthew@zeroc.com>2006-11-15 13:31:33 +0000
commit709152f1ce6f82cbadfaea5e8fdbd22be6f8fd4c (patch)
treea09c44e0aa4263fe90a26bed3049551b9cf66363 /cpp/demo/IceStorm/clock/Subscriber.cpp
parentAdded NullPermissionsVerifiers to Glacier2 (diff)
downloadice-709152f1ce6f82cbadfaea5e8fdbd22be6f8fd4c.tar.bz2
ice-709152f1ce6f82cbadfaea5e8fdbd22be6f8fd4c.tar.xz
ice-709152f1ce6f82cbadfaea5e8fdbd22be6f8fd4c.zip
http://bugzilla.zeroc.com/bugzilla/show_bug.cgi?id=1547. Demo cleanup.
Diffstat (limited to 'cpp/demo/IceStorm/clock/Subscriber.cpp')
-rw-r--r--cpp/demo/IceStorm/clock/Subscriber.cpp121
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;
}