Package edu.jas.util
Class ChannelFactory
- java.lang.Object
-
- java.lang.Thread
-
- edu.jas.util.ChannelFactory
-
- All Implemented Interfaces:
java.lang.Runnable
public class ChannelFactory extends java.lang.Thread
ChannelFactory implements a symmetric and non blocking way of setting up sockets on the client and server side. The constructor sets up a ServerSocket and accepts and stores any Socket creation requests from clients. The created Sockets can the be retrieved from the store without blocking. Refactored for java.util.concurrent.- See Also:
SocketChannel
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.BlockingQueue<SocketChannel>
buf
BoundedBuffer for sockets.private static boolean
debug
static int
DEFAULT_PORT
default port of socket.private static org.apache.logging.log4j.Logger
logger
private int
port
port of socket.private java.net.ServerSocket
srv
local server socket.private boolean
srvrun
is local server up and running.private boolean
srvstart
is thread started.
-
Constructor Summary
Constructors Constructor Description ChannelFactory()
Constructs a ChannelFactory on the DEFAULT_PORT.ChannelFactory(int p)
Constructs a ChannelFactory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SocketChannel
getChannel()
Get a new socket channel from a server socket.SocketChannel
getChannel(java.lang.String h)
Get a new socket channel to a given host.SocketChannel
getChannel(java.lang.String h, int p)
Get a new socket channel to a given host.void
init()
thread initialization and start.void
run()
Run the servers accept() in an infinite loop.void
terminate()
Terminate the Channel Factoryjava.lang.String
toString()
toString.-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, yield
-
-
-
-
Field Detail
-
logger
private static final org.apache.logging.log4j.Logger logger
-
debug
private static final boolean debug
-
DEFAULT_PORT
public static final int DEFAULT_PORT
default port of socket.- See Also:
- Constant Field Values
-
port
private final int port
port of socket.
-
buf
private final java.util.concurrent.BlockingQueue<SocketChannel> buf
BoundedBuffer for sockets.
-
srv
private volatile java.net.ServerSocket srv
local server socket.
-
srvrun
private volatile boolean srvrun
is local server up and running.
-
srvstart
private volatile boolean srvstart
is thread started.
-
-
Method Detail
-
toString
public java.lang.String toString()
toString.- Overrides:
toString
in classjava.lang.Thread
-
init
public void init()
thread initialization and start.
-
getChannel
public SocketChannel getChannel() throws java.lang.InterruptedException
Get a new socket channel from a server socket.- Throws:
java.lang.InterruptedException
-
getChannel
public SocketChannel getChannel(java.lang.String h) throws java.io.IOException
Get a new socket channel to a given host.- Parameters:
h
- hostname- Throws:
java.io.IOException
-
getChannel
public SocketChannel getChannel(java.lang.String h, int p) throws java.io.IOException
Get a new socket channel to a given host.- Parameters:
h
- hostnamep
- port- Throws:
java.io.IOException
-
run
public void run()
Run the servers accept() in an infinite loop.- Specified by:
run
in interfacejava.lang.Runnable
- Overrides:
run
in classjava.lang.Thread
-
terminate
public void terminate()
Terminate the Channel Factory
-
-