diff options
author | Benoit Foucher <benoit@zeroc.com> | 2009-01-15 16:02:54 +0100 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2009-01-15 16:02:54 +0100 |
commit | a10f33af6ee6298b4a7412a3819cc4818f086fa4 (patch) | |
tree | 7dc924b0692491a6419b81ae419b559ccef467cf /java/src/IceInternal/LocatorInfo.java | |
parent | bug 3266 - fix IceSSL/C++ for custom contexts (diff) | |
download | ice-a10f33af6ee6298b4a7412a3819cc4818f086fa4.tar.bz2 ice-a10f33af6ee6298b4a7412a3819cc4818f086fa4.tar.xz ice-a10f33af6ee6298b4a7412a3819cc4818f086fa4.zip |
- Fixed errorDetection test
- Fixed bug 3504, added locator request queue.
Diffstat (limited to 'java/src/IceInternal/LocatorInfo.java')
-rw-r--r-- | java/src/IceInternal/LocatorInfo.java | 465 |
1 files changed, 371 insertions, 94 deletions
diff --git a/java/src/IceInternal/LocatorInfo.java b/java/src/IceInternal/LocatorInfo.java index ffec5d256c7..5e7938a3ce2 100644 --- a/java/src/IceInternal/LocatorInfo.java +++ b/java/src/IceInternal/LocatorInfo.java @@ -11,12 +11,325 @@ package IceInternal; public final class LocatorInfo { + private interface RequestCallback + { + public void response(LocatorInfo locatorInfo, Ice.ObjectPrx proxy); + public void exception(LocatorInfo locatorInfo, Exception ex); + }; + + private abstract class Request + { + synchronized public void + addCallback(RequestCallback callback) + { + if(_response) + { + callback.response(_locatorInfo, _proxy); + return; + } + else if(_exception != null) + { + callback.exception(_locatorInfo, _exception); + return; + } + + _callbacks.add(callback); + + if(!_sent) + { + _sent = true; + try + { + send(); + } + catch(Exception ex) + { + exception(ex); + } + } + } + + synchronized public Ice.ObjectPrx + getProxy() + throws Exception + { + if(_response) + { + return _proxy; + } + else if(_exception != null) + { + throw _exception; + } + + if(!_sent) + { + _sent = true; + try + { + send(); + } + catch(Exception ex) + { + exception(ex); + } + } + + while(!_response && _exception == null) + { + try + { + wait(); + } + catch(java.lang.InterruptedException ex) + { + } + } + + if(_exception != null) + { + throw _exception; + } + assert(_response); + return _proxy; + } + + protected + Request(LocatorInfo locatorInfo) + { + _locatorInfo = locatorInfo; + _sent = false; + _response = false; + } + + synchronized protected void + response(Ice.ObjectPrx proxy) + { + _response = true; + _proxy = proxy; + for(RequestCallback c : _callbacks) + { + c.response(_locatorInfo, proxy); + } + notifyAll(); + } + + synchronized protected void + exception(Exception ex) + { + _exception = ex; + for(RequestCallback c : _callbacks) + { + c.exception(_locatorInfo, ex); + } + notifyAll(); + } + + protected abstract void send(); + + final protected LocatorInfo _locatorInfo; + + private java.util.List<RequestCallback> _callbacks = new java.util.ArrayList<RequestCallback>(); + private boolean _sent; + private boolean _response; + private Ice.ObjectPrx _proxy; + private Exception _exception; + }; + interface GetEndpointsCallback { void setEndpoints(EndpointI[] endpoints, boolean cached); void setException(Ice.LocalException ex); } + private class ObjectRequest extends Request + { + public ObjectRequest(LocatorInfo locatorInfo, Ice.Identity id) + { + super(locatorInfo); + _id = id; + } + + protected void + send() + { + _locatorInfo.getLocator().findObjectById_async( + new Ice.AMI_Locator_findObjectById() + { + public void + ice_response(Ice.ObjectPrx proxy) + { + _locatorInfo.removeObjectRequest(_id); + response(proxy); + } + + public void + ice_exception(Ice.UserException ex) + { + _locatorInfo.removeObjectRequest(_id); + exception(ex); + } + + public void + ice_exception(Ice.LocalException ex) + { + _locatorInfo.removeObjectRequest(_id); + exception(ex); + } + }, + _id); + } + + private final Ice.Identity _id; + }; + + private class AdapterRequest extends Request + { + public AdapterRequest(LocatorInfo locatorInfo, String id) + { + super(locatorInfo); + _id = id; + } + + protected void + send() + { + _locatorInfo.getLocator().findAdapterById_async( + new Ice.AMI_Locator_findAdapterById() + { + public void + ice_response(Ice.ObjectPrx proxy) + { + _locatorInfo.removeAdapterRequest(_id); + response(proxy); + } + + public void + ice_exception(Ice.UserException ex) + { + _locatorInfo.removeAdapterRequest(_id); + exception(ex); + } + + public void + ice_exception(Ice.LocalException ex) + { + _locatorInfo.removeAdapterRequest(_id); + exception(ex); + } + }, + _id); + } + + private final String _id; + }; + + private class ObjectRequestCallback implements RequestCallback + { + public void + response(LocatorInfo locatorInfo, Ice.ObjectPrx object) + { + locatorInfo.getWellKnownObjectEndpoints(_reference, object, _ttl, false, _callback); + } + + public void + exception(LocatorInfo locatorInfo, Exception ex) + { + if(ex instanceof Ice.CollocationOptimizationException) + { + try + { + Ice.BooleanHolder cached = new Ice.BooleanHolder(); + _callback.setEndpoints(locatorInfo.getEndpoints(_reference, _ttl, cached), cached.value); + } + catch(Ice.LocalException e) + { + _callback.setException(e); + } + } + else + { + locatorInfo.getEndpointsException(_reference, ex, _callback); + } + } + + public + ObjectRequestCallback(Reference ref, int ttl, GetEndpointsCallback cb) + { + _reference = ref; + _ttl = ttl; + _callback = cb; + } + + private final Reference _reference; + private final int _ttl; + private final GetEndpointsCallback _callback; + }; + + private class AdapterRequestCallback implements RequestCallback + { + public void + response(LocatorInfo locatorInfo, Ice.ObjectPrx object) + { + EndpointI[] endpoints = null; + if(object != null) + { + endpoints = ((Ice.ObjectPrxHelperBase)object).__reference().getEndpoints(); + if(endpoints.length > 0) + { + locatorInfo.getTable().addAdapterEndpoints(_reference.getAdapterId(), endpoints); + } + } + + if(_reference.getInstance().traceLevels().location >= 1) + { + locatorInfo.getEndpointsTrace(_reference, endpoints, false); + } + + if(endpoints == null) + { + _callback.setEndpoints(new EndpointI[0], false); + } + else + { + _callback.setEndpoints(endpoints, false); + } + } + + public void + exception(LocatorInfo locatorInfo, Exception ex) + { + if(ex instanceof Ice.CollocationOptimizationException) + { + try + { + Ice.BooleanHolder cached = new Ice.BooleanHolder(); + _callback.setEndpoints(getEndpoints(_reference, _ttl, cached), cached.value); + } + catch(Ice.LocalException e) + { + _callback.setException(e); + } + } + else + { + getEndpointsException(_reference, ex, _callback); + } + } + + public + AdapterRequestCallback(Reference ref, int ttl, GetEndpointsCallback callback) + { + _reference = ref; + _ttl = ttl; + _callback = callback; + } + + private final Reference _reference; + private final int _ttl; + private final GetEndpointsCallback _callback; + }; + LocatorInfo(Ice.LocatorPrx locator, LocatorTable table) { _locator = locator; @@ -104,7 +417,8 @@ public final class LocatorInfo // Search the adapter in the location service if we didn't // find it in the cache. // - object = _locator.findAdapterById(adapterId); + Request request = getAdapterRequest(adapterId); + object = request.getProxy(); if(object != null) { endpoints = ((Ice.ObjectPrxHelperBase)object).__reference().getEndpoints(); @@ -133,7 +447,8 @@ public final class LocatorInfo ref.getInstance().traceLevels().locationCat, s.toString()); } - object = _locator.findObjectById(identity); + Request request = getObjectRequest(identity); + object = request.getProxy(); } boolean endpointsCached = true; @@ -197,63 +512,8 @@ public final class LocatorInfo // Search the adapter in the location service if we didn't // find it in the cache. // - _locator.findAdapterById_async(new Ice.AMI_Locator_findAdapterById() - { - public void - ice_response(Ice.ObjectPrx object) - { - EndpointI[] endpoints = null; - if(object != null) - { - endpoints = ((Ice.ObjectPrxHelperBase)object).__reference().getEndpoints(); - if(endpoints.length > 0) - { - _table.addAdapterEndpoints(adapterId, endpoints); - } - } - - if(instance.traceLevels().location >= 1) - { - getEndpointsTrace(ref, endpoints, false); - } - - if(endpoints == null) - { - callback.setEndpoints(new EndpointI[0], false); - } - else - { - callback.setEndpoints(endpoints, false); - } - } - - public void - ice_exception(Ice.UserException ex) - { - getEndpointsException(ref, ex, callback); - } - - public void - ice_exception(Ice.LocalException ex) - { - if(ex instanceof Ice.CollocationOptimizationException) - { - try - { - Ice.BooleanHolder cached = new Ice.BooleanHolder(); - callback.setEndpoints(getEndpoints(ref, ttl, cached), cached.value); - } - catch(Ice.LocalException e) - { - callback.setException(e); - } - } - else - { - getEndpointsException(ref, ex, callback); - } - } - }, adapterId); + Request request = getAdapterRequest(adapterId); + request.addCallback(new AdapterRequestCallback(ref, ttl, callback)); return; } else @@ -278,42 +538,9 @@ public final class LocatorInfo s.append("object = " + instance.identityToString(identity)); instance.initializationData().logger.trace(instance.traceLevels().locationCat, s.toString()); } - - _locator.findObjectById_async(new Ice.AMI_Locator_findObjectById() - { - public void - ice_response(Ice.ObjectPrx object) - { - getWellKnownObjectEndpoints(ref, object, ttl, false, callback); - } - - public void - ice_exception(Ice.UserException ex) - { - getEndpointsException(ref, ex, callback); - } - public void - ice_exception(Ice.LocalException ex) - { - if(ex instanceof Ice.CollocationOptimizationException) - { - try - { - Ice.BooleanHolder cached = new Ice.BooleanHolder(); - callback.setEndpoints(getEndpoints(ref, ttl, cached), cached.value); - } - catch(Ice.LocalException e) - { - callback.setException(e); - } - } - else - { - getEndpointsException(ref, ex, callback); - } - } - }, identity); + Request request = getObjectRequest(identity); + request.addCallback(new ObjectRequestCallback(ref, ttl, callback)); return; } else @@ -600,7 +827,57 @@ public final class LocatorInfo } } + synchronized private Request + getAdapterRequest(String id) + { + Request request = _adapterRequests.get(id); + if(request != null) + { + return request; + } + + request = new AdapterRequest(this, id); + _adapterRequests.put(id, request); + return request; + } + + synchronized private void + removeAdapterRequest(String id) + { + assert(_adapterRequests.get(id) != null); + _adapterRequests.remove(id); + } + + synchronized private Request + getObjectRequest(Ice.Identity id) + { + Request request = _objectRequests.get(id); + if(request != null) + { + return request; + } + + request = new ObjectRequest(this, id); + _objectRequests.put(id, request); + return request; + } + + synchronized private void + removeObjectRequest(Ice.Identity id) + { + assert(_objectRequests.get(id) != null); + _objectRequests.remove(id); + } + + LocatorTable getTable() + { + return _table; + } + private final Ice.LocatorPrx _locator; private Ice.LocatorRegistryPrx _locatorRegistry; private final LocatorTable _table; + + private java.util.Map<String, Request> _adapterRequests = new java.util.HashMap<String, Request>(); + private java.util.Map<Ice.Identity, Request> _objectRequests = new java.util.HashMap<Ice.Identity, Request>(); } |