summaryrefslogtreecommitdiff
path: root/js/demo/Ice/minimal/browser/Client.js
blob: 5bb07b816d83020675e8e479c632744f8d6b7586 (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
// **********************************************************************
//
// 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(){

//
// Initialize the communicator.
//
var communicator = Ice.initialize();

function sayHello()
{
    Ice.Promise.try(
        function()
        {
            setState(State.Busy);
            
            //
            // Create a proxy for the hello object
            //
            var hostname = document.location.hostname || "127.0.0.1";
            var proxy = communicator.stringToProxy("hello:ws -h " + hostname + " -p 10000");
            
            //
            // Down-cast this proxy to the derived interface Demo::Hello
            // using checkedCast, and invoke the sayHello operation if 
            // the checkedCast succeeds.
            //
            return Demo.HelloPrx.checkedCast(proxy).then(
                function(hello)
                {
                    return hello.sayHello();
                });
        }
    ).exception(
        function(ex)
        {
            //
            // Handle any exceptions thrown above.
            //
            $("#output").val(ex.toString());
        }
    ).finally(
        function()
        {
            setState(State.Idle);
        }
    );
    return false;
}

//
// Handle the client state
//
var State = {
    Idle: 0,
    Busy: 1
};

var state;

function setState(newState)
{
    function assert(v)
    {
        if(!v)
        {
            throw new Error("Assertion failed");
        }
    }

    assert(state !== newState);
    switch(newState)
    {
        case State.Idle:
        {
            assert(state === undefined || state == State.Busy);

            //
            // Hide the progress indicator.
            //
            $("#progress").hide();
            $("body").removeClass("waiting");
            
            //
            // Enable buttons
            //
            $("#hello").removeClass("disabled").click(sayHello);
            break;
        }
        case State.Busy:
        {
            assert(state == State.Idle);

            //
            // Clear any previous error messages.
            //
            $("#output").val("");
            
            //
            // Disable buttons.
            //
            $("#hello").addClass("disabled").off("click");
            
            //
            // Display the progress indicator and set the wait cursor.
            //
            $("#progress").show();
            $("body").addClass("waiting");
            break;
        }
    }
    state = newState;
}

//
// Start in the idle state
//
setState(State.Idle);

}());