Simple TCP Server
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Optional;
import java.util.function.Consumer;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class SimpleTcpServer {
protected static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
protected volatile int throughput = 3;
protected volatile int port = Short.MAX_VALUE;
protected volatile boolean closed = false;
protected volatile SocketAddress endpoint;
protected volatile IoAcceptor acceptor;
public TcpServer(int port) {
this.port = port;
this.endpoint = new InetSocketAddress(port);
}
public TcpServer(int port, int throughput) {
this.throughput = throughput;
this.port = port;
this.endpoint = new InetSocketAddress(port);
}
public void stop() {
this.closed = true;
Optional.ofNullable(this.acceptor).ifPresent(x -> { x.unbind(); x.dispose(); });
}
public void start(Consumer<IoAcceptor> consumer) throws Exception {
this.closed = false;
this.acceptor = new NioSocketAcceptor(CPU_COUNT + 1);
this.acceptor.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, 30);
this.acceptor.getSessionConfig().setThroughputCalculationInterval(this.throughput);
Optional.ofNullable(consumer).ifPresent(x -> x.accept(this.acceptor));
doBind();
}
protected void doBind() {
if (this.closed) {
return;
}
while (!this.closed) {
try {
this.acceptor.bind(this.endpoint);
return;
} catch (Exception ex) {
ex.printStackTrace();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
Simple TCP Client
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class SimpleTcpClient {
protected volatile String addr = "127.0.0.1";
protected volatile int port = Short.MAX_VALUE;
protected volatile boolean closed = false;
protected volatile SocketAddress endpoint;
protected volatile NioSocketConnector connector;
protected volatile IoSession session;
public TcpClient(String addr, int port) {
this.addr = addr;
this.port = port;
this.endpoint = new InetSocketAddress(addr, port);
}
public void stop() {
this.closed = true;
Optional.ofNullable(this.session).ifPresent(x -> x.closeNow());
Optional.ofNullable(this.connector).ifPresent(x -> x.dispose());
}
public void start(Consumer<NioSocketConnector> consumer) throws Exception {
this.closed = false;
this.connector = new NioSocketConnector();
this.connector.setConnectTimeoutMillis(3000);
this.connector.getSessionConfig().setReuseAddress(true);
this.connector.getSessionConfig().setKeepAlive(true);
this.connector.getSessionConfig().setTcpNoDelay(true);
this.connector.getSessionConfig().setSoLinger(0);
this.connector.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, 30);
this.connector.getFilterChain().addFirst("reconnection", new IoFilterAdapter() {
@Override
public void sessionClosed(NextFilter nextFilter, IoSession ioSession) throws Exception {
CompletableFuture.runAsync(() -> doConnect());
}
});
Optional.ofNullable(consumer).ifPresent(x -> x.accept(this.connector));
doConnect();
}
protected void doConnect() {
if (this.closed) {
return;
}
this.connector.connect(this.endpoint).addListener(new IoFutureListener<ConnectFuture>() {
@Override
public void operationComplete(ConnectFuture future) {
if (!future.isConnected()) {
CompletableFuture.runAsync(() -> doConnect());
} else {
session = future.getSession();
}
}
});
}
public void sent(Object data) {
Optional.ofNullable(this.session)
.filter(x -> x.isActive())
.ifPresent(x -> x.write(data));
}
}