summaryrefslogtreecommitdiff
path: root/cs/demo/IceStorm/clock/Subscriber.cs
blob: 3c4a13017f8332c33867366d17005fda72a72b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

using System;
using System.Collections;
using Demo;

public class Subscriber : Ice.Application
{
    public override int run(string[] args)
    {
        Ice.Properties properties = communicator().getProperties();

        const string proxyProperty = "IceStorm.TopicManager.Proxy";
        string proxy = properties.getProperty(proxyProperty);
        if(proxy == null)
        {
            Console.WriteLine("property `" + proxyProperty + "' not set");
            return 1;
        }

        Ice.ObjectPrx basePrx = communicator().stringToProxy(proxy);
        IceStorm.TopicManagerPrx manager = IceStorm.TopicManagerPrxHelper.checkedCast(basePrx);
        if(manager == null)
        {
            Console.WriteLine("invalid proxy");
            return 1;
        }

        //
        // Gather the set of topics to which to subscribe. It is either
        // the set provided on the command line, or the topic "time".
        //
        ArrayList topics = new ArrayList();;
        if(args.Length > 1)
        {
            for(int i = 0; i < args.Length; ++i)
            {
                topics.Add(args[i]);
            }
        }
        else
        {
            topics.Add("time");
        }

        //
        // 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 = new IceStorm.QoS();
        qos["reliability"] = "batch";

        //
        // Create the servant to receive the events.
        //
        Ice.ObjectAdapter adapter = communicator().createObjectAdapter("Clock.Subscriber");
        Ice.Object clock = new ClockI();

        //
        // List of all subscribers.
        //
	Hashtable subscribers = new Hashtable();

        //
        // Add the servant to the adapter for each topic. A ServantLocator
        // could have been used for the same purpose.
        //
	for(int i = 0; i < topics.Count; ++i)
        {
            //
            // Add a Servant for the Ice Object.
            //
            Ice.ObjectPrx obj = adapter.addWithUUID(clock);
            try
            {
                IceStorm.TopicPrx topic = manager.retrieve((string)topics[i]);
                topic.subscribe(qos, obj);
            }
            catch(IceStorm.NoSuchTopic e)
            {                               
                Console.WriteLine(e + " name: " + e.name);
            }

            //
            // Add to the set of subscribers _after_ subscribing. This
            // ensures that only subscribed subscribers are unsubscribed
            // in the case of an error.
            //
            subscribers[(string)topics[i]] = obj;
        }

        //
        // 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.
        //
        if(subscribers.Count == topics.Count)
        {
            adapter.activate();
            shutdownOnInterrupt();
            communicator().waitForShutdown();
        }

        //
        // Unsubscribe all subscribed objects.
        //
	foreach(DictionaryEntry entry in (Hashtable)subscribers)
        {
            try
            {
                IceStorm.TopicPrx topic = manager.retrieve((string)entry.Key);
                topic.unsubscribe((Ice.ObjectPrx)entry.Value);
            }
            catch(IceStorm.NoSuchTopic e)
            {
                Console.WriteLine(e + " name: " + e.name);
            }
        }

        return 0;
    }

    public static void Main(string[] args)
    {
        Subscriber app = new Subscriber();
        int status = app.main(args, "config");
        System.Environment.Exit(status);
    }
}