summaryrefslogtreecommitdiff
path: root/js/demo/Ice/bidir/Client.js
blob: 9dbfaff65ee51e2d48a85c5c0b6dbab15ba55cfd (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
// **********************************************************************
//
// 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.
//
// **********************************************************************

(function(){

require("Ice");
require("./Callback");

var Demo = global.Demo;
var CallbackSenderPrx = Demo.CallbackSenderPrx;

//
// 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();
//
// Client-side ACM must be disabled for bidirectional connections.
//
id.properties.setProperty("Ice.ACM.Client", "0");

var communicator = Ice.initialize(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 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());
        Promise.try(
            function()
            {
                if(communicator)
                {
                    return communicator.destroy();
                }
            }
        ).finally(
            function()
            {
                process.exit(1);
            });
    });

}());