summaryrefslogtreecommitdiff
path: root/csharp/src/Ice/LocatorInfo.cs
blob: fb4df8944f8d0b1ca7fa093f72b57d453a70181d (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
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
// **********************************************************************
//
// Copyright (c) 2003-2017 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.
//
// **********************************************************************

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

namespace IceInternal
{
    public sealed class LocatorInfo
    {
        public interface GetEndpointsCallback
        {
            void setEndpoints(EndpointI[] endpoints, bool cached);
            void setException(Ice.LocalException ex);
        }

        private class RequestCallback
        {
            public void
            response(LocatorInfo locatorInfo, Ice.ObjectPrx proxy)
            {
                EndpointI[] endpoints = null;
                if(proxy != null)
                {
                    Reference r = ((Ice.ObjectPrxHelperBase)proxy).iceReference();
                    if(_ref.isWellKnown() && !Protocol.isSupported(_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(_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.
                        //
                        locatorInfo.getEndpoints(r, _ref, _ttl, _callback);
                        return;
                    }
                }

                if(_ref.getInstance().traceLevels().location >= 1)
                {
                    locatorInfo.getEndpointsTrace(_ref, endpoints, false);
                }
                if(_callback != null)
                {
                    _callback.setEndpoints(endpoints == null ? new EndpointI[0] : endpoints, false);
                }
            }

            public void
            exception(LocatorInfo locatorInfo, Ice.Exception exc)
            {
                try
                {
                    locatorInfo.getEndpointsException(_ref, exc); // This throws.
                }
                catch(Ice.LocalException ex)
                {
                    if(_callback != null)
                    {
                        _callback.setException(ex);
                    }
                }
            }

            public
            RequestCallback(Reference @ref, int ttl, GetEndpointsCallback cb)
            {
                _ref = @ref;
                _ttl = ttl;
                _callback = cb;
            }

            readonly Reference _ref;
            readonly int _ttl;
            readonly GetEndpointsCallback _callback;
        }

        private abstract class Request
        {
            public void
            addCallback(Reference @ref, Reference wellKnownRef, int ttl, GetEndpointsCallback cb)
            {
                RequestCallback callback = new RequestCallback(@ref, ttl, cb);
                lock(this)
                {
                    if(!_response && _exception == null)
                    {
                        _callbacks.Add(callback);
                        if(wellKnownRef != null)
                        {
                            // This request is to resolve the endpoints of a cached well-known object ref
                            _wellKnownRefs.Add(wellKnownRef);
                        }
                        if(!_sent)
                        {
                            _sent = true;
                            send();
                        }
                        return;
                    }
                }

                if(_response)
                {
                    callback.response(_locatorInfo, _proxy);
                }
                else
                {
                    Debug.Assert(_exception != null);
                    callback.exception(_locatorInfo, _exception);
                }
            }

            public Request(LocatorInfo locatorInfo, Reference @ref)
            {
                _locatorInfo = locatorInfo;
                _ref = @ref;
                _sent = false;
                _response = false;
            }

            public void
            response(Ice.ObjectPrx proxy)
            {
                lock(this)
                {
                    _locatorInfo.finishRequest(_ref, _wellKnownRefs, proxy, false);
                    _response = true;
                    _proxy = proxy;
                    Monitor.PulseAll(this);
                }
                foreach(RequestCallback callback in _callbacks)
                {
                    callback.response(_locatorInfo, proxy);
                }
            }

            public void
            exception(Ice.Exception ex)
            {
                lock(this)
                {
                    _locatorInfo.finishRequest(_ref, _wellKnownRefs, null, ex is Ice.UserException);
                    _exception = ex;
                    Monitor.PulseAll(this);
                }
                foreach(RequestCallback callback in _callbacks)
                {
                    callback.exception(_locatorInfo, ex);
                }
            }

            protected abstract void send();

            readonly protected LocatorInfo _locatorInfo;
            readonly protected Reference _ref;

            private List<RequestCallback> _callbacks = new List<RequestCallback>();
            private List<Reference> _wellKnownRefs = new List<Reference>();
            private bool _sent;
            private bool _response;
            private Ice.ObjectPrx _proxy;
            private Ice.Exception _exception;
        }

        private class ObjectRequest : Request
        {
            public ObjectRequest(LocatorInfo locatorInfo, Reference @ref) : base(locatorInfo, @ref)
            {
            }

            override protected void
            send()
            {
                try
                {
                    _locatorInfo.getLocator().begin_findObjectById(_ref.getIdentity()).whenCompleted(
                        this.response, this.exception);
                }
                catch(Ice.Exception ex)
                {
                    exception(ex);
                }
            }
        }

        private class AdapterRequest : Request
        {
            public AdapterRequest(LocatorInfo locatorInfo, Reference @ref) : base(locatorInfo, @ref)
            {
            }

            override protected void
            send()
            {
                try
                {
                    _locatorInfo.getLocator().begin_findAdapterById(_ref.getAdapterId()).whenCompleted(
                        response, exception);
                }
                catch(Ice.Exception ex)
                {
                    exception(ex);
                }
            }
        }

        internal LocatorInfo(Ice.LocatorPrx locator, LocatorTable table, bool background)
        {
            _locator = locator;
            _table = table;
            _background = background;
        }

        public void destroy()
        {
            lock(this)
            {
                _locatorRegistry = null;
                _table.clear();
            }
        }

        public override bool Equals(object obj)
        {
            if(ReferenceEquals(this, obj))
            {
                return true;
            }

            LocatorInfo rhs = obj as LocatorInfo;
            return rhs == null ? false : _locator.Equals(rhs._locator);
        }

        public override int GetHashCode()
        {
            return _locator.GetHashCode();
        }

        public Ice.LocatorPrx getLocator()
        {
            //
            // No synchronization necessary, _locator is immutable.
            //
            return _locator;
        }

        public Ice.LocatorRegistryPrx getLocatorRegistry()
        {
            lock(this)
            {
                if(_locatorRegistry != null)
                {
                    return _locatorRegistry;
                }
            }

            //
            // Do not make locator calls from within sync.
            //
            Ice.LocatorRegistryPrx locatorRegistry = _locator.getRegistry();
            if(locatorRegistry == null)
            {
                return null;
            }

            lock(this)
            {
                //
                // 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.
                //
                _locatorRegistry = (Ice.LocatorRegistryPrx)locatorRegistry.ice_locator(null).ice_endpointSelection(
                    Ice.EndpointSelectionType.Ordered);
                return _locatorRegistry;
            }
        }

        public void
        getEndpoints(Reference @ref, int ttl, GetEndpointsCallback callback)
        {
            getEndpoints(@ref, null, ttl, callback);
        }

        public void
        getEndpoints(Reference @ref, Reference wellKnownRef, int ttl, GetEndpointsCallback callback)
        {
            Debug.Assert(@ref.isIndirect());
            EndpointI[] endpoints = null;
            bool cached = false;
            if(!@ref.isWellKnown())
            {
                endpoints = _table.getAdapterEndpoints(@ref.getAdapterId(), ttl, out cached);
                if(!cached)
                {
                    if(_background && endpoints != null)
                    {
                        getAdapterRequest(@ref).addCallback(@ref, wellKnownRef, ttl, null);
                    }
                    else
                    {
                        getAdapterRequest(@ref).addCallback(@ref, wellKnownRef, ttl, callback);
                        return;
                    }
                }
            }
            else
            {
                Reference r = _table.getObjectReference(@ref.getIdentity(), ttl, out cached);
                if(!cached)
                {
                    if(_background && r != null)
                    {
                        getObjectRequest(@ref).addCallback(@ref, null, ttl, null);
                    }
                    else
                    {
                        getObjectRequest(@ref).addCallback(@ref, null, ttl, callback);
                        return;
                    }
                }

                if(!r.isIndirect())
                {
                    endpoints = r.getEndpoints();
                }
                else if(!r.isWellKnown())
                {
                    getEndpoints(r, @ref, ttl, callback);
                    return;
                }
            }

            Debug.Assert(endpoints != null);
            if(@ref.getInstance().traceLevels().location >= 1)
            {
                getEndpointsTrace(@ref, endpoints, true);
            }
            if(callback != null)
            {
                callback.setEndpoints(endpoints, true);
            }
        }

        public void clearCache(Reference rf)
        {
            Debug.Assert(rf.isIndirect());
            if(!rf.isWellKnown())
            {
                EndpointI[] endpoints = _table.removeAdapterEndpoints(rf.getAdapterId());

                if(endpoints != null && rf.getInstance().traceLevels().location >= 2)
                {
                    trace("removed endpoints from locator table\n", rf, endpoints);
                }
            }
            else
            {
                Reference r = _table.removeObjectReference(rf.getIdentity());
                if(r != null)
                {
                    if(!r.isIndirect())
                    {
                        if(rf.getInstance().traceLevels().location >= 2)
                        {
                            trace("removed endpoints from locator table", rf, r.getEndpoints());
                        }
                    }
                    else if(!r.isWellKnown())
                    {
                        clearCache(r);
                    }
                }
            }
        }

        private void trace(string msg, Reference r, EndpointI[] endpoints)
        {
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            s.Append(msg + "\n");
            if(r.getAdapterId().Length > 0)
            {
                s.Append("adapter = " + r.getAdapterId() + "\n");
            }
            else
            {
                s.Append("object = " + Ice.Util.identityToString(r.getIdentity(), r.getInstance().toStringMode())
                         + "\n");
            }

            s.Append("endpoints = ");
            int sz = endpoints.Length;
            for (int i = 0; i < sz; i++)
            {
                s.Append(endpoints[i].ToString());
                if(i + 1 < sz)
                {
                    s.Append(":");
                }
            }

            r.getInstance().initializationData().logger.trace(r.getInstance().traceLevels().locationCat, s.ToString());
        }

        private void getEndpointsException(Reference @ref, System.Exception exc)
        {
            try
            {
                throw exc;
            }
            catch(Ice.AdapterNotFoundException ex)
            {
                Instance instance = @ref.getInstance();
                if(instance.traceLevels().location >= 1)
                {
                    System.Text.StringBuilder s = new System.Text.StringBuilder();
                    s.Append("adapter not found\n");
                    s.Append("adapter = " + @ref.getAdapterId());
                    instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.ToString());
                }

                Ice.NotRegisteredException e = new Ice.NotRegisteredException(ex);
                e.kindOfObject = "object adapter";
                e.id = @ref.getAdapterId();
                throw e;
            }
            catch(Ice.ObjectNotFoundException ex)
            {
                Instance instance = @ref.getInstance();
                if(instance.traceLevels().location >= 1)
                {
                    System.Text.StringBuilder s = new System.Text.StringBuilder();
                    s.Append("object not found\n");
                    s.Append("object = " + Ice.Util.identityToString(@ref.getIdentity(), instance.toStringMode()));
                    instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.ToString());
                }

                Ice.NotRegisteredException e = new Ice.NotRegisteredException(ex);
                e.kindOfObject = "object";
                e.id = Ice.Util.identityToString(@ref.getIdentity(), instance.toStringMode());
                throw e;
            }
            catch(Ice.NotRegisteredException)
            {
                throw;
            }
            catch(Ice.LocalException ex)
            {
                Instance instance = @ref.getInstance();
                if(instance.traceLevels().location >= 1)
                {
                    System.Text.StringBuilder s = new System.Text.StringBuilder();
                    s.Append("couldn't contact the locator to retrieve adapter endpoints\n");
                    if(@ref.getAdapterId().Length > 0)
                    {
                        s.Append("adapter = " + @ref.getAdapterId() + "\n");
                    }
                    else
                    {
                        s.Append("object = " + Ice.Util.identityToString(@ref.getIdentity(), instance.toStringMode()) + "\n");
                    }
                    s.Append("reason = " + ex);
                    instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.ToString());
                }
                throw;
            }
            catch(System.Exception)
            {
                Debug.Assert(false);
            }
        }

        private void getEndpointsTrace(Reference @ref, EndpointI[] endpoints, bool cached)
        {
            if(endpoints != null && endpoints.Length > 0)
            {
                if(cached)
                {
                    trace("found endpoints in locator table", @ref, endpoints);
                }
                else
                {
                    trace("retrieved endpoints from locator, adding to locator table", @ref, endpoints);
                }
            }
            else
            {
                Instance instance = @ref.getInstance();
                System.Text.StringBuilder s = new System.Text.StringBuilder();
                s.Append("no endpoints configured for ");
                if(@ref.getAdapterId().Length > 0)
                {
                    s.Append("adapter\n");
                    s.Append("adapter = " + @ref.getAdapterId());
                }
                else
                {
                    s.Append("object\n");
                    s.Append("object = " + Ice.Util.identityToString(@ref.getIdentity(), instance.toStringMode()));
                }
                instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.ToString());
            }
        }

        private Request
        getAdapterRequest(Reference @ref)
        {
            if(@ref.getInstance().traceLevels().location >= 1)
            {
                Instance instance = @ref.getInstance();
                System.Text.StringBuilder s = new System.Text.StringBuilder();
                s.Append("searching for adapter by id\nadapter = ");
                s.Append(@ref.getAdapterId());
                instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.ToString());
            }

            lock(this)
            {
                Request request;
                if(_adapterRequests.TryGetValue(@ref.getAdapterId(), out request))
                {
                    return request;
                }

                request = new AdapterRequest(this, @ref);
                _adapterRequests.Add(@ref.getAdapterId(), request);
                return request;
            }
        }

        private Request
        getObjectRequest(Reference @ref)
        {
            if(@ref.getInstance().traceLevels().location >= 1)
            {
                Instance instance = @ref.getInstance();
                System.Text.StringBuilder s = new System.Text.StringBuilder();
                s.Append("searching for object by id\nobject = ");
                s.Append(Ice.Util.identityToString(@ref.getIdentity(), instance.toStringMode()));
                instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.ToString());
            }

            lock(this)
            {
                Request request;
                if(_objectRequests.TryGetValue(@ref.getIdentity(), out request))
                {
                    return request;
                }

                request = new ObjectRequest(this, @ref);
                _objectRequests.Add(@ref.getIdentity(), request);
                return request;
            }
        }

        private void
        finishRequest(Reference @ref, List<Reference> wellKnownRefs, Ice.ObjectPrx proxy, bool notRegistered)
        {
            Ice.ObjectPrxHelperBase @base = proxy as Ice.ObjectPrxHelperBase;
            if(proxy == null || @base.iceReference().isIndirect())
            {
                //
                // Remove the cached references of well-known objects for which we tried
                // to resolved the endpoints if these endpoints are empty.
                //
                foreach(Reference r in wellKnownRefs)
                {
                    _table.removeObjectReference(r.getIdentity());
                }
            }

            if(!@ref.isWellKnown())
            {
                if(proxy != null && !@base.iceReference().isIndirect())
                {
                    // Cache the adapter endpoints.
                    _table.addAdapterEndpoints(@ref.getAdapterId(), @base.iceReference().getEndpoints());
                }
                else if(notRegistered) // If the adapter isn't registered anymore, remove it from the cache.
                {
                    _table.removeAdapterEndpoints(@ref.getAdapterId());
                }

                lock(this)
                {
                    Debug.Assert(_adapterRequests.ContainsKey(@ref.getAdapterId()));
                    _adapterRequests.Remove(@ref.getAdapterId());
                }
            }
            else
            {
                if(proxy != null && !@base.iceReference().isWellKnown())
                {
                    // Cache the well-known object reference.
                    _table.addObjectReference(@ref.getIdentity(), @base.iceReference());
                }
                else if(notRegistered) // If the well-known object isn't registered anymore, remove it from the cache.
                {
                    _table.removeObjectReference(@ref.getIdentity());
                }

                lock(this)
                {
                    Debug.Assert(_objectRequests.ContainsKey(@ref.getIdentity()));
                    _objectRequests.Remove(@ref.getIdentity());
                }
            }
        }

        private readonly Ice.LocatorPrx _locator;
        private Ice.LocatorRegistryPrx _locatorRegistry;
        private readonly LocatorTable _table;
        private readonly bool _background;

        private Dictionary<string, Request> _adapterRequests = new Dictionary<string, Request>();
        private Dictionary<Ice.Identity, Request> _objectRequests = new Dictionary<Ice.Identity, Request>();
    }

    public sealed class LocatorManager
    {
        struct LocatorKey
        {
            public LocatorKey(Ice.LocatorPrx prx)
            {
                Reference r = ((Ice.ObjectPrxHelperBase)prx).iceReference();
                _id = r.getIdentity();
                _encoding = r.getEncoding();
            }

            public override bool Equals(object o)
            {
                LocatorKey k = (LocatorKey)o;
                if(!k._id.Equals(_id))
                {
                    return false;
                }
                if(!k._encoding.Equals(_encoding))
                {
                    return false;
                }
                return true;
            }

            public override int GetHashCode()
            {
                int h = 5381;
                HashUtil.hashAdd(ref h, _id);
                HashUtil.hashAdd(ref h, _encoding);
                return h;
            }

            private Ice.Identity _id;
            private Ice.EncodingVersion _encoding;
        }

        internal LocatorManager(Ice.Properties properties)
        {
            _table = new Dictionary<Ice.LocatorPrx, LocatorInfo>();
            _locatorTables = new Dictionary<LocatorKey, LocatorTable>();
            _background = properties.getPropertyAsInt("Ice.BackgroundLocatorCacheUpdates") > 0;
        }

        internal void destroy()
        {
            lock(this)
            {
                foreach(LocatorInfo info in _table.Values)
                {
                    info.destroy();
                }
                _table.Clear();
                _locatorTables.Clear();
            }
        }

        //
        // Returns locator info for a given locator. Automatically creates
        // the locator info if it doesn't exist yet.
        //
        public LocatorInfo get(Ice.LocatorPrx loc)
        {
            if(loc == null)
            {
                return null;
            }

            //
            // The locator can't be located.
            //
            Ice.LocatorPrx locator = Ice.LocatorPrxHelper.uncheckedCast(loc.ice_locator(null));

            //
            // TODO: reap unused locator info objects?
            //
            lock(this)
            {
                LocatorInfo info = null;
                if(!_table.TryGetValue(locator, out info))
                {
                    //
                    // Rely on locator identity for the adapter table. We want to
                    // have only one table per locator (not one per locator
                    // proxy).
                    //
                    LocatorTable table = null;
                    LocatorKey key = new LocatorKey(locator);
                    if(!_locatorTables.TryGetValue(key, out table))
                    {
                        table = new LocatorTable();
                        _locatorTables[key] = table;
                    }

                    info = new LocatorInfo(locator, table, _background);
                    _table[locator] = info;
                }

                return info;
            }
        }

        private Dictionary<Ice.LocatorPrx, LocatorInfo> _table;
        private Dictionary<LocatorKey, LocatorTable> _locatorTables;
        private readonly bool _background;
    }

    sealed class LocatorTable
    {
        internal LocatorTable()
        {
            _adapterEndpointsTable = new Dictionary<string, EndpointTableEntry>();
            _objectTable = new Dictionary<Ice.Identity, ReferenceTableEntry>();
        }

        internal void clear()
        {
            lock(this)
            {
                _adapterEndpointsTable.Clear();
                _objectTable.Clear();
            }
        }

        internal EndpointI[] getAdapterEndpoints(string adapter, int ttl, out bool cached)
        {
            if(ttl == 0) // Locator cache disabled.
            {
                cached = false;
                return null;
            }

            lock(this)
            {
                EndpointTableEntry entry = null;
                if(_adapterEndpointsTable.TryGetValue(adapter, out entry))
                {
                    cached = checkTTL(entry.time, ttl);
                    return entry.endpoints;

                }
                cached = false;
                return null;
            }
        }

        internal void addAdapterEndpoints(string adapter, EndpointI[] endpoints)
        {
            lock(this)
            {
                _adapterEndpointsTable[adapter] =
                    new EndpointTableEntry(Time.currentMonotonicTimeMillis(), endpoints);
            }
        }

        internal EndpointI[] removeAdapterEndpoints(string adapter)
        {
            lock(this)
            {
                EndpointTableEntry entry = null;
                if(_adapterEndpointsTable.TryGetValue(adapter, out entry))
                {
                    _adapterEndpointsTable.Remove(adapter);
                    return entry.endpoints;
                }
                return null;
            }
        }

        internal Reference getObjectReference(Ice.Identity id, int ttl, out bool cached)
        {
            if(ttl == 0) // Locator cache disabled.
            {
                cached = false;
                return null;
            }

            lock(this)
            {
                ReferenceTableEntry entry = null;
                if(_objectTable.TryGetValue(id, out entry))
                {
                    cached = checkTTL(entry.time, ttl);
                    return entry.reference;
                }
                cached = false;
                return null;
            }
        }

        internal void addObjectReference(Ice.Identity id, Reference reference)
        {
            lock(this)
            {
                _objectTable[id] = new ReferenceTableEntry(Time.currentMonotonicTimeMillis(), reference);
            }
        }

        internal Reference removeObjectReference(Ice.Identity id)
        {
            lock(this)
            {
                ReferenceTableEntry entry = null;
                if(_objectTable.TryGetValue(id, out entry))
                {
                    _objectTable.Remove(id);
                    return entry.reference;
                }
                return null;
            }
        }

        private bool checkTTL(long time, int ttl)
        {
            Debug.Assert(ttl != 0);
            if(ttl < 0) // TTL = infinite
            {
                return true;
            }
            else
            {
                return Time.currentMonotonicTimeMillis() - time <= ((long)ttl * 1000);
            }
        }

        sealed private class EndpointTableEntry
        {
            public EndpointTableEntry(long time, EndpointI[] endpoints)
            {
                this.time = time;
                this.endpoints = endpoints;
            }

            public long time;
            public EndpointI[] endpoints;
        }

        sealed private class ReferenceTableEntry
        {
            public ReferenceTableEntry(long time, Reference reference)
            {
                this.time = time;
                this.reference = reference;
            }

            public long time;
            public Reference reference;
        }

        private Dictionary<string, EndpointTableEntry> _adapterEndpointsTable;
        private Dictionary<Ice.Identity, ReferenceTableEntry> _objectTable;
    }

}