summaryrefslogtreecommitdiff
path: root/js/demo/Glacier2/chat/Client.js
blob: ae47b85bb764a60bda83c84c5464a42c2698e74c (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// **********************************************************************
//
// 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("Glacier2");
require("./Chat");

//
// Servant that implements the ChatCallback interface,
// the message operation just writes the received data
// to stdout.
//
var ChatCallbackI = Ice.Class(Demo.ChatCallback, {
    message: function(data)
    {
        console.log(data);
    }
});

var communicator;

//
// Destroy communicator on SIGINT so application
// exit cleanly.
//
process.once("SIGINT", function() {
    if(communicator)
    {
        communicator.destroy().finally(
            function()
            {
                process.exit(0);
            });
    }
});

Ice.Promise.try(
    function() 
    {
        //
        // Initialize the communicator with Ice.Default.Router property
        // set to the chat demo Glacier2 router.
        //
        var id = new Ice.InitializationData();
        id.properties = Ice.createProperties();
        id.properties.setProperty("Ice.Default.Router", "DemoGlacier2/router:tcp -p 4063 -h localhost");
        communicator = Ice.initialize(id);
        
        function createSession()
        {
            return Ice.Promise.try(
                function()
                {
                    //
                    // Get a proxy to the default rotuer and down-cast it to Glacier2.Router
                    // interface to ensure Glacier2 server is available.
                    //
                    var router = communicator.getDefaultRouter();
                    var id;

                    return Glacier2.RouterPrx.checkedCast(router).then(
                        function(proxy)
                        {
                            router = proxy;
                            console.log("This demo accepts any user-id / password combination.");
                            process.stdout.write("user id: ");
                            return getline();
                        }
                    ).then(
                        function(str)
                        {
                            id = str;
                            process.stdout.write("password: ");
                            return getline();
                        }
                    ).then(
                        function(password)
                        {
                            return router.createSession(id, password);
                        }
                    ).then(
                        function(session)
                        {
                            return runWithSession(router, Demo.ChatSessionPrx.uncheckedCast(session));
                        },
                        function(ex)
                        {
                            if(ex instanceof Glacier2.PermissionDeniedException)
                            {
                                console.log("permission denied:\n" + ex.reason);
                                return createSession();
                            }
                            else if(ex instanceof Glacier2.CannotCreateSessionException)
                            {
                                console.log("cannot create session:\n" + ex.reason);
                                return createSession();
                            }
                            else
                            {
                                throw ex;
                            }
                        });
                });
        };
        
        function runWithSession(router, session)
        {
            var p = new Ice.Promise();

            //
            // Get the session timeout, the router client category and
            // create the client object adapter.
            //
            // Use Ice.Promise.all to wait for the completion of all the
            // calls.
            //
            Ice.Promise.all(
                router.getSessionTimeout(),
                router.getCategoryForClient(),
                communicator.createObjectAdapterWithRouter("", router)
            ).then(
                function(timeoutA, categoryA, adapterA)
                {
                    var timeout = timeoutA[0];
                    var category = categoryA[0];
                    var adapter = adapterA[0];
                    
                    //
                    // Call refreshSession in a loop to keep the 
                    // session alive.
                    //
                    var refreshSession = function()
                    {
                        router.refreshSession().exception(
                            function(ex)
                            {
                                p.fail(ex);
                            }
                        ).delay(timeout.toNumber() * 500).then(
                            function()
                            {
                                if(!p.completed())
                                {
                                    refreshSession();
                                }
                            });
                    };
                    refreshSession();
                    
                    //
                    // Create the ChatCallback servant and add it to the ObjectAdapter.
                    //
                    var callback = Demo.ChatCallbackPrx.uncheckedCast(
                        adapter.add(new ChatCallbackI(), new Ice.Identity("callback", category)));
                    
                    //
                    // Set the chat session callback.
                    //
                    return session.setCallback(callback);
                }
            ).then(
                function()
                {
                    //
                    // The chat function sequantially reads stdin messages
                    // and send it to server using the session say method.
                    //
                    function chat()
                    {
                        process.stdout.write("==> ");
                        return getline().then(
                            function(msg)
                            {
                                if(msg == "/quit")
                                {
                                    p.succeed();
                                }
                                else if(msg.indexOf("/") == 0)
                                {
                                    console.log("enter /quit to exit.");
                                }
                                else
                                {
                                    return session.say(msg);
                                }
                            }
                        ).then(
                            function()
                            {
                                if(!p.completed())
                                {
                                    return chat();
                                }
                            }
                        ).exception(
                            function(ex)
                            {
                                p.fail(ex);
                            });
                    }
                    
                    //
                    // Start the chat loop
                    //
                    return chat();
                }
            ).finally(
                function()
                {
                    //
                    // Destroy the session.
                    //
                    return router.destroySession();
                }
            ).exception(
                function(ex)
                {
                    p.fail(ex);
                });
            return p;
        }
        return createSession();
    }
).finally(
    function()
    {
        //
        // Destroy the communicator if required.
        //
        if(communicator)
        {
            return communicator.destroy();
        }
    }
).then(
    function()
    {
        process.exit(0);
    },
    function(ex)
    {
        //
        // Handle any exceptions above.
        //
        console.log(ex.toString());
        process.exit(1);
    });

//
// Asynchonously process stdin lines using a promise
//
var getline = function()
{
    var p = new Ice.Promise();
    process.stdin.resume();
    process.stdin.once("data", 
        function(buffer)
        {
            process.stdin.pause();
            p.succeed(buffer.toString("utf-8").trim());
        });
    return p;
};

}());