パッケージ org.piax.gtrans

クラス RPCInvoker<T extends RPCIf,​E extends Endpoint>

java.lang.Object
org.piax.gtrans.RPCInvoker<T,​E>
すべての実装されたインタフェース:
RPCIf
直系の既知のサブクラス:
FloodingNode, NodeManager, RingManager, SkipGraph

public class RPCInvoker<T extends RPCIf,​E extends Endpoint>
extends Object
implements RPCIf

ChannelTransportを指定して、そのTransport上でRPCを行うためのクラスである。

 RPCは、以下のようにして行う。

 int n = rpcInvoker.getStub(AppIf.class,targetObjectId,targetEndpoint).appMethod(arg1,arg2);
 

ここで、AppIfは、対象のオブジェクトが実装しているインターフェースである。 AppIfは、RPCIfを継承していなければいけない。targetObjectIdは、対象のObjectIdである。 targetEndpointは、対象のオブジェクトが存在するピアを表すEndpointである。Endpointの実際の型は、 このクラスの型パラメータEが示す型でなければいけない。対象は、対象の存在するピアの対応する RPCInvokerに対して、registerRPCObjectを使用して登録されていなければならない。 appMethodは、対象が持つメソッドであり、arg1, arg2は、appMethodの引数であるものとする。 appMethodの返り値の型はintであるとする。 appMethodは、AppIfあるいは、 そのスーパー・インターフェースで宣言されていなければいけない。さらに、対象が呼び出し側と異なるピアに存在する場合は @RemoteCallableアノテーションがついていて、かつRPCExceptionをスローするように宣言されていなければいけない。 例えば、以下の通りである。

 import org.piax.gtrans.RemoteCallable;
 
 interface AppIf extends RPCIf {
   @RemoteCallable
   int appMethod(int a1, String a2) throws RPCException;
 }
 
引数のコピー RPCでは、引数は、一般にはコピーされるが、保証されない。呼び出されたメソッド内で引数のオブジェクトを変更した場合、 それが呼び出し側に反映されるかどうかは不明であるので注意が必要である。 呼び出された側では、変更しない方が無難である。

oneway RPC

呼び出したメソッドの終了を待つ通常の同期型RPCの他に、終了を待たないoneway RPCが存在する。 oneway RPCでは、呼び出したメソッドの返り値を受け取ることはできない。 @RemoteCallable アノテーションにType.ONEWAYの引数を指定すると デフォルトではONEWAY RPCとして呼ばれる。 oneway RPCを指定しても実際に、非同期になるかどうかは、 使用しているトランスポートに依存するので、注意が必要である。

動的RPC

RPCは原則として対象のインターフェースを指定して行うが 例外的に対象のインターフェースが、実行時まで不明な場合がある。 例えば、デバッグなどで実行時に人間が、メソッドや引数を指定する場合である。 この場合は、人間はインターフェースを知っているが、呼び出し側のプログラムには インターフェースが存在しないことがある。 このような場合のために、動的な呼び出しを用意している。 以下のように使用する。
 try {
     Object r = rpcInvoker.rcall(targetObjectId,targetEndpoint,"appMethod",arg1,arg2);
 } catch (Throwable t) {
     // 例外処理
 }
 
返り値は常にObject型となる。intなど基本型はボクシングが行われてInteger型などになる。 例外は、何が発生するか不明なので、Throwableを受けなければいけない。

その他

getStubには、いくつかバリエーションが存在する。 timeoutを指定できるもの、RPCModeを指定できるもの、 targetObjectId,targetEndpointの代わりにCallerrIdを指定するものが存在する。 timeoutが指定できないものでは、GTransConfigValues.rpcTimeoutが使用される。 RPCModeを指定できるものでは、指定によりoneway RPCかどうかの決定方法を 以下のように変更可能である。
  • AUTOならば、Annotationにより決定する(デフォルト)
  • SYNCならば、常に同期型である
  • ONEWAYならば、常にOnewayである
