Class Link<L,​R>

  • Type Parameters:
    L - The type of the local server
    R - The type of the remote server
    All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Runnable

    public class Link<L,​R>
    extends java.lang.Thread
    implements java.io.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.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Field Summary

      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Constructor Summary

      Constructors 
      Constructor Description
      Link​(java.lang.Class<R> remoteType, java.io.DataInputStream in, java.io.DataOutputStream out, java.util.concurrent.Executor es)
      Create a new link based on an Data in/output stream.
      Link​(java.lang.Class<R> remoteType, java.io.DataInputStream in, java.io.DataOutputStream out, java.util.concurrent.Executor es, java.net.Socket socket)
      Create a new link based on an Data in/output stream.
      Link​(java.lang.Class<R> remoteType, java.io.InputStream in, java.io.OutputStream out, java.util.concurrent.Executor es)
      Create a new link based on an in/output stream.
      Link​(java.lang.Class<R> type, java.net.Socket socket, java.util.concurrent.Executor es)
      Create a new link based on a socket.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close this link.
      java.io.DataInputStream getInput()
      Get the input stream
      java.io.DataOutputStream getOutput()
      Get the output stream
      R getRemote()
      Get the proxy to the remote peer.
      java.util.Optional<java.net.Socket> getSocket()
      Some links are connected over sockets.
      boolean isOpen()
      Answer if this link is open
      void open​(L local)
      Open the stream by providing the local interface to use
      void run()
      The thread method that receives the messages from the input stream
      static <L,​R>
      java.io.Closeable
      server​(java.lang.String name, java.lang.Class<R> type, int port, java.lang.String host, java.util.function.Function<Link<L,​R>,​L> local, boolean localOnly, java.util.concurrent.ExecutorService es)
      Create a server.
      static <L,​R>
      java.io.Closeable
      server​(java.lang.String name, java.lang.Class<R> type, java.net.ServerSocket server, java.util.function.Function<Link<L,​R>,​L> local, boolean localOnly, java.util.concurrent.Executor es)
      Create a server.
      void setRemote​(java.lang.Object remote)
      Change the object used for the remote.
      protected void terminate​(java.lang.Exception t)  
      void transfer​(java.lang.Object result)
      Transfer the link to another and close this link object
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Link

        public Link​(java.lang.Class<R> remoteType,
                    java.io.InputStream in,
                    java.io.OutputStream out,
                    java.util.concurrent.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​(java.lang.Class<R> remoteType,
                    java.io.DataInputStream in,
                    java.io.DataOutputStream out,
                    java.util.concurrent.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​(java.lang.Class<R> remoteType,
                    java.io.DataInputStream in,
                    java.io.DataOutputStream out,
                    java.util.concurrent.Executor es,
                    java.net.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​(java.lang.Class<R> type,
                    java.net.Socket socket,
                    java.util.concurrent.Executor es)
             throws java.io.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:
        java.io.IOException
    • Method Detail

      • 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 java.io.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 java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.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 java.lang.Runnable
        Overrides:
        run in class java.lang.Thread
      • server

        public static <L,​R> java.io.Closeable server​(java.lang.String name,
                                                           java.lang.Class<R> type,
                                                           int port,
                                                           java.lang.String host,
                                                           java.util.function.Function<Link<L,​R>,​L> local,
                                                           boolean localOnly,
                                                           java.util.concurrent.ExecutorService es)
                                                    throws java.io.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:
        java.io.IOException
      • server

        public static <L,​R> java.io.Closeable server​(java.lang.String name,
                                                           java.lang.Class<R> type,
                                                           java.net.ServerSocket server,
                                                           java.util.function.Function<Link<L,​R>,​L> local,
                                                           boolean localOnly,
                                                           java.util.concurrent.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 java.io.DataOutputStream getOutput()
        Get the output stream
        Returns:
        the output stream
      • getInput

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

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

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

        protected void terminate​(java.lang.Exception t)
      • getSocket

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