Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2010 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) 2008, Nathan Sweet
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package dorkbox.network.rmi;
import com.esotericsoftware.kryo.util.IntMap;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.EndPoint;
import dorkbox.network.connection.Listener;
import dorkbox.util.collections.ObjectIntMap;
import org.slf4j.Logger;
import java.io.IOException;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
/**
* Allows methods on objects to be invoked remotely over TCP, UDP, UDT, or LOCAL. Local connections ignore TCP/UDP/UDT requests, and perform
* object transformation (because there is no serialization occurring) using a series of weak hashmaps.
*
*
* Objects are {@link dorkbox.network.util.RMISerializationManager#registerRemote(Class, Class)}, and endpoint connections can then {@link
* Connection#createProxyObject(Class)} for the registered objects.
*
* It costs at least 2 bytes more to use remote method invocation than just sending the parameters. If the method has a return value which
* is not {@link RemoteObject#setAsync(boolean) ignored}, an extra byte is written. If the type of a parameter is not final (note that
* primitives are final) then an extra byte is written for that parameter.
*
*
* In situations where we want to pass in the Connection (to an RMI method), we have to be able to override method A, with method B.
*
* This is to support calling RMI methods from an interface (that does pass the connection reference) to an implementation, that DOES pass
* the connection reference. The remote side (that initiates the RMI calls), MUST use the interface, and the implementation may override the
* method, so that we add the connection as the first in the list of parameters.
*
* for example:
* Interface: foo(String x)
* Impl: foo(Connection c, String x)
*
* The implementation (if it exists, with the same name, and with the same signature+connection) will be called from the interface. This
* MUST hold valid for both remote and local connection types.
*
* To facilitate this functionality, for methods with the same name, the "overriding" method is the one that inherits the Connection
* interface as the first parameter, and CachedMethod.registerOverridden(ifaceClass, implClass) must be called.
*
*
* @author Nathan Sweet , Nathan Robinson
*/
public final
class RmiBridge {
public static final int INVALID_RMI = 0;
static final int returnValueMask = 1 << 7;
static final int returnExceptionMask = 1 << 6;
static final int responseIdMask = 0xFF & ~returnValueMask & ~returnExceptionMask;
// global RMI objects -> ODD in range 1-16380 (max 2 bytes) throws error on outside of range
// connection local RMI -> EVEN in range 1-16380 (max 2 bytes) throws error on outside of range
private static final int MAX_RMI_VALUE = 16380;
/**
* @return true if the objectId is a "global" id (it's odd) otherwise, false (it's connection local)
*/
public static
boolean isGlobal(final int objectId) {
return (objectId & 1) != 0;
}
// the name of who created this RmiBridge
private final org.slf4j.Logger logger;
// we start at 1, because 0 (INVALID_RMI) means we access connection only objects
private final AtomicInteger rmiObjectIdCounter;
// can be accessed by DIFFERENT threads.
private final ReentrantReadWriteLock objectLock = new ReentrantReadWriteLock();
private final IntMap