Java - Develop Network Applications with Apache MINA
By admin on Feb 12, 2008 in Java, open source
To develop clients for FTP, Telnet, SMTP, POP3, etc, I normally use Apache Commons Net, and to develop HTTP based client I use Commons HttpClient, and to develop network servers in various protocols I used Apache MINA.
As quoted from the web site, Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract · event-driven · asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.
Example as provided to develop a proxy server is very straightforward.
AbstractProxyIoHandler
public abstract class AbstractProxyIoHandler
extends IoHandlerAdapter {
private static Charset CHARSET = Charset.forName("iso8859-1");
public void sessionCreated(IoSession session) throws Exception {
session.setTrafficMask(TrafficMask.NONE);
}
public void sessionClosed(IoSession session) throws Exception {
if (session.getAttachment() != null) {
((IoSession) session.getAttachment()).
setAttachment(null);
((IoSession) session.getAttachment()).close();
session.setAttachment(null);
}
}
public void messageReceived(IoSession session, Object message)
throws Exception {
ByteBuffer rb = (ByteBuffer) message;
ByteBuffer wb = ByteBuffer.allocate(rb.remaining());
rb.mark();
wb.put(rb);
wb.flip();
((IoSession) session.getAttachment()).write(wb);
rb.reset();
SessionLog.info(session, rb.getString(
CHARSET.newDecoder()));
}
}
ServerToProxyIoHandler
public class ServerToProxyIoHandler
extends AbstractProxyIoHandler {
}
ClientToProxyIoHandler
public class ClientToProxyIoHandler
extends AbstractProxyIoHandler {
private final ServerToProxyIoHandler connectorHandler;
private final IoConnector connector;
private final InetSocketAddress address;
public ClientToProxyIoHandler(
ServerToProxyIoHandler connectorHandler,
IoConnector connector, InetSocketAddress address) {
this.connectorHandler = connectorHandler;
this.connector = connector;
this.address = address;
}
public void sessionOpened(final IoSession session)
throws Exception {
connector.connect(address, connectorHandler).
addListener(
new IoFutureListener() {
public void operationComplete(IoFuture f) {
ConnectFuture future = (ConnectFuture) f;
try {
future.getSession().
setAttachment(session);
session.
setAttachment(future.getSession());
future.getSession().
setTrafficMask(TrafficMask.ALL);
} catch (RuntimeIOException e) {
// Connect failed
session.close();
} finally {
session.setTrafficMask(TrafficMask.ALL);
}
}
});
}
}
Main
public class Main {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println(Main.class.getName()
+ " <proxy-port> <server-hostname>
<server-port>");
return;
}
// Create TCP/IP acceptor.
IoAcceptor acceptor = new SocketAcceptor();
((SocketAcceptorConfig) acceptor.getDefaultConfig())
.setReuseAddress(true);
// Create TCP/IP connector.
IoConnector connector = new SocketConnector();
// Set connect timeout.
((IoConnectorConfig) connector.getDefaultConfig())
.setConnectTimeout(30);
ClientToProxyIoHandler handler =
new ClientToProxyIoHandler(
new ServerToProxyIoHandler(),
connector, new InetSocketAddress(
args[1], Integer.parseInt(args[2])));
// Start proxy.
acceptor
.bind(new InetSocketAddress(
Integer.parseInt(args[0])), handler);
System.out.println("Listening on port " +
Integer.parseInt(args[0]));
}
}

