summaryrefslogtreecommitdiff
path: root/js/demo/Ice/bidir/Client.js
blob: c5075a2d54e1323c1f3ccdf56a535a3dad1cb96d (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
// **********************************************************************
//
// Copyright (c) 2003-2014 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.
//
// **********************************************************************

var Ice = require("icejs").Ice;
var Demo = require("./Callback").Demo;

//
// Define a servant class that implements Demo.CallbackReceiver
// interface.
//
var CallbackReceiverI = Ice.Class(Demo.CallbackReceiver, {
    callback: function(num, current)
    {
        console.log("received callback #" + num);
    }
});

var id = new Ice.InitializationData();
id.properties = Ice.createProperties();

var communicator = Ice.initialize(process.argv, id);

//
// Exit on SIGINT
//
process.on("SIGINT", function() {
    if(communicator)
    {
        communicator.destroy().finally(
            function()
            {
                process.exit(0);
            });
    }
});

Ice.Promise.try(
    function()
    {
        //
        // Initialize the communicator and create a proxy to the sender object.
        //
        var proxy = communicator.stringToProxy("sender:tcp -p 10000");

        //
        // Down-cast the proxy to the Demo.CallbackSender interface.
        //
        return Demo.CallbackSenderPrx.checkedCast(proxy).then(
            function(server)
            {
                //
                // Create the client object adapter.
                //
                return communicator.createObjectAdapter("").then(
                    function(adapter)
                    {
                        //
                        // Create a callback receiver servant and add it to
                        // the object adapter.
                        //
                        var r = adapter.addWithUUID(new CallbackReceiverI());

                        //
                        // Set the connection adapter.
                        //
                        proxy.ice_getCachedConnection().setAdapter(adapter);

                        //
                        // Register the client with the bidir server.
                        //
                        return server.addClient(r.ice_getIdentity());
                    });
            });
    }
).exception(
    function(ex)
    {
        console.log(ex.toString());
        Ice.Promise.try(
            function()
            {
                if(communicator)
                {
                    return communicator.destroy();
                }
            }
        ).finally(
            function()
            {
                process.exit(1);
            });
    });