summaryrefslogtreecommitdiff
path: root/java/demo/IceStorm/clock/Publisher.java
blob: e0faa91d583d965bf9b48265eb83ad47cd8adad8 (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
// **********************************************************************
//
// 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.
//
// **********************************************************************

import Demo.*;

public class Publisher extends Ice.Application
{
    public int
    run(String[] args)
    {
        IceStorm.TopicManagerPrx manager = IceStorm.TopicManagerPrxHelper.checkedCast(
	    communicator().propertyToProxy("IceStorm.TopicManager.Proxy"));
        if(manager == null)
        {
            System.err.println("invalid proxy");
            return 1;
        }

	String topicName = "time";
	if(args.length != 0)
	{
	    topicName = args[0];
	}

	//
	// Retrieve the topic named "time".
	//
	IceStorm.TopicPrx topic;
	try
	{
	    topic = manager.retrieve(topicName);
	}
	catch(IceStorm.NoSuchTopic e)
	{
	    try
	    {
	        topic = manager.create(topicName);
	    }
	    catch(IceStorm.TopicExists ex)
	    {
	        System.err.println("temporary failure, try again.");
	        return 1;
	    }
	}

	//
	// Get the topic's publisher object, the Clock type, and create a
	// oneway Clock proxy (for efficiency reasons).
	//
	ClockPrx clock = ClockPrxHelper.uncheckedCast(topic.getPublisher().ice_oneway());

        System.out.println("publishing tick events. Press ^C to terminate the application.");
	try
	{
	    java.text.SimpleDateFormat date = new java.text.SimpleDateFormat("MM/dd/yy HH:mm:ss:SSS");
	    while(true)
	    {
	        
                clock.tick(date.format(new java.util.Date()));

		try
		{
		    Thread.currentThread().sleep(1000);
		}
		catch(java.lang.InterruptedException e)
		{
		}
	    }
	}
	catch(Ice.CommunicatorDestroyedException ex)
	{
	    // Ignore
	}

        return 0;
    }

    public static void
    main(String[] args)
    {
        Publisher app = new Publisher();
        int status = app.main("Publisher", args, "config.pub");
        System.exit(status);
    }
}