Class Link<L,R>

java.lang.Object
java.lang.Thread
aQute.lib.link.Link<L,R>
Type Parameters:
L - The type of the local server
R - The type of the remote server
All Implemented Interfaces:
Closeable, AutoCloseable, Runnable

public class Link<L,R> extends Thread implements Closeable
This is a simple RPC module that has a R(remote) and L(ocal) interface. The R interface is implemented on the remote side. The methods on this subclass are then available remotely. I.e. this is a two way street. Void messages are asynchronous, other messages block to a reply.
  • Field Details

  • Constructor Details

    • Link

      public Link(Class<R> remoteType, InputStream in, OutputStream out, Executor es)
      Create a new link based on an in/output stream. This link is still closed. Call open to activate the link.
      Parameters:
      remoteType - the remote type
      in - the incoming messages stream
      out - where messages are send to
    • Link

      public Link(Class<R> remoteType, DataInputStream in, DataOutputStream out, Executor es)
      Create a new link based on an Data in/output stream. This link is still closed. Call open to activate the link.
      Parameters:
      remoteType - the remote type
      in - the incoming messages stream
      out - where messages are send to
    • Link

      public Link(Class<R> remoteType, DataInputStream in, DataOutputStream out, Executor es, Socket socket)
      Create a new link based on an Data in/output stream. This link is still closed. Call open to activate the link.
      Parameters:
      remoteType - the remote type
      in - the incoming messages stream
      out - where messages are send to
      socket - An optional socket
    • Link

      public Link(Class<R> type, Socket socket, Executor es) throws IOException
      Create a new link based on a socket. This link is still closed. Call open to activate the link.
      Parameters:
      type - the type of the remote
      socket - the socket
      Throws:
      IOException
  • Method Details

    • open

      public void open(L local)
      Open the stream by providing the local interface to use
      Parameters:
      local - the local server
    • close

      public void close() throws IOException
      Close this link. This will also close the peer link. If local implements Closeable then the local server will also be notified by calling close. Since we also close the remote peer link we also try to call close on the remote peer.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getRemote

      public R getRemote()
      Get the proxy to the remote peer.
      Returns:
      the remote peer
    • run

      public void run()
      The thread method that receives the messages from the input stream
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • server

      public static <L, R> Closeable server(String name, Class<R> type, int port, String host, Function<Link<L,R>,L> local, boolean localOnly, ExecutorService es) throws IOException
      Create a server. This server will create instances when it is contacted.
      Parameters:
      name - the name of the server
      type - the remote type
      port - the local port
      host - on which host to register
      local - the local's peer interface
      localOnly - only accept calls from the local host
      Returns:
      a closeable to close the link
      Throws:
      IOException
    • server

      public static <L, R> Closeable server(String name, Class<R> type, ServerSocket server, Function<Link<L,R>,L> local, boolean localOnly, Executor es)
      Create a server. This server will create instances when it is contacted.
      Parameters:
      name - the name of the server
      type - the remote type
      server - the Socket Server
      local - the local's peer interface
      localOnly - only accept calls from the local host
      Returns:
      a closeable to close the link
    • isOpen

      public boolean isOpen()
      Answer if this link is open
      Returns:
      true if this link is open
    • getOutput

      public DataOutputStream getOutput()
      Get the output stream
      Returns:
      the output stream
    • getInput

      public DataInputStream getInput()
      Get the input stream
      Returns:
      the input stream
    • setRemote

      public void setRemote(Object remote)
      Change the object used for the remote.
      Parameters:
      remote - peer
    • transfer

      public void transfer(Object result) throws Exception
      Transfer the link to another and close this link object
      Parameters:
      result - the result of the call that caused the transfer
      Throws:
      Exception
    • terminate

      protected void terminate(Exception t)
    • getMethod

      Method getMethod(String cmd, int count)
    • send

      int send(int msgId, Method m, Object[] args) throws Exception
      Throws:
      Exception
    • response

      void response(int msgId, byte[] data)
    • waitForResult

      <T> T waitForResult(int id, Type type) throws Exception
      Throws:
      Exception
    • trace

      private void trace(String string)
    • executeCommand

      void executeCommand(String cmd, int id, List<byte[]> args) throws Exception
      Throws:
      Exception
    • getSocket

      public Optional<Socket> getSocket()
      Some links are connected over sockets. In that case, the socket is available from here.