com.esotericsoftware.kryonet.rmi
Class ObjectSpace

java.lang.Object
  extended by com.esotericsoftware.kryonet.rmi.ObjectSpace

public class ObjectSpace
extends java.lang.Object

Allows methods on objects to be invoked remotely over TCP. Objects are registered with an ID. The remote end of connections that have been added are allowed to access 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 ignored, an extra byte is written. If the type of a parameter is not final (note primitives are final) then an extra byte is written for that parameter.

Author:
Nathan Sweet

Nested Class Summary
static class ObjectSpace.InvokeMethod
          Internal message to invoke methods remotely.
static class ObjectSpace.InvokeMethodResult
          Internal message to return the result of a remotely invoked method.
 
Constructor Summary
ObjectSpace()
          Creates an ObjectSpace with no connections.
ObjectSpace(Connection connection)
          Creates an ObjectSpace with the specified connection.
 
Method Summary
 void addConnection(Connection connection)
          Allows the remote end of the specified connection to access objects registered in this ObjectSpace.
 void close()
          Causes this ObjectSpace to stop listening to the connections for method invocation messages.
static RemoteObject getRemoteObject(Connection connection, int objectID, java.lang.Class... ifaces)
          Returns a proxy object that implements the specified interfaces.
static
<T> T
getRemoteObject(Connection connection, int objectID, java.lang.Class<T> iface)
          Identical to getRemoteObject(Connection, int, Class...) except returns the object cast to the specified interface type.
 void register(int objectID, java.lang.Object object)
          Registers an object to allow the remote end of the ObjectSpace's connections to access it using the specified ID.
static void registerClasses(com.esotericsoftware.kryo.Kryo kryo)
          Registers the classes needed to use ObjectSpaces.
 void removeConnection(Connection connection)
          Removes the specified connection, it will no longer be able to access objects registered in this ObjectSpace.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectSpace

public ObjectSpace()
Creates an ObjectSpace with no connections. Connections must be added to allow the remote end of the connections to access objects in this ObjectSpace.


ObjectSpace

public ObjectSpace(Connection connection)
Creates an ObjectSpace with the specified connection. More connections can be added.

Method Detail

register

public void register(int objectID,
                     java.lang.Object object)
Registers an object to allow the remote end of the ObjectSpace's connections to access it using the specified ID.

If a connection is added to multiple ObjectSpaces, the same object ID should not be registered in more than one of those ObjectSpaces.

See Also:
getRemoteObject(Connection, int, Class...)

close

public void close()
Causes this ObjectSpace to stop listening to the connections for method invocation messages.


addConnection

public void addConnection(Connection connection)
Allows the remote end of the specified connection to access objects registered in this ObjectSpace.


removeConnection

public void removeConnection(Connection connection)
Removes the specified connection, it will no longer be able to access objects registered in this ObjectSpace.


getRemoteObject

public static <T> T getRemoteObject(Connection connection,
                                    int objectID,
                                    java.lang.Class<T> iface)
Identical to getRemoteObject(Connection, int, Class...) except returns the object cast to the specified interface type. The returned object still implements RemoteObject.


getRemoteObject

public static RemoteObject getRemoteObject(Connection connection,
                                           int objectID,
                                           java.lang.Class... ifaces)
Returns a proxy object that implements the specified interfaces. Methods invoked on the proxy object will be invoked remotely on the object with the specified ID in the ObjectSpace for the specified connection. If the remote end of the connection has not added the connection to the ObjectSpace, the remote method invocations will be ignored.

Methods that return a value will throw TimeoutException if the response is not received with the response timeout.

If non-blocking is false (the default), then methods that return a value must not be called from the update thread for the connection. An exception will be thrown if this occurs. Methods with a void return value can be called on the update thread.

If a proxy returned from this method is part of an object graph sent over the network, the object graph on the receiving side will have the proxy object replaced with the registered object.

See Also:
RemoteObject

registerClasses

public static void registerClasses(com.esotericsoftware.kryo.Kryo kryo)
Registers the classes needed to use ObjectSpaces. This should be called before any connections are opened.

See Also:
EndPoint.getKryo(), Kryo.register(Class, Serializer)