Package aQute.lib.link
Class Link<L,R>
- java.lang.Object
-
- java.lang.Thread
-
- aQute.lib.link.Link<L,R>
-
- Type Parameters:
L
- The type of the local serverR
- 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.
-
-
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 streamjava.io.DataOutputStream
getOutput()
Get the output streamR
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 openvoid
open(L local)
Open the stream by providing the local interface to usevoid
run()
The thread method that receives the messages from the input streamstatic <L,R>
java.io.Closeableserver(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.Closeableserver(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
-
-
-
-
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 typein
- the incoming messages streamout
- 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 typein
- the incoming messages streamout
- 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 typein
- the incoming messages streamout
- where messages are send tosocket
- 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 remotesocket
- 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 interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.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 interfacejava.lang.Runnable
- Overrides:
run
in classjava.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 servertype
- the remote typeport
- the local porthost
- on which host to registerlocal
- the local's peer interfacelocalOnly
- 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 servertype
- the remote typeserver
- the Socket Serverlocal
- the local's peer interfacelocalOnly
- 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.
-
-