Changeset 327:9900146bbf30 in finroc_core-java


Ignore:
Timestamp:
20.08.2019 07:57:33 (5 weeks ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
default
Phase:
public
Tags:
tip
Message:

Makes improvements and extensions to URI authority handling (e.g. use of concurrent list; distinction of same-host and different-host URI authority support)

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • net/generic_protocol/RemoteRuntime.java

    r326 r327  
    6161import org.finroc.core.remote.RemoteType; 
    6262import org.finroc.core.remote.RemoteUriConnector; 
     63import org.rrlib.finroc_core_utils.jc.ArrayWrapper; 
     64import org.rrlib.finroc_core_utils.jc.container.SafeConcurrentlyIterableList; 
    6365import org.rrlib.logging.Log; 
    6466import org.rrlib.logging.LogLevel; 
     
    122124 
    123125    /** List with alternatives to reference this runtime in URI authority (host) */ 
    124     private final ArrayList<String> uriAuthorityAlternatives = new ArrayList<String>(); 
     126    private final SafeConcurrentlyIterableList<String> uriAuthorityAlternatives = new SafeConcurrentlyIterableList<String>(10, 2); 
     127    private final SafeConcurrentlyIterableList<String> uriAuthorityAlternativesSameHost = new SafeConcurrentlyIterableList<String>(10, 2); 
    125128 
    126129 
     
    229232     * @param alternative New alternative 
    230233     */ 
    231     public void addUriAuthorityAlternative(String alternative) { 
    232         synchronized (uriAuthorityAlternatives) { 
    233             if (!uriAuthorityAlternatives.contains(alternative)) { 
    234                 uriAuthorityAlternatives.add(alternative); 
    235             } 
    236         } 
     234    public void addUriAuthorityAlternative(String alternative, boolean sameHostOnly) { 
     235        if (!sameHostOnly) { 
     236            ArrayWrapper<String> iterable = uriAuthorityAlternatives.getIterable(); 
     237            for (int i = 0, n = iterable.size(); i < n; i++) { 
     238                String s = iterable.get(i); 
     239                if (alternative.equals(s)) { 
     240                    return; 
     241                } 
     242            } 
     243            uriAuthorityAlternatives.add(alternative, true); 
     244        } 
     245 
     246        ArrayWrapper<String> iterable = uriAuthorityAlternativesSameHost.getIterable(); 
     247        for (int i = 0, n = iterable.size(); i < n; i++) { 
     248            String s = iterable.get(i); 
     249            if (alternative.equals(s)) { 
     250                return; 
     251            } 
     252        } 
     253        //System.out.println(getName() + " adding " + alternative); 
     254        uriAuthorityAlternativesSameHost.add(alternative, true); 
    237255    } 
    238256 
     
    364382 
    365383    /** 
    366      * @param sameHost Whether to include alternatives from the local host 
     384     * @param sameHost Whether to include alternatives from the localhost 
    367385     * @return List with alternatives to reference this runtime in URI authority (host). The returned list is a copy (-> thread-safe) 
    368386     */ 
    369     public ArrayList<String> getUriAuthorityAlternatives(boolean sameHost) { 
    370         synchronized (uriAuthorityAlternatives) { 
    371             return new ArrayList<String>(uriAuthorityAlternatives); 
    372         } 
     387    public SafeConcurrentlyIterableList<String> getUriAuthorityAlternatives(boolean sameHost) { 
     388        return sameHost ? uriAuthorityAlternativesSameHost : uriAuthorityAlternatives; 
    373389    } 
    374390 
     
    378394     */ 
    379395    public abstract boolean hasSameHost(RemoteRuntime other); 
     396 
     397    /** 
     398     * @param authority URI authority string 
     399     * @param originRuntime Runtime in which URI is used 
     400     * @return Whether this runtime can be addressed using the authority string 
     401     */ 
     402    public boolean hasUriAuthority(String authority, RemoteRuntime originRuntime) { 
     403        ArrayWrapper<String> iterable = getUriAuthorityAlternatives(originRuntime.hasSameHost(this)).getIterable(); 
     404        for (int i = 0, n = iterable.size(); i < n; i++) { 
     405            String s = iterable.get(i); 
     406            if (authority.equals(s)) { 
     407                return true; 
     408            } 
     409        } 
     410        return false; 
     411    } 
    380412 
    381413    /** 
  • remote/RemoteRuntime.java

    r318 r327  
    5656 
    5757    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    58     public RemoteRuntime(String name, String uuid, AdminClient adminInterface, BinaryInputStream stream, int handleStampWidth) { 
     58    public RemoteRuntime(String name, String uuid, AdminClient adminInterface, BinaryInputStream stream, int handleStampWidth, org.finroc.core.net.generic_protocol.RemoteRuntime runtimeElement) { 
    5959        super(0, name); 
    6060        this.handleStampWidth = handleStampWidth; 
     
    6666        elementLookup = new ConcurrentLookupTable<>(chunkCount, chunkSize); 
    6767        connectorTable = new ConcurrentLookupTable<>(chunkCount, chunkSize); 
    68         remoteTypes = (Register<RemoteType>)(Register)PublishedRegisters.getRemoteRegister(stream, Definitions.RegisterUIDs.TYPE.ordinal()); 
    69         remoteStaticCasts = (Register<RemoteStaticCast>)(Register)PublishedRegisters.getRemoteRegister(stream, Definitions.RegisterUIDs.STATIC_CAST.ordinal()); 
    70         remoteTypeConversions = (Register<RemoteTypeConversion>)(Register)PublishedRegisters.getRemoteRegister(stream, Definitions.RegisterUIDs.CONVERSION_OPERATION.ordinal()); 
    71         remoteSchemeHandlers = (Register<RemoteUriSchemeHandler>)(Register)PublishedRegisters.getRemoteRegister(stream, Definitions.RegisterUIDs.SCHEME_HANDLER.ordinal()); 
    72         remoteCreateActions = (Register<RemoteCreateAction>)(Register)PublishedRegisters.getRemoteRegister(stream, Definitions.RegisterUIDs.CREATE_ACTION.ordinal()); 
     68        remoteTypes = (Register)PublishedRegisters.getRemoteRegister(stream, Definitions.RegisterUIDs.TYPE.ordinal()); 
     69        remoteStaticCasts = (Register)PublishedRegisters.getRemoteRegister(stream, Definitions.RegisterUIDs.STATIC_CAST.ordinal()); 
     70        remoteTypeConversions = (Register)PublishedRegisters.getRemoteRegister(stream, Definitions.RegisterUIDs.CONVERSION_OPERATION.ordinal()); 
     71        remoteSchemeHandlers = (Register)PublishedRegisters.getRemoteRegister(stream, Definitions.RegisterUIDs.SCHEME_HANDLER.ordinal()); 
     72        remoteCreateActions = (Register)PublishedRegisters.getRemoteRegister(stream, Definitions.RegisterUIDs.CREATE_ACTION.ordinal()); 
    7373        if (remoteStaticCasts == null) { 
    7474            remoteStaticCasts = new Register<>(4, 4, 1); 
     
    8686        this.uuid = uuid; 
    8787        this.adminInterface = adminInterface; 
     88        this.runtimeElement = runtimeElement; 
    8889        elementLookup.set(0, this); // Runtime lookup 
    8990        this.serializationInfo = stream.getSourceInfo(); 
     
    191192                return (RemoteRuntime)remoteElement; 
    192193            } 
    193             remoteElement = (ModelNode)remoteElement.getParent(); 
     194            remoteElement = remoteElement.getParent(); 
    194195        } while (remoteElement != null); 
    195196        return null; 
     
    986987    private final ArrayList<RemoteFrameworkElement> elementsInErrorState = new ArrayList<>(); 
    987988 
     989    /** Reference to remote runtime element of generic protocol (may be null) */ 
     990    private final org.finroc.core.net.generic_protocol.RemoteRuntime runtimeElement; 
     991 
    988992    /** Internal helper type for getConversionOptions() methods */ 
    989993    private static class GetCastOperationEntry implements Comparable<GetCastOperationEntry> { 
     
    11811185        return rating == Definitions.TypeConversionRating.IMPLICIT_CAST.ordinal() || rating == Definitions.TypeConversionRating.EXPLICIT_CONVERSION.ordinal() || rating == Definitions.TypeConversionRating.EXPLICIT_CONVERSION_FROM_GENERIC_TYPE.ordinal() || rating == Definitions.TypeConversionRating.EXPLICIT_CONVERSION_TO_GENERIC_TYPE.ordinal(); 
    11821186    } 
     1187 
     1188    /** 
     1189     * @param authority URI authority string 
     1190     * @param originRuntime Runtime in which URI is used 
     1191     * @return Whether this runtime can be addressed using the authority string 
     1192     */ 
     1193    public boolean hasUriAuthority(String authority, RemoteRuntime originRuntime) { 
     1194        if (originRuntime.runtimeElement == null || runtimeElement == null) { 
     1195            return originRuntime.runtimeElement == null && runtimeElement == null && originRuntime.uuid.equals(uuid); 
     1196        } 
     1197        return runtimeElement.hasUriAuthority(authority, originRuntime.runtimeElement); 
     1198    } 
     1199 
     1200    /** 
     1201     * @return Reference to remote runtime element of generic protocol (may be null) 
     1202     */ 
     1203    public org.finroc.core.net.generic_protocol.RemoteRuntime getRuntimeElement() { 
     1204        return runtimeElement; 
     1205    } 
    11831206} 
  • remote/RemoteUriConnector.java

    r293 r327  
    6060     */ 
    6161    protected boolean currentConnectionMayGoTo(RemoteRuntime runtime) { 
    62         return status == Status.CONNECTED && runtime != ownerRuntime && (authority == null || authority.length() == 0 || runtime.uuid.equals(authority)); 
     62        return status == Status.CONNECTED && runtime != ownerRuntime && (authority == null || authority.length() == 0 || runtime.hasUriAuthority(authority, ownerRuntime)); 
    6363    } 
    6464 
Note: See TracChangeset for help on using the changeset viewer.