@Override public ProtocolSignature getProtocolSignature(String protocol, long clientVersion, int clientMethodsHash) throws IOException { return ProtocolSignature.getProtocolSignature(this, protocol, clientVersion, clientMethodsHash); }
/** * Return a protocol's signature and finger print from cache * * @param protocol a protocol class * @param serverVersion protocol version * @return its signature and finger print */ private static ProtocolSigFingerprint getSigFingerprint( Class <?> protocol, long serverVersion) { String protocolName = RPC.getProtocolName(protocol); synchronized (PROTOCOL_FINGERPRINT_CACHE) { ProtocolSigFingerprint sig = PROTOCOL_FINGERPRINT_CACHE.get(protocolName); if (sig == null) { int[] serverMethodHashcodes = getFingerprints(protocol.getMethods()); sig = new ProtocolSigFingerprint( new ProtocolSignature(serverVersion, serverMethodHashcodes), getFingerprint(serverMethodHashcodes)); PROTOCOL_FINGERPRINT_CACHE.put(protocolName, sig); } return sig; } }
private void fetchServerMethods(Method method) throws IOException { long clientVersion; clientVersion = RPC.getProtocolVersion(method.getDeclaringClass()); int clientMethodsHash = ProtocolSignature.getFingerprint(method .getDeclaringClass().getMethods()); ProtocolSignature serverInfo = ((VersionedProtocol) proxy) .getProtocolSignature(RPC.getProtocolName(protocol), clientVersion, clientMethodsHash); long serverVersion = serverInfo.getVersion(); if (serverVersion != clientVersion) { throw new RPC.VersionMismatch(protocol.getName(), clientVersion, serverVersion); } int[] serverMethodsCodes = serverInfo.getMethods(); if (serverMethodsCodes != null) { serverMethods = new HashSet<Integer>(serverMethodsCodes.length); for (int m : serverMethodsCodes) { this.serverMethods.add(Integer.valueOf(m)); } } serverMethodsFetched = true; }
/** * Get the hash code of an array of methods * Methods are sorted before hashcode is calculated. * So the returned value is irrelevant of the method order in the array. * * @param methods an array of methods * @return the hash code */ static int getFingerprint(Method[] methods) { return getFingerprint(getFingerprints(methods)); }
/** * Get a server protocol's signature * * @param clientMethodsHashCode client protocol methods hashcode * @param serverVersion server protocol version * @param protocol protocol * @return the server's protocol signature */ public static ProtocolSignature getProtocolSignature( int clientMethodsHashCode, long serverVersion, Class<? extends VersionedProtocol> protocol) { // try to get the finger print & signature from the cache ProtocolSigFingerprint sig = getSigFingerprint(protocol, serverVersion); // check if the client side protocol matches the one on the server side if (clientMethodsHashCode == sig.fingerprint) { return new ProtocolSignature(serverVersion, null); // null indicates a match } return sig.signature; }
sigBuilder.setVersion(v); try { ProtocolSignature signature = ProtocolSignature.getProtocolSignature( protocol, v); for (int m : signature.getMethods()) { sigBuilder.addMethods(m);
@Override public Writable newInstance() { return new ProtocolSignature(); } });
/** * Convert an array of Method into an array of hash codes * * @param methods * @return array of hash codes */ private static int[] getFingerprints(Method[] methods) { if (methods == null) { return null; } int[] hashCodes = new int[methods.length]; for (int i = 0; i<methods.length; i++) { hashCodes[i] = getFingerprint(methods[i]); } return hashCodes; }
ProtocolSignature serverInfo = proxy .getProtocolSignature(protocolName, clientVersion, ProtocolSignature.getFingerprint(protocol.getMethods())); return new ProtocolProxy<T>(protocol, proxy, serverInfo.getMethods()); } catch (RemoteException re) { IOException ioe = re.unwrapRemoteException(IOException.class);
public static ProtocolSignature getProtocolSignature(String protocolName, long version) throws ClassNotFoundException { Class<?> protocol = Class.forName(protocolName); return getSigFingerprint(protocol, version).signature; }
private static boolean methodExists(int methodHash, long version, Map<Long, ProtocolSignature> versionMap) { ProtocolSignature sig = versionMap.get(version); if (sig != null) { for (int m : sig.getMethods()) { if (m == methodHash) { return true; } } } return false; }
sigBuilder.setVersion(v); try { ProtocolSignature signature = ProtocolSignature.getProtocolSignature( protocol, v); for (int m : signature.getMethods()) { sigBuilder.addMethods(m);
private static Map<Long, ProtocolSignature> convertProtocolSignatureProtos(List<ProtocolSignatureProto> protoList) { Map<Long, ProtocolSignature> map = new TreeMap<Long, ProtocolSignature>(); for (ProtocolSignatureProto p : protoList) { int [] methods = new int[p.getMethodsList().size()]; int index=0; for (int m : p.getMethodsList()) { methods[index++] = m; } map.put(p.getVersion(), new ProtocolSignature(p.getVersion(), methods)); } return map; }
/** * Get a server protocol's signature * * @param clientMethodsHashCode client protocol methods hashcode * @param serverVersion server protocol version * @param protocol protocol * @return the server's protocol signature */ public static ProtocolSignature getProtocolSignature( int clientMethodsHashCode, long serverVersion, Class<? extends VersionedProtocol> protocol) { // try to get the finger print & signature from the cache ProtocolSigFingerprint sig = getSigFingerprint(protocol, serverVersion); // check if the client side protocol matches the one on the server side if (clientMethodsHashCode == sig.fingerprint) { return new ProtocolSignature(serverVersion, null); // null indicates a match } return sig.signature; }
/** * Get the hash code of an array of methods * Methods are sorted before hashcode is calculated. * So the returned value is irrelevant of the method order in the array. * * @param methods an array of methods * @return the hash code */ static int getFingerprint(Method[] methods) { return getFingerprint(getFingerprints(methods)); }
public Invocation(Method method, Object[] parameters) { this.methodName = method.getName(); this.parameterClasses = method.getParameterTypes(); this.parameters = parameters; rpcVersion = writableRpcVersion; if (method.getDeclaringClass().equals(VersionedProtocol.class)) { //VersionedProtocol is exempted from version check. clientVersion = 0; clientMethodsHash = 0; } else { this.clientVersion = RPC.getProtocolVersion(method.getDeclaringClass()); this.clientMethodsHash = ProtocolSignature.getFingerprint(method .getDeclaringClass().getMethods()); } this.declaringClassProtocolName = RPC.getProtocolName(method.getDeclaringClass()); }
public static int getMethodsSigFingerPrint(Class <? extends VersionedProtocol> protocol, long serverVersion) { return getSigFingerprint(protocol, serverVersion).fingerprint; }
private static boolean methodExists(int methodHash, long version, Map<Long, ProtocolSignature> versionMap) { ProtocolSignature sig = versionMap.get(version); if (sig != null) { for (int m : sig.getMethods()) { if (m == methodHash) { return true; } } } return false; }
/** {@inheritDoc} */ @Override public ProtocolSignature getProtocolSignature(String protocol, long clientVersion, int clientMethodsHash) throws IOException { return ProtocolSignature.getProtocolSignature(this, protocol, clientVersion, clientMethodsHash); }
sigBuilder.setVersion(v); try { ProtocolSignature signature = ProtocolSignature.getProtocolSignature( protocol, v); for (int m : signature.getMethods()) { sigBuilder.addMethods(m);