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
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
const Ice = require("../Ice/ModuleRegistry").Ice;
Ice._ModuleRegistry.require(module,
[
"../Ice/Debug",
"../Ice/HashMap",
"../Ice/Promise",
"../Ice/LocalException",
"../Ice/Exception"
]);
const Debug = Ice.Debug;
const HashMap = Ice.HashMap;
class RouterInfo
{
constructor(router)
{
this._router = router;
Debug.assert(this._router !== null);
this._clientEndpoints = null;
this._adapter = null;
this._identities = new HashMap(HashMap.compareEquals); // Set<Identity> = Map<Identity, 1>
this._evictedIdentities = [];
this._hasRoutingTable = false;
}
destroy()
{
this._clientEndpoints = [];
this._adapter = null;
this._identities.clear();
}
equals(rhs)
{
if(this === rhs)
{
return true;
}
if(rhs instanceof RouterInfo)
{
return this._router.equals(rhs._router);
}
return false;
}
hashCode()
{
return this._router.hashCode();
}
getRouter()
{
//
// No mutex lock necessary, _router is immutable.
//
return this._router;
}
getClientEndpoints()
{
const promise = new Ice.Promise();
if(this._clientEndpoints !== null)
{
promise.resolve(this._clientEndpoints);
}
else
{
this._router.getClientProxy().then(result =>
this.setClientEndpoints(result[0],
result[1] !== undefined ? result[1] : true,
promise)).catch(promise.reject);
}
return promise;
}
getServerEndpoints()
{
return this._router.getServerProxy().then(serverProxy => {
if(serverProxy === null)
{
throw new Ice.NoEndpointException();
}
serverProxy = serverProxy.ice_router(null); // The server proxy cannot be routed.
return serverProxy._getReference().getEndpoints();
});
}
addProxy(proxy)
{
Debug.assert(proxy !== null);
if(!this._hasRoutingTable)
{
return Ice.Promise.resolve(); // The router implementation doesn't maintain a routing table.
}
else if(this._identities.has(proxy.ice_getIdentity()))
{
//
// Only add the proxy to the router if it's not already in our local map.
//
return Ice.Promise.resolve();
}
else
{
return this._router.addProxies([proxy]).then(
evictedProxies =>
{
this.addAndEvictProxies(proxy, evictedProxies);
});
}
}
setAdapter(adapter)
{
this._adapter = adapter;
}
getAdapter()
{
return this._adapter;
}
clearCache(ref)
{
this._identities.delete(ref.getIdentity());
}
setClientEndpoints(clientProxy, hasRoutingTable, promise)
{
if(this._clientEndpoints === null)
{
this._hasRoutingTable = hasRoutingTable;
if(clientProxy === null)
{
//
// If getClientProxy() return nil, use router endpoints.
//
this._clientEndpoints = this._router._getReference().getEndpoints();
promise.resolve(this._clientEndpoints);
}
else
{
clientProxy = clientProxy.ice_router(null); // The client proxy cannot be routed.
//
// In order to avoid creating a new connection to the
// router, we must use the same timeout as the already
// existing connection.
//
this._router.ice_getConnection().then(
con =>
{
this._clientEndpoints = clientProxy.ice_timeout(con.timeout())._getReference().getEndpoints();
promise.resolve(this._clientEndpoints);
}).catch(promise.reject);
}
}
else
{
promise.resolve(this._clientEndpoints);
}
}
addAndEvictProxies(proxy, evictedProxies)
{
//
// Check if the proxy hasn't already been evicted by a
// concurrent addProxies call. If it's the case, don't
// add it to our local map.
//
const index = this._evictedIdentities.findIndex(e => e.equals(proxy.ice_getIdentity()));
if(index >= 0)
{
this._evictedIdentities.splice(index, 1);
}
else
{
//
// If we successfully added the proxy to the router,
// we add it to our local map.
//
this._identities.set(proxy.ice_getIdentity(), 1);
}
//
// We also must remove whatever proxies the router evicted.
//
evictedProxies.forEach(proxy =>
{
this._identities.delete(proxy.ice_getIdentity());
});
}
}
Ice.RouterInfo = RouterInfo;
module.exports.Ice = Ice;
|