詳細は、各メソッドの説明を見て欲しい。
  • フィールド詳細

  • コンストラクタの詳細

  • メソッドの詳細

    • getSrcPeerId

      public PeerId getSrcPeerId()
      RPCで呼ばれた際の呼び出し側を返す。 同期的local呼び出しでは、変更されないことに注意が 必要である。一度も非同期呼び出しもremote呼び出しも されていない場合はnullを返す。
      戻り値:
      呼び出し側のPeerId
    • createObjId

      public static ObjectId createObjId​(ChannelTransport<?> trans, ObjectId rpcId)
    • fin

      public void fin()
    • checkActive

      protected void checkActive() throws IllegalStateException
      RPCInvokerオブジェクトがアクティブな状態であるかどうかをチェックする。 fin() が呼ばれてインアクティブな状態である場合は、IllegalStateExceptionがthrowされる。 サブクラスの場合も含め、fin() の後に呼び出されては困る場合のメソッド呼び出しの際のチェックに用いる。
      例外:
      IllegalStateException - RPCInvokerオブジェクトがインアクティブな状態である場合
    • changeTransport

      public void changeTransport​(ChannelTransport<?> trans)
    • registerRPCObject

      public void registerRPCObject​(ObjectId objId, RPCIf obj) throws IdConflictException
      RPCの対象となるオブジェクトを登録する。 RPCで呼び出されるオブジェクトは予め登録する必要がある。
      パラメータ:
      objId - 対象のObjectId
      obj - 対象オブジェクト
      例外:
      IdConflictException - thrown if the object ID is already used.
    • unregisterRPCObject

      public boolean unregisterRPCObject​(ObjectId objId, RPCIf obj)
      RPC対象から抹消する。
      パラメータ:
      objId - 対象のObjectId
      obj - 対象オブジェクト
      戻り値:
      削除したらtrue、もともと登録されていない場合はfalseを返す。
    • getRPCObject

      public RPCIf getRPCObject​(ObjectId objId)
    • online

      @Deprecated public void online()
      推奨されていません。
    • offline

      @Deprecated public void offline()
      推奨されていません。
    • isOnline

      @Deprecated public boolean isOnline()
      推奨されていません。
    • getTransport

      public ChannelTransport<E> getTransport()
    • getEndpoint

      public E getEndpoint()
    • getStub

      public T getStub​(E remotePeer)
      リモートピア上のこのオブジェクトに対応するRPCInvokerオブジェクトのメソッドを 呼び出すためのstubを返す。
      パラメータ:
      remotePeer - リモートピアを示すEndpoint
      戻り値:
      RPCのためのstub
    • getStub

      public T getStub​(E remotePeer, int timeout)
      リモートピア上のこのオブジェクトに対応するRPCInvokerオブジェクトのメソッドを呼び出すためのstubを返す。
      パラメータ:
      remotePeer - リモートピアを示すEndpoint
      timeout - timeout値(msec)
      戻り値:
      RPCのためのstub
    • getStub

      public <S extends RPCIf> S getStub​(Class<S> clz, ObjectId targetId, E remotePeer, int timeout, RPCMode rpcMode)
      リモートピア上の、clz型のインターフェースを実装し、 targetIdを持つオブジェクトのメソッドを呼び出すためのstubを返す。
      型パラメータ:
      S - A type of RPCIf
      パラメータ:
      clz - RPC呼び出しの対象となるオブジェクトが実装しているインターフェース
      targetId - RPC呼び出しの対象となるオブジェクトのId
      remotePeer - リモートピアを示すEndpoint
      timeout - timeout値(msec)
      rpcMode - Onewayかどうかを指定する。     AUTOならば、Annotationにより決定する。 SYNCならば、常に同期型である。 ONEWAYならば、常にOnewayである。
      戻り値:
      RPCのためのstub
    • getStub

      public <S extends RPCIf> S getStub​(Class<S> clz, ObjectId targetId, E remotePeer, RPCMode rpcMode)
      リモートピア上の、clz型のインターフェースを実装し、 targetIdを持つオブジェクトのメソッドを呼び出すためのstubを返す。
      型パラメータ:
      S - A type of RPCIf
      パラメータ:
      clz - RPC呼び出しの対象となるオブジェクトが実装しているインターフェース
      targetId - RPC呼び出しの対象となるオブジェクトのId
      remotePeer - リモートピアを示すEndpoint
      rpcMode - Onewayかどうかを指定する。     AUTOならば、Annotationにより決定する。 SYNCならば、常に同期型である。 ONEWAYならば、常にOnewayである。
      戻り値:
      RPCのためのstub
    • getStub

      public <S extends RPCIf> S getStub​(Class<S> clz, ObjectId targetId, E remotePeer, int timeout)
      リモートピア上の、clz型のインターフェースを持ち、 targetIdを持つオブジェクトのメソッドを呼び出すためのstubを返す。
      型パラメータ:
      S - A type of RPCIf
      パラメータ:
      clz - RPC呼び出しの対象となるオブジェクトの型
      targetId - RPC呼び出しの対象となるオブジェクトのId
      remotePeer - リモートピアを示すEndpoint
      timeout - timeout値(msec)
      戻り値:
      RPCのためのstub
    • getStub

      public <S extends RPCIf> S getStub​(Class<S> clz, ObjectId targetId, E remotePeer)
      リモートピア上の、clz型のインターフェースを持ち、 targetIdを持つオブジェクトのメソッドを呼び出すためのstubを返す。
      型パラメータ:
      S - A type of RPCIf
      パラメータ:
      clz - RPC呼び出しの対象となるオブジェクトの型
      targetId - RPC呼び出しの対象となるオブジェクトのId
      remotePeer - リモートピアを示すEndpoint
      戻り値:
      RPCのためのstub
    • getStub

      public <S extends RPCIf> S getStub​(Class<S> clz, CalleeId cid, int timeout, RPCMode rpcMode)
      リモートピア上の、clz型のインターフェースを実装し、 cidで指定されるオブジェクトのメソッドを呼び出すためのstubを返す。
      型パラメータ:
      S - A type of RPCIf
      パラメータ:
      clz - RPC呼び出しの対象となるオブジェクトが実装しているインターフェース
      cid - RPC呼び出しの対象となるオブジェクトのId
      timeout - timeout値(msec)
      rpcMode - Onewayかどうかを指定する。     AUTOならば、Annotationにより決定する。 SYNCならば、常に同期型である。 ONEWAYならば、常にOnewayである。
      戻り値:
      RPCのためのstub
    • getStub

      public <S extends RPCIf> S getStub​(Class<S> clz, CalleeId cid, RPCMode rpcMode)
      リモートピア上の、clz型のインターフェースを実装し、 cidで指定されるオブジェクトのメソッドを呼び出すためのstubを返す。
      型パラメータ:
      S - A type of RPCIf
      パラメータ:
      clz - RPC呼び出しの対象となるオブジェクトが実装しているインターフェース
      cid - RPC呼び出しの対象となるオブジェクトのId
      rpcMode - Onewayかどうかを指定する。     AUTOならば、Annotationにより決定する。 SYNCならば、常に同期型である。 ONEWAYならば、常にOnewayである。
      戻り値:
      RPCのためのstub
    • getStub

      public <S extends RPCIf> S getStub​(Class<S> clz, CalleeId cid, int timeout)
      リモートピア上の、clz型のインターフェースを持ち、 cidで指定される オブジェクトのメソッドを呼び出すためのstubを返す。
      型パラメータ:
      S - A type of RPCIf
      パラメータ:
      clz - RPC呼び出しの対象となるオブジェクトの型
      cid - the id of the callee.
      timeout - timeout値(msec)
      戻り値:
      RPCのためのstub
    • getStub

      public <S extends RPCIf> S getStub​(Class<S> clz, CalleeId cid)
      リモートピア上の、clz型のインターフェースをを持ち、 cidで指定される オブジェクトのメソッドを呼び出すためのstubを返す。
      型パラメータ:
      S - A type of RPCIf
      パラメータ:
      clz - RPC呼び出しの対象となるオブジェクトの型
      cid - the id of the callee.
      戻り値:
      RPCのためのstub
    • rcall

      public Object rcall​(ObjectId targetId, E remotePeer, int timeout, RPCMode rpcMode, String method, Object... args) throws Throwable
      targetIdを持つオブジェクトのメソッドを動的に呼び出す。
      パラメータ:
      targetId - RPC呼び出しの対象となるオブジェクトのId
      remotePeer - リモートピアを示すEndpoint
      timeout - timeout値(msec)
      rpcMode - Onewayかどうかを指定する。     AUTOならば、Annotationにより決定する。 SYNCならば、常に同期型である。 ONEWAYならば、常にOnewayである。
      method - the name of the method.
      args - the arguments for the method.
      戻り値:
      RPCのためのstub
      例外:
      Throwable - exceptions occurred while RPC.
    • rcall

      public Object rcall​(ObjectId targetId, E remotePeer, RPCMode rpcMode, String method, Object... args) throws Throwable
      リモートピア上の、clz型のインターフェースを実装し、 targetIdを持つオブジェクトのメソッドを動的に呼び出す。
      パラメータ:
      targetId - RPC呼び出しの対象となるオブジェクトのId
      remotePeer - リモートピアを示すEndpoint
      rpcMode - Onewayかどうかを指定する。     AUTOならば、Annotationにより決定する。 SYNCならば、常に同期型である。 ONEWAYならば、常にOnewayである。
      method - the name of the method.
      args - the arguments for the method.
      戻り値:
      RPCのためのstub
      例外:
      Throwable - exceptions occurred while RPC.
    • rcall

      public Object rcall​(ObjectId targetId, E remotePeer, int timeout, String method, Object... args) throws Throwable
      リモートピア上の、clz型のインターフェースを実装し、 targetIdを持つオブジェクトのメソッドを動的に呼び出す。
      パラメータ:
      targetId - RPC呼び出しの対象となるオブジェクトのId
      remotePeer - リモートピアを示すEndpoint
      timeout - timeout値(msec)
      method - the name of the method.
      args - the arguments for the method.
      戻り値:
      RPCのためのstub
      例外:
      Throwable - exceptions occurred while RPC.
    • rcall

      public Object rcall​(ObjectId targetId, E remotePeer, String method, Object... args) throws Throwable
      リモートピア上の、clz型のインターフェースを実装し、 targetIdを持つオブジェクトのメソッドを動的に呼び出す。
      パラメータ:
      targetId - RPC呼び出しの対象となるオブジェクトのId
      remotePeer - リモートピアを示すEndpoint
      method - the name of the method.
      args - the arguments for the method.
      戻り値:
      RPCのためのstub
      例外:
      Throwable - exceptions occurred while RPC.
    • rcall

      public Object rcall​(CalleeId cid, int timeout, RPCMode rpcMode, String method, Object... args) throws Throwable
      cidで指定されるオブジェクトのメソッドを動的に呼び出す。
      パラメータ:
      cid - the id of the callee.
      timeout - timeout値(msec)
      rpcMode - Onewayかどうかを指定する。     AUTOならば、Annotationにより決定する。 SYNCならば、常に同期型である。 ONEWAYならば、常にOnewayである。
      method - the name of the method.
      args - the arguments for the method.
      戻り値:
      RPCのためのstub
      例外:
      Throwable - exceptions occurred while RPC.
    • rcall

      public Object rcall​(CalleeId cid, RPCMode rpcMode, String method, Object... args) throws Throwable
      cidで指定されるオブジェクトのメソッドを動的に呼び出す。
      パラメータ:
      cid - the id of the callee.
      rpcMode - Onewayかどうかを指定する。     AUTOならば、Annotationにより決定する。 SYNCならば、常に同期型である。 ONEWAYならば、常にOnewayである。
      method - the name of the method.
      args - the arguments for the method.
      戻り値:
      RPCのためのstub
      例外:
      Throwable - exceptions occurred while RPC.
    • rcall

      public Object rcall​(CalleeId cid, int timeout, String method, Object... args) throws Throwable
      リモートピア上の、clz型のインターフェースを実装し、 cidで指定されるオブジェクトのメソッドを動的に呼び出す。
      パラメータ:
      cid - the id of the callee.
      timeout - timeout値(msec)
      method - the name of the method.
      args - the arguments for the method.
      戻り値:
      RPCのためのstub
      例外:
      Throwable - exceptions occurred while RPC.
    • rcall

      public Object rcall​(CalleeId cid, String method, Object... args) throws Throwable
      Call a remote method on the object specified by cid.
      パラメータ:
      cid - the id of the callee.
      method - the name of the method.
      args - the arguments for the method.
      戻り値:
      a stub for RPC
      例外:
      Throwable - exceptions occurred while RPC.
    • changeRPCTimeout

      public void changeRPCTimeout​(RPCIf stub, int timeout) throws IllegalArgumentException
      例外:
      IllegalArgumentException
    • newMethodCall

      protected RPCInvoker.MethodCall newMethodCall​(ObjectId target, E remotePeer, boolean oneway, String method, Object... args)
    • sendOnewayInvoke

      public void sendOnewayInvoke​(ObjectId target, E remotePeer, String method, Object... args) throws RPCException
      send an oneway RPC invocation message.
      パラメータ:
      target - the target object ID.
      remotePeer - the endpoint of the remote peer.
      method - the name of the method.
      args - the arguments of the method.
      例外:
      RPCException - an exception occurred while the RPC.
    • sendInvoke

      public ReturnValue<?> sendInvoke​(ObjectId target, E remotePeer, int timeout, String method, Object... args) throws RPCException
      send an oneway RPC invocation message.
      パラメータ:
      target - the target object ID.
      remotePeer - the endpoint of the remote peer.
      timeout - the timeout.
      method - the name of the method.
      args - the arguments of the method.
      戻り値:
      the return value object of the invocation.
      例外:
      RPCException - an exception occurred while the RPC.
    • invokeInReceive

      protected Object invokeInReceive​(boolean isOneway, RPCIf obj, RPCInvoker.MethodCall mc) throws NoSuchMethodException, InvocationTargetException
      例外:
      NoSuchMethodException
      InvocationTargetException
    • receiveOneway

      protected void receiveOneway​(RPCInvoker.MethodCall mc)
    • receiveSync

      protected ReturnValue<?> receiveSync​(RPCInvoker.MethodCall mc)