summaryrefslogtreecommitdiff
path: root/js/src/Ice/TcpEndpointI.js
blob: f344b9212a5a1306eb084012c277e45a988a03ed (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
274
275
276
277
278
279
280
281
// **********************************************************************
//
// Copyright (c) 2003-2015 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("../Ice/ModuleRegistry").Ice;
Ice.__M.require(module,
    [
        "../Ice/Class",
        "../Ice/Debug",
        "../Ice/HashUtil",
        "../Ice/StringUtil",
        "../Ice/IPEndpointI",
        "../Ice/TcpTransceiver",
        "../Ice/LocalException",
        "../Ice/EndpointInfo"
    ]);

var IceSSL = Ice.__M.require(module, ["../Ice/EndpointInfo"]).IceSSL;

var Debug = Ice.Debug;
var HashUtil = Ice.HashUtil;
var StringUtil = Ice.StringUtil;
var TcpTransceiver = typeof(Ice.TcpTransceiver) !== "undefined" ? Ice.TcpTransceiver : null;
var Class = Ice.Class;

var TcpEndpointI = Class(Ice.IPEndpointI, {
    __init__: function(instance, ho, po, sif, ti, conId, co)
    {
        Ice.IPEndpointI.call(this, instance, ho, po, sif, conId);
        this._timeout = ti === undefined ? (instance ? instance.defaultTimeout() : undefined) : ti;
        this._compress = co === undefined ? false : co;
    },
    //
    // Return the endpoint information.
    //
    getInfo: function()
    {
        var info = this.secure() ? new IceSSL.EndpointInfo() : new Ice.TCPEndpointInfo();
        this.fillEndpointInfo(info);
        return info;
    },
    //
    // Return the timeout for the endpoint in milliseconds. 0 means
    // non-blocking, -1 means no timeout.
    //
    timeout: function()
    {
        return this._timeout;
    },
    //
    // Return a new endpoint with a different timeout value, provided
    // that timeouts are supported by the endpoint. Otherwise the same
    // endpoint is returned.
    //
    changeTimeout: function(timeout)
    {
        if(timeout === this._timeout)
        {
            return this;
        }
        else
        {
            return new TcpEndpointI(this._instance, this._host, this._port, this._sourceAddr, timeout,
                                    this._connectionId, this._compress);
        }
    },
    //
    // Return a new endpoint with a different connection id.
    //
    changeConnectionId: function(connectionId)
    {
        if(connectionId === this._connectionId)
        {
            return this;
        }
        else
        {
            return new TcpEndpointI(this._instance, this._host, this._port, this._sourceAddr, this._timeout,
                                    connectionId, this._compress);
        }
    },
    //
    // Return true if the endpoints support bzip2 compress, or false
    // otherwise.
    //
    compress: function()
    {
        return this._compress;
    },
    //
    // Return a new endpoint with a different compression value,
    // provided that compression is supported by the
    // endpoint. Otherwise the same endpoint is returned.
    //
    changeCompress: function(compress)
    {
        if(compress === this._compress)
        {
            return this;
        }
        else
        {
            return new TcpEndpointI(this._instance, this._host, this._port, this._sourceAddr, this._timeout,
                                    this._connectionId, compress);
        }
    },
    //
    // Return true if the endpoint is datagram-based.
    //
    datagram: function()
    {
        return false;
    },
    connectable: function()
    {
        //
        // TCP endpoints are not connectable when running in a browser, SSL
        // isn't currently supported.
        //
        return TcpTransceiver !== null && !this.secure();
    },
    connect: function()
    {
        Debug.assert(!this.secure());
        return TcpTransceiver.createOutgoing(this._instance, this.getAddress(), this._sourceAddr);
    },
    //
    // Convert the endpoint to its string form
    //
    options: function()
    {
        //
        // WARNING: Certain features, such as proxy validation in Glacier2,
        // depend on the format of proxy strings. Changes to toString() and
        // methods called to generate parts of the reference string could break
        // these features. Please review for all features that depend on the
        // format of proxyToString() before changing this and related code.
        //
        var s = Ice.IPEndpointI.prototype.options.call(this);
        if(this._timeout == -1)
        {
            s += " -t infinite";
        }
        else
        {
            s += " -t " + this._timeout;
        }

        if(this._compress)
        {
            s += " -z";
        }
        return s;
    },
    compareTo: function(p)
    {
        if(this === p)
        {
            return 0;
        }

        if(p === null)
        {
            return 1;
        }

        if(!(p instanceof TcpEndpointI))
        {
            return this.type() < p.type() ? -1 : 1;
        }

        if(this._timeout < p._timeout)
        {
            return -1;
        }
        else if(p._timeout < this._timeout)
        {
            return 1;
        }

        if(!this._compress && p._compress)
        {
            return -1;
        }
        else if(!p._compress && this._compress)
        {
            return 1;
        }

        return Ice.IPEndpointI.prototype.compareTo.call(this, p);
    },
    streamWriteImpl: function(s)
    {
        Ice.IPEndpointI.prototype.streamWriteImpl.call(this, s);
        s.writeInt(this._timeout);
        s.writeBool(this._compress);
    },
    hashInit: function(h)
    {
        h = Ice.IPEndpointI.prototype.hashInit.call(this, h);
        h = HashUtil.addNumber(h, this._timeout);
        h = HashUtil.addBoolean(h, this._compress);
        return h;
    },
    fillEndpointInfo: function(info)
    {
        Ice.IPEndpointI.prototype.fillEndpointInfo.call(this, info);
        info.timeout = this._timeout;
        info.compress = this._compress;
    },
    initWithStream: function(s)
    {
        Ice.IPEndpointI.prototype.initWithStream.call(this, s);
        this._timeout = s.readInt();
        this._compress = s.readBool();
    },
    checkOption: function(option, argument, endpoint)
    {
        if(Ice.IPEndpointI.prototype.checkOption.call(this, option, argument, endpoint))
        {
            return true;
        }

        if(option === "-t")
        {
            if(argument === null)
            {
                throw new Ice.EndpointParseException("no argument provided for -t option in endpoint " + endpoint);
            }

            if(argument == "infinite")
            {
                this._timeout = -1;
            }
            else
            {
                var invalid = false;
                try
                {
                    this._timeout = StringUtil.toInt(argument);
                }
                catch(ex)
                {
                    invalid = true;
                }
                if(invalid || this._timeout < 1)
                {
                    throw new Ice.EndpointParseException("invalid timeout value `" + argument + "' in endpoint " +
                                                         endpoint);
                }
            }
        }
        else if(option === "-z")
        {
            if(argument !== null)
            {
                throw new Ice.EndpointParseException("unexpected argument `" + argument +
                                                     "' provided for -z option in " + endpoint);
            }

            this._compress = true;
        }
        else
        {
            return false;
        }
        return true;
    },
    createEndpoint: function(host, port, conId)
    {
        return new TcpEndpointI(this._instance, host, port, this._sourceAddr, this._timeout, conId, this._compress);
    }
});

Ice.TcpEndpointI = TcpEndpointI;
module.exports.Ice = Ice;