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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
const Ice = require("../Ice/ModuleRegistry").Ice;
Ice._ModuleRegistry.require(module,
[
"../Ice/Debug",
"../Ice/Promise",
"../Ice/Protocol",
"../Ice/Locator",
"../Ice/LocalException",
"../Ice/Exception",
"../Ice/HashMap",
"../Ice/IdentityUtil"
]);
const Debug = Ice.Debug;
const Protocol = Ice.Protocol;
const LocatorRegistryPrx = Ice.LocatorRegisterPrx;
const HashMap = Ice.HashMap;
class LocatorInfo
{
constructor(locator, table, background)
{
this._locator = locator;
this._locatorRegistry = null;
this._table = table;
this._background = background;
this._adapterRequests = new Map(); // Map<String, Request>
this._objectRequests = new HashMap(HashMap.compareEquals); // Map<Ice.Identity, Request>
}
destroy()
{
this._locatorRegistry = null;
this._table.clear();
}
equals(rhs)
{
if(this === rhs)
{
return true;
}
if(rhs instanceof LocatorInfo)
{
return this._locator.equals(rhs._locator);
}
return false;
}
hashCode()
{
return this._locator.hashCode();
}
getLocator()
{
return this._locator;
}
getLocatorRegistry()
{
if(this._locatorRegistry !== null)
{
return Ice.Promise.resolve(this._locatorRegistry);
}
return this._locator.getRegistry().then(reg =>
{
//
// The locator registry can't be located. We use ordered
// endpoint selection in case the locator returned a proxy
// with some endpoints which are prefered to be tried first.
//
this._locatorRegistry = LocatorRegistryPrx.uncheckedCast(reg.ice_locator(null).ice_endpointSelection(
Ice.EndpointSelectionType.Ordered));
return this._locatorRegistry;
});
}
getEndpoints(ref, wellKnownRef, ttl, p)
{
const promise = p || new Ice.Promise(); // success callback receives (endpoints, cached)
Debug.assert(ref.isIndirect());
let endpoints = null;
const cached = {value: false};
if(!ref.isWellKnown())
{
endpoints = this._table.getAdapterEndpoints(ref.getAdapterId(), ttl, cached);
if(!cached.value)
{
if(this._background && endpoints !== null)
{
this.getAdapterRequest(ref).addCallback(ref, wellKnownRef, ttl, null);
}
else
{
this.getAdapterRequest(ref).addCallback(ref, wellKnownRef, ttl, promise);
return promise;
}
}
}
else
{
const r = this._table.getObjectReference(ref.getIdentity(), ttl, cached);
if(!cached.value)
{
if(this._background && r !== null)
{
this.getObjectRequest(ref).addCallback(ref, null, ttl, null);
}
else
{
this.getObjectRequest(ref).addCallback(ref, null, ttl, promise);
return promise;
}
}
if(!r.isIndirect())
{
endpoints = r.getEndpoints();
}
else if(!r.isWellKnown())
{
if(ref.getInstance().traceLevels().location >= 1)
{
this.traceWellKnown("found adapter for well-known object in locator cache", ref, r);
}
this.getEndpoints(r, ref, ttl, promise);
return promise;
}
}
Debug.assert(endpoints !== null);
if(ref.getInstance().traceLevels().location >= 1)
{
this.getEndpointsTrace(ref, endpoints, true);
}
promise.resolve([endpoints, true]);
return promise;
}
clearCache(ref)
{
Debug.assert(ref.isIndirect());
if(!ref.isWellKnown())
{
const endpoints = this._table.removeAdapterEndpoints(ref.getAdapterId());
if(endpoints !== null && ref.getInstance().traceLevels().location >= 2)
{
this.trace("removed endpoints for adapter from locator cache", ref, endpoints);
}
}
else
{
const r = this._table.removeObjectReference(ref.getIdentity());
if(r !== null)
{
if(!r.isIndirect())
{
if(ref.getInstance().traceLevels().location >= 2)
{
this.trace("removed endpoints for well-known object from locator cache", ref, r.getEndpoints());
}
}
else if(!r.isWellKnown())
{
if(ref.getInstance().traceLevels().location >= 2)
{
this.traceWellKnown("removed adapter for well-known object from locator cache", ref, r);
}
this.clearCache(r);
}
}
}
}
trace(msg, ref, endpoints)
{
Debug.assert(ref.isIndirect());
const s = [];
s.push(msg);
s.push("\n");
if(!ref.isWellKnown())
{
s.push("adapter = ");
s.push(ref.getAdapterId());
s.push("\n");
}
else
{
s.push("well-known proxy = ");
s.push(ref.toString());
s.push("\n");
}
s.push("endpoints = ");
s.push(endpoints.map(e => e.toString()).join(":"));
ref.getInstance().initializationData().logger.trace(ref.getInstance().traceLevels().locationCat, s.join(""));
}
traceWellKnown(msg, ref, resolved)
{
Debug.assert(ref.isWellKnown());
const s = [];
s.push(msg);
s.push("\n");
s.push("well-known proxy = ");
s.push(ref.toString());
s.push("\n");
s.push("adapter = ");
s.push(resolved.getAdapterId());
ref.getInstance().initializationData().logger.trace(ref.getInstance().traceLevels().locationCat, s.join(""));
}
getEndpointsException(ref, exc)
{
Debug.assert(ref.isIndirect());
const instance = ref.getInstance();
try
{
throw exc;
}
catch(ex)
{
if(ex instanceof Ice.AdapterNotFoundException)
{
if(instance.traceLevels().location >= 1)
{
const s = [];
s.push("adapter not found\n");
s.push("adapter = ");
s.push(ref.getAdapterId());
instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.join(""));
}
const e = new Ice.NotRegisteredException();
e.kindOfObject = "object adapter";
e.id = ref.getAdapterId();
throw e;
}
else if(ex instanceof Ice.ObjectNotFoundException)
{
if(instance.traceLevels().location >= 1)
{
const s = [];
s.push("object not found\n");
s.push("object = ");
s.push(Ice.identityToString(ref.getIdentity(), instance.toStringMode()));
instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.join(""));
}
const e = new Ice.NotRegisteredException();
e.kindOfObject = "object";
e.id = Ice.identityToString(ref.getIdentity(), instance.toStringMode());
throw e;
}
else if(ex instanceof Ice.NotRegisteredException)
{
throw ex;
}
else if(ex instanceof Ice.LocalException)
{
if(instance.traceLevels().location >= 1)
{
const s = [];
s.push("couldn't contact the locator to retrieve endpoints\n");
if(ref.getAdapterId().length > 0)
{
s.push("adapter = ");
s.push(ref.getAdapterId());
s.push("\n");
}
else
{
s.push("well-known proxy = ");
s.push(ref.toString());
s.push("\n");
}
s.push("reason = " + ex.toString());
instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.join(""));
}
throw ex;
}
else
{
Debug.assert(false);
}
}
}
getEndpointsTrace(ref, endpoints, cached)
{
if(endpoints !== null && endpoints.length > 0)
{
if(cached)
{
if(ref.isWellKnown())
{
this.trace("found endpoints for well-known proxy in locator cache", ref, endpoints);
}
else
{
this.trace("found endpoints for adapter in locator cache", ref, endpoints);
}
}
else if(ref.isWellKnown())
{
this.trace("retrieved endpoints for well-known proxy from locator, adding to locator cache",
ref, endpoints);
}
else
{
this.trace("retrieved endpoints for adapter from locator, adding to locator cache",
ref, endpoints);
}
}
else
{
const instance = ref.getInstance();
const s = [];
s.push("no endpoints configured for ");
if(ref.getAdapterId().length > 0)
{
s.push("adapter\n");
s.push("adapter = ");
s.push(ref.getAdapterId());
s.push("\n");
}
else
{
s.push("well-known object\n");
s.push("well-known proxy = ");
s.push(ref.toString());
s.push("\n");
}
instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.join(""));
}
}
getAdapterRequest(ref)
{
if(ref.getInstance().traceLevels().location >= 1)
{
const instance = ref.getInstance();
const s = [];
s.push("searching for adapter by id\n");
s.push("adapter = ");
s.push(ref.getAdapterId());
instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.join(""));
}
let request = this._adapterRequests.get(ref.getAdapterId());
if(request !== undefined)
{
return request;
}
request = new AdapterRequest(this, ref);
this._adapterRequests.set(ref.getAdapterId(), request);
return request;
}
getObjectRequest(ref)
{
if(ref.getInstance().traceLevels().location >= 1)
{
const instance = ref.getInstance();
const s = [];
s.push("searching for well-known object\n");
s.push("well-known proxy = ");
s.push(ref.toString());
instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.join(""));
}
let request = this._objectRequests.get(ref.getIdentity());
if(request !== undefined)
{
return request;
}
request = new ObjectRequest(this, ref);
this._objectRequests.set(ref.getIdentity(), request);
return request;
}
finishRequest(ref, wellKnownRefs, proxy, notRegistered)
{
if(proxy === null || proxy._getReference().isIndirect())
{
//
// Remove the cached references of well-known objects for which we tried
// to resolved the endpoints if these endpoints are empty.
//
for(let i = 0; i < wellKnownRefs.length; ++i)
{
this._table.removeObjectReference(wellKnownRefs[i].getIdentity());
}
}
if(!ref.isWellKnown())
{
if(proxy !== null && !proxy._getReference().isIndirect())
{
// Cache the adapter endpoints.
this._table.addAdapterEndpoints(ref.getAdapterId(), proxy._getReference().getEndpoints());
}
else if(notRegistered) // If the adapter isn't registered anymore, remove it from the cache.
{
this._table.removeAdapterEndpoints(ref.getAdapterId());
}
Debug.assert(this._adapterRequests.has(ref.getAdapterId()));
this._adapterRequests.delete(ref.getAdapterId());
}
else
{
if(proxy !== null && !proxy._getReference().isWellKnown())
{
// Cache the well-known object reference.
this._table.addObjectReference(ref.getIdentity(), proxy._getReference());
}
else if(notRegistered) // If the well-known object isn't registered anymore, remove it from the cache.
{
this._table.removeObjectReference(ref.getIdentity());
}
Debug.assert(this._objectRequests.has(ref.getIdentity()));
this._objectRequests.delete(ref.getIdentity());
}
}
}
Ice.LocatorInfo = LocatorInfo;
class RequestCallback
{
constructor(ref, ttl, promise)
{
this._ref = ref;
this._ttl = ttl;
this._promise = promise;
}
response(locatorInfo, proxy)
{
let endpoints = null;
if(proxy !== null)
{
const r = proxy._getReference();
if(this._ref.isWellKnown() && !Protocol.isSupported(this._ref.getEncoding(), r.getEncoding()))
{
//
// If a well-known proxy and the returned proxy
// encoding isn't supported, we're done: there's
// no compatible endpoint we can use.
//
}
else if(!r.isIndirect())
{
endpoints = r.getEndpoints();
}
else if(this._ref.isWellKnown() && !r.isWellKnown())
{
//
// We're resolving the endpoints of a well-known object and the proxy returned
// by the locator is an indirect proxy. We now need to resolve the endpoints
// of this indirect proxy.
//
if(this._ref.getInstance().traceLevels().location >= 1)
{
locatorInfo.traceWellKnown("retrieved adapter for well-known object from locator, " +
"adding to locator cache", this._ref, r);
}
locatorInfo.getEndpoints(r, this._ref, this._ttl).then(
values =>
{
if(this._promise !== null)
{
this._promise.resolve(values);
}
},
ex =>
{
if(this._promise !== null)
{
this._promise.reject(ex);
}
});
return;
}
}
if(this._ref.getInstance().traceLevels().location >= 1)
{
locatorInfo.getEndpointsTrace(this._ref, endpoints, false);
}
if(this._promise !== null)
{
this._promise.resolve(endpoints === null ? [[], false] : [endpoints, false]);
}
}
exception(locatorInfo, exc)
{
try
{
locatorInfo.getEndpointsException(this._ref, exc); // This throws.
}
catch(ex)
{
if(this._promise !== null)
{
this._promise.reject(ex);
}
}
}
}
class Request
{
constructor(locatorInfo, ref)
{
this._locatorInfo = locatorInfo;
this._ref = ref;
this._callbacks = []; // Array<RequestCallback>
this._wellKnownRefs = []; // Array<Reference>
this._sent = false;
this._response = false;
this._proxy = null;
this._exception = null;
}
addCallback(ref, wellKnownRef, ttl, promise)
{
const callback = new RequestCallback(ref, ttl, promise);
if(this._response)
{
callback.response(this._locatorInfo, this._proxy);
}
else if(this._exception !== null)
{
callback.exception(this._locatorInfo, this._exception);
}
else
{
this._callbacks.push(callback);
if(wellKnownRef !== null) // This request is to resolve the endpoints of a cached well-known object ref
{
this._wellKnownRefs.push(wellKnownRef);
}
if(!this._sent)
{
this._sent = true;
this.send();
}
}
}
response(proxy)
{
this._locatorInfo.finishRequest(this._ref, this._wellKnownRefs, proxy, false);
this._response = true;
this._proxy = proxy;
for(let i = 0; i < this._callbacks.length; ++i)
{
this._callbacks[i].response(this._locatorInfo, proxy);
}
}
exception(ex)
{
this._locatorInfo.finishRequest(this._ref, this._wellKnownRefs, null, ex instanceof Ice.UserException);
this._exception = ex;
for(let i = 0; i < this._callbacks.length; ++i)
{
this._callbacks[i].exception(this._locatorInfo, ex);
}
}
}
class ObjectRequest extends Request
{
constructor(locatorInfo, reference)
{
super(locatorInfo, reference);
Debug.assert(reference.isWellKnown());
}
send()
{
try
{
this._locatorInfo.getLocator().findObjectById(this._ref.getIdentity()).then(
proxy => this.response(proxy),
ex => this.exception(ex));
}
catch(ex)
{
this.exception(ex);
}
}
}
class AdapterRequest extends Request
{
constructor(locatorInfo, reference)
{
super(locatorInfo, reference);
Debug.assert(reference.isIndirect());
}
send()
{
try
{
this._locatorInfo.getLocator().findAdapterById(this._ref.getAdapterId()).then(
proxy => this.response(proxy),
ex => this.exception(ex));
}
catch(ex)
{
this.exception(ex);
}
}
}
module.exports.Ice = Ice;
|