summaryrefslogtreecommitdiff
path: root/py/demo/IceStorm/clock/Subscriber.py
blob: e1fd12bc9a614c29eaee6fe187731f7f99c96b8a (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
#!/usr/bin/env python
# **********************************************************************
#
# Copyright (c) 2003-2008 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.
#
# **********************************************************************

import sys, traceback, Ice, IceStorm, getopt

Ice.loadSlice('Clock.ice')
import Demo

class ClockI(Demo.Clock):
    def tick(self, date, current=None):
        print date

class Subscriber(Ice.Application):
    def usage(self):
        print "Usage: " + self.appName() + " [--batch] [--datagram|--twoway|--ordered|--oneway] [topic]"

    def run(self, args):
        try:
            opts, args = getopt.getopt(args[1:], '', ['datagram', 'twoway', 'oneway', 'ordered', 'batch'])
        except getopt.GetoptError:
            self.usage()
            return 1

        topicName = "time"
        datagram = False
        twoway = False
        ordered = False
        batch = False
        optsSet = 0
        for o, a in opts:
            if o == "--datagram":
                datagram = True
                optsSet = optsSet + 1
            elif o =="--twoway":
                twoway = True
                optsSet = optsSet + 1
            elif o =="--ordered":
                ordered = True
                optsSet = optsSet + 1
            elif o =="--oneway":
                optsSet = optsSet + 1
            elif o =="--batch":
                batch = True

        if batch and (twoway or ordered):
            print self.appName() + ": batch can only be set with oneway or datagram"
            return 1

        if optsSet > 1:
            self.usage()
            sys.exit(1)

        if len(args) > 0:
            topicName = args[0]

        manager = IceStorm.TopicManagerPrx.checkedCast(\
            self.communicator().propertyToProxy('IceStorm.TopicManager.Proxy'))
        if not manager:
            print args[0] + ": invalid proxy"
            return 1

        #
        # Retrieve the topic.
        #
        try:
            topic = manager.retrieve(topicName)
        except IceStorm.NoSuchTopic, e:
            try:
                topic = manager.create(topicName)
            except IceStorm.TopicExists, ex:
                print self.appName() + ": temporary error. try again"
                return 1

        adapter = self.communicator().createObjectAdapter("Clock.Subscriber")

        #
        # Add a Servant for the Ice Object.
        #
        qos = {}
        subscriber = adapter.addWithUUID(ClockI())

        #
        # Set up the proxy.
        #
        if datagram:
            subscriber = subscriber.ice_datagram()
        elif twoway:
             pass
            # Do nothing to the subscriber proxy. Its already twoway.
        elif ordered:
            # Do nothing to the subscriber proxy. Its already twoway.
            qos["reliability"] = "ordered"
        else: # if(oneway)
            subscriber = subscriber.ice_oneway()
        if batch:
            if datagram:
                subscriber = subscriber.ice_batchDatagram()
            else:
                subscriber = subscriber.ice_batchOneway()

        topic.subscribeAndGetPublisher(qos, subscriber)
        adapter.activate()

        self.shutdownOnInterrupt()
        self.communicator().waitForShutdown()

        #
        # Unsubscribe all subscribed objects.
        #
        topic.unsubscribe(subscriber)
            
        return 0

app = Subscriber()
sys.exit(app.main(sys.argv, "config.sub"))