summaryrefslogtreecommitdiff
path: root/py/demo/IceStorm/clock/Publisher.py
blob: 131476c52e219bb2b8302b1160f140c1174a50ac (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
#!/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, time, Ice, IceStorm, getopt

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

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

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

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

        if optsSet > 1:
            self.usage()
            return 1

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

        manager = IceStorm.TopicManagerPrx.checkedCast(self.communicator().propertyToProxy('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

        #
        # Get the topic's publisher object, and create a Clock proxy with
        # the mode specified as an argument of this application.
        #
        publisher = topic.getPublisher();
        if datagram:
            publisher = publisher.ice_datagram();
        elif twoway:
            # Do nothing.
            pass
        else: # if(oneway)
            publisher = publisher.ice_oneway();
        clock = Demo.ClockPrx.uncheckedCast(publisher)

        print "publishing tick events. Press ^C to terminate the application."
        try:
            while 1:
                clock.tick(time.strftime("%m/%d/%Y %H:%M:%S"))
                time.sleep(1)
        except IOError, e:
            # Ignore
            pass
        except Ice.CommunicatorDestroyedException, e:
            # Ignore
            pass
                
        return 0

app = Publisher()
sys.exit(app.main(sys.argv, "config.pub"))