You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2013/03/21 09:34:40 UTC
git commit: o Injected the IoSessionConfig in the Tcp and Udp server
constructors o Moved the config to the AbstractIoServer class o Moved all the
DefaultTcpSessionConfig fields to teh top of the class
Updated Branches:
refs/heads/trunk f361cbc44 -> 638f2abd2
o Injected the IoSessionConfig in the Tcp and Udp server constructors
o Moved the config to the AbstractIoServer class
o Moved all the DefaultTcpSessionConfig fields to teh top of the class
Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/638f2abd
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/638f2abd
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/638f2abd
Branch: refs/heads/trunk
Commit: 638f2abd2f4c39e19fe4660164a5e9bd1fd8a6ae
Parents: f361cbc
Author: Emmanuel Lécharny <el...@apache.org>
Authored: Thu Mar 21 09:34:15 2013 +0100
Committer: Emmanuel Lécharny <el...@apache.org>
Committed: Thu Mar 21 09:34:15 2013 +0100
----------------------------------------------------------------------
.../mina/service/server/AbstractIoServer.java | 25 +++-
.../apache/mina/transport/nio/NioTcpServer.java | 47 ++++++-
.../apache/mina/transport/nio/NioUdpServer.java | 102 +++++++++++---
.../mina/transport/tcp/AbstractTcpServer.java | 23 ++-
.../transport/tcp/DefaultTcpSessionConfig.java | 35 ++----
.../mina/transport/udp/AbstractUdpServer.java | 35 ++++-
.../mina/examples/udpecho/NioUdpEchoServer.java | 112 ++++++---------
7 files changed, 250 insertions(+), 129 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mina/blob/638f2abd/core/src/main/java/org/apache/mina/service/server/AbstractIoServer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/service/server/AbstractIoServer.java b/core/src/main/java/org/apache/mina/service/server/AbstractIoServer.java
index db2a5fb..863e49c 100644
--- a/core/src/main/java/org/apache/mina/service/server/AbstractIoServer.java
+++ b/core/src/main/java/org/apache/mina/service/server/AbstractIoServer.java
@@ -20,6 +20,7 @@
package org.apache.mina.service.server;
import org.apache.mina.api.IoServer;
+import org.apache.mina.api.IoSessionConfig;
import org.apache.mina.service.AbstractIoService;
import org.apache.mina.service.executor.IoHandlerExecutor;
@@ -29,6 +30,9 @@ import org.apache.mina.service.executor.IoHandlerExecutor;
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public abstract class AbstractIoServer extends AbstractIoService implements IoServer {
+ /** the default session configuration */
+ protected IoSessionConfig config;
+
/**
* Create an new AbstractIoServer instance
*
@@ -36,8 +40,9 @@ public abstract class AbstractIoServer extends AbstractIoService implements IoSe
* Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
* operations.
*/
- protected AbstractIoServer(final IoHandlerExecutor eventExecutor) {
+ protected AbstractIoServer(IoSessionConfig config, IoHandlerExecutor eventExecutor) {
super(eventExecutor);
+ this.config = config;
}
// does the reuse address flag should be positioned
@@ -48,7 +53,7 @@ public abstract class AbstractIoServer extends AbstractIoService implements IoSe
*
* @param reuseAddress <code>true</code> to enable
*/
- public void setReuseAddress(final boolean reuseAddress) {
+ public void setReuseAddress(boolean reuseAddress) {
this.reuseAddress = reuseAddress;
}
@@ -61,4 +66,20 @@ public abstract class AbstractIoServer extends AbstractIoService implements IoSe
return this.reuseAddress;
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public IoSessionConfig getSessionConfig() {
+ return config;
+ }
+
+ /**
+ * Set the default configuration for created TCP sessions
+ *
+ * @param config
+ */
+ public void setSessionConfig(IoSessionConfig config) {
+ this.config = config;
+ }
}
http://git-wip-us.apache.org/repos/asf/mina/blob/638f2abd/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java b/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java
index 919b222..97601dd 100644
--- a/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java
+++ b/core/src/main/java/org/apache/mina/transport/nio/NioTcpServer.java
@@ -72,6 +72,17 @@ public class NioTcpServer extends AbstractTcpServer implements SelectorListener
}
/**
+ * Create a TCP server with new selector pool of default size and a {@link IoHandlerExecutor} of default type (
+ * {@link OrderedHandlerExecutor})
+ *
+ * @param config The specific configuration to use
+ */
+ public NioTcpServer(TcpSessionConfig config) {
+ this(config, new NioSelectorLoop("accept", 0), new FixedSelectorLoopPool("Server", Runtime.getRuntime()
+ .availableProcessors() + 1), null);
+ }
+
+ /**
* Create a TCP server with provided selector loops pool. We will use one SelectorLoop get from the pool to manage
* the OP_ACCEPT events. If the pool contains only one SelectorLoop, then all the events will be managed by the same
* Selector.
@@ -82,9 +93,22 @@ public class NioTcpServer extends AbstractTcpServer implements SelectorListener
* operations.
*/
public NioTcpServer(SelectorLoopPool selectorLoopPool, IoHandlerExecutor handlerExecutor) {
- super(handlerExecutor);
- this.acceptSelectorLoop = selectorLoopPool.getSelectorLoop();
- this.readWriteSelectorPool = selectorLoopPool;
+ this(selectorLoopPool.getSelectorLoop(), selectorLoopPool, handlerExecutor);
+ }
+
+ /**
+ * Create a TCP server with provided selector loops pool. We will use one SelectorLoop get from the pool to manage
+ * the OP_ACCEPT events. If the pool contains only one SelectorLoop, then all the events will be managed by the same
+ * Selector.
+ *
+ * @param config The specific configuration to use
+ * @param selectorLoopPool the selector loop pool for handling all I/O events (accept, read, write)
+ * @param ioHandlerExecutor used for executing IoHandler event in another pool of thread (not in the low level I/O
+ * one). Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
+ * operations.
+ */
+ public NioTcpServer(TcpSessionConfig config, SelectorLoopPool selectorLoopPool, IoHandlerExecutor handlerExecutor) {
+ this(config, selectorLoopPool.getSelectorLoop(), selectorLoopPool, handlerExecutor);
}
/**
@@ -104,6 +128,23 @@ public class NioTcpServer extends AbstractTcpServer implements SelectorListener
}
/**
+ * Create a TCP server with provided selector loops pool
+ *
+ * @param config The specific configuration to use
+ * @param acceptSelectorLoop the selector loop for handling accept events (connection of new session)
+ * @param readWriteSelectorLoop the pool of selector loop for handling read/write events of connected sessions
+ * @param ioHandlerExecutor used for executing IoHandler event in another pool of thread (not in the low level I/O
+ * one). Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
+ * operations.
+ */
+ public NioTcpServer(TcpSessionConfig config, SelectorLoop acceptSelectorLoop,
+ SelectorLoopPool readWriteSelectorLoop, IoHandlerExecutor handlerExecutor) {
+ super(config, handlerExecutor);
+ this.acceptSelectorLoop = acceptSelectorLoop;
+ this.readWriteSelectorPool = readWriteSelectorLoop;
+ }
+
+ /**
* Get the inner Server socket for accepting new client connections
*
* @return
http://git-wip-us.apache.org/repos/asf/mina/blob/638f2abd/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java b/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
index c38cb55..2366e1a 100644
--- a/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
+++ b/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
@@ -28,12 +28,12 @@ import java.nio.channels.SelectionKey;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
-import org.apache.mina.api.IoSessionConfig;
import org.apache.mina.service.executor.IoHandlerExecutor;
import org.apache.mina.service.executor.OrderedHandlerExecutor;
import org.apache.mina.service.idlechecker.IdleChecker;
import org.apache.mina.service.idlechecker.IndexedIdleChecker;
import org.apache.mina.transport.udp.AbstractUdpServer;
+import org.apache.mina.transport.udp.UdpSessionConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -50,7 +50,7 @@ public class NioUdpServer extends AbstractUdpServer implements SelectorListener
private SocketAddress address = null;
// the processor used for read and write this server
- private final NioSelectorLoop selectorLoop;
+ //private final NioSelectorLoop selectorLoop;
// used for detecting idle sessions
private final IdleChecker idleChecker = new IndexedIdleChecker();
@@ -64,38 +64,101 @@ public class NioUdpServer extends AbstractUdpServer implements SelectorListener
// list of all the sessions by remote socket address
private final Map<SocketAddress /* remote socket address */, NioUdpSession> sessions = new ConcurrentHashMap<SocketAddress, NioUdpSession>();
+ /** The selector loop used to accept incoming connection */
+ private final SelectorLoop acceptSelectorLoop;
+
+ /** The read/write selectorPool */
+ private final SelectorLoopPool readWriteSelectorPool;
+
/**
- * Create an UDP server with new selector pool of default size and a {@link IoHandlerExecutor} of default type (
+ * Create an UDP server with a new selector pool of default size and a {@link IoHandlerExecutor} of default type (
* {@link OrderedHandlerExecutor})
*/
public NioUdpServer() {
- this(new NioSelectorLoop("accept", 0), null);
+ this(new NioSelectorLoop("accept", 0), new FixedSelectorLoopPool("Server", Runtime.getRuntime()
+ .availableProcessors() + 1), null);
}
/**
- * Create a new instance of NioUdpServer
+ * Create an UDP server with a new selector pool of default size and a {@link IoHandlerExecutor} of default type (
+ * {@link OrderedHandlerExecutor})
+ *
+ * @param sessionConfig The configuration to use for this server
*/
- public NioUdpServer(NioSelectorLoop selectorLoop, IoHandlerExecutor ioHandlerExecutor) {
- super(ioHandlerExecutor);
- this.selectorLoop = selectorLoop;
+ public NioUdpServer(UdpSessionConfig config) {
+ this(config, new NioSelectorLoop("accept", 0), new FixedSelectorLoopPool("Server", Runtime.getRuntime()
+ .availableProcessors() + 1), null);
}
/**
- * Get the inner datagram channel for read and write operations. To be called by the {@link NioSelectorProcessor}
+ * Create a UDP server with provided selector loops pool. We will use one SelectorLoop get from the pool to manage
+ * the OP_ACCEPT events. If the pool contains only one SelectorLoop, then all the events will be managed by the same
+ * Selector.
*
- * @return the datagram channel bound to this {@link NioUdpServer}.
+ * @param selectorLoopPool the selector loop pool for handling all I/O events (accept, read, write)
+ * @param ioHandlerExecutor used for executing IoHandler event in another pool of thread (not in the low level I/O
+ * one). Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
+ * operations.
*/
- public DatagramChannel getDatagramChannel() {
- return datagramChannel;
+ public NioUdpServer(SelectorLoopPool selectorLoopPool, IoHandlerExecutor handlerExecutor) {
+ this(selectorLoopPool.getSelectorLoop(), selectorLoopPool, handlerExecutor);
}
/**
- * {@inheritDoc}
+ * Create a UDP server with provided selector loops pool. We will use one SelectorLoop get from the pool to manage
+ * the OP_ACCEPT events. If the pool contains only one SelectorLoop, then all the events will be managed by the same
+ * Selector.
+ *
+ * @param sessionConfig The configuration to use for this server
+ * @param selectorLoopPool the selector loop pool for handling all I/O events (accept, read, write)
+ * @param ioHandlerExecutor used for executing IoHandler event in another pool of thread (not in the low level I/O
+ * one). Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
+ * operations.
*/
- @Override
- public IoSessionConfig getSessionConfig() {
- // TODO Auto-generated method stub
- return null;
+ public NioUdpServer(UdpSessionConfig config, SelectorLoopPool selectorLoopPool, IoHandlerExecutor handlerExecutor) {
+ this(config, selectorLoopPool.getSelectorLoop(), selectorLoopPool, handlerExecutor);
+ }
+
+ /**
+ * Create an UDP server with provided selector loops pool
+ *
+ * @param acceptSelectorLoop the selector loop for handling accept events (connection of new session)
+ * @param readWriteSelectorLoop the pool of selector loop for handling read/write events of connected sessions
+ * @param ioHandlerExecutor used for executing IoHandler event in another pool of thread (not in the low level I/O
+ * one). Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
+ * operations.
+ */
+ public NioUdpServer(SelectorLoop acceptSelectorLoop, SelectorLoopPool readWriteSelectorLoop,
+ IoHandlerExecutor handlerExecutor) {
+ super(handlerExecutor);
+ this.acceptSelectorLoop = acceptSelectorLoop;
+ this.readWriteSelectorPool = readWriteSelectorLoop;
+ }
+
+ /**
+ * Create an UDP server with provided selector loops pool
+ *
+ * @param sessionConfig The configuration to use for this server
+ * @param acceptSelectorLoop the selector loop for handling accept events (connection of new session)
+ * @param readWriteSelectorLoop the pool of selector loop for handling read/write events of connected sessions
+ * @param ioHandlerExecutor used for executing IoHandler event in another pool of thread (not in the low level I/O
+ * one). Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
+ * operations.
+ */
+ public NioUdpServer(UdpSessionConfig config, SelectorLoop acceptSelectorLoop,
+ SelectorLoopPool readWriteSelectorLoop, IoHandlerExecutor handlerExecutor) {
+ super(config, handlerExecutor);
+ this.acceptSelectorLoop = acceptSelectorLoop;
+ this.readWriteSelectorPool = readWriteSelectorLoop;
+ }
+
+ /**
+ * Get the inner datagram channel for read and write operations. To be called by the {@link NioSelectorProcessor}
+ *
+ * @return the datagram channel bound to this {@link NioUdpServer}.
+ */
+ public DatagramChannel getDatagramChannel() {
+ return datagramChannel;
}
/**
@@ -138,7 +201,7 @@ public class NioUdpServer extends AbstractUdpServer implements SelectorListener
datagramChannel.socket().bind(address);
datagramChannel.configureBlocking(false);
- selectorLoop.register(false, false, true, false, this, datagramChannel, null);
+ acceptSelectorLoop.register(false, false, true, false, this, datagramChannel, null);
// it's the first address bound, let's fire the event
this.fireServiceActivated();
@@ -154,7 +217,7 @@ public class NioUdpServer extends AbstractUdpServer implements SelectorListener
throw new IllegalStateException("server not bound");
}
- selectorLoop.unregister(this, datagramChannel);
+ acceptSelectorLoop.unregister(this, datagramChannel);
datagramChannel.socket().close();
datagramChannel.close();
@@ -195,6 +258,7 @@ public class NioUdpServer extends AbstractUdpServer implements SelectorListener
// let's find the corresponding session
NioUdpSession session = sessions.get(source);
+
if (session == null) {
session = new NioUdpSession(this, idleChecker, address, source);
}
http://git-wip-us.apache.org/repos/asf/mina/blob/638f2abd/core/src/main/java/org/apache/mina/transport/tcp/AbstractTcpServer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/tcp/AbstractTcpServer.java b/core/src/main/java/org/apache/mina/transport/tcp/AbstractTcpServer.java
index 32e73a3..9935b31 100644
--- a/core/src/main/java/org/apache/mina/transport/tcp/AbstractTcpServer.java
+++ b/core/src/main/java/org/apache/mina/transport/tcp/AbstractTcpServer.java
@@ -28,8 +28,6 @@ import org.apache.mina.service.server.AbstractIoServer;
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public abstract class AbstractTcpServer extends AbstractIoServer {
- /** the default session configuration */
- private TcpSessionConfig config;
/**
* Create an new AbsractTcpServer instance
@@ -38,9 +36,20 @@ public abstract class AbstractTcpServer extends AbstractIoServer {
* Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
* operations.
*/
- protected AbstractTcpServer(final IoHandlerExecutor eventExecutor) {
- super(eventExecutor);
- this.config = new DefaultTcpSessionConfig();
+ protected AbstractTcpServer(IoHandlerExecutor eventExecutor) {
+ super(new DefaultTcpSessionConfig(), eventExecutor);
+ }
+
+ /**
+ * Create an new AbsractTcpServer instance, with a specific configuration
+ *
+ * @param sessionConfig The configuration to use for this server
+ * @param eventExecutor used for executing IoHandler event in another pool of thread (not in the low level I/O one).
+ * Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
+ * operations.
+ */
+ protected AbstractTcpServer(TcpSessionConfig config, IoHandlerExecutor eventExecutor) {
+ super(config, eventExecutor);
}
/**
@@ -48,7 +57,7 @@ public abstract class AbstractTcpServer extends AbstractIoServer {
*/
@Override
public TcpSessionConfig getSessionConfig() {
- return this.config;
+ return (TcpSessionConfig) config;
}
/**
@@ -56,7 +65,7 @@ public abstract class AbstractTcpServer extends AbstractIoServer {
*
* @param config
*/
- public void setSessionConfig(final TcpSessionConfig config) {
+ public void setSessionConfig(TcpSessionConfig config) {
this.config = config;
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/mina/blob/638f2abd/core/src/main/java/org/apache/mina/transport/tcp/DefaultTcpSessionConfig.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/tcp/DefaultTcpSessionConfig.java b/core/src/main/java/org/apache/mina/transport/tcp/DefaultTcpSessionConfig.java
index 803d2ac..c281e1f 100644
--- a/core/src/main/java/org/apache/mina/transport/tcp/DefaultTcpSessionConfig.java
+++ b/core/src/main/java/org/apache/mina/transport/tcp/DefaultTcpSessionConfig.java
@@ -36,9 +36,18 @@ public class DefaultTcpSessionConfig extends AbstractIoSessionConfig implements
//=====================
// socket options
//=====================
-
+ /** The TCP_NODELAY socket option */
private Boolean tcpNoDelay = null;
+ /** The OOBINLINE socket option */
+ private Boolean oobInline = null;
+
+ /** The SO_KEEPALIVE socket option */
+ private Boolean keepAlive = null;
+
+ /** The SO_LINGER socket option */
+ private Integer soLinger;
+
/**
* {@inheritDoc}
*/
@@ -55,26 +64,6 @@ public class DefaultTcpSessionConfig extends AbstractIoSessionConfig implements
this.tcpNoDelay = tcpNoDelay;
}
- private Boolean reuseAddress = null;
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Boolean isReuseAddress() {
- return reuseAddress;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void setReuseAddress(boolean reuseAddress) {
- this.reuseAddress = reuseAddress;
- }
-
- private Boolean keepAlive = null;
-
/**
* {@inheritDoc}
*/
@@ -91,8 +80,6 @@ public class DefaultTcpSessionConfig extends AbstractIoSessionConfig implements
this.keepAlive = keepAlive;
}
- private Boolean oobInline = null;
-
/**
* {@inheritDoc}
*/
@@ -110,8 +97,6 @@ public class DefaultTcpSessionConfig extends AbstractIoSessionConfig implements
}
- private Integer soLinger;
-
/**
* {@inheritDoc}
*/
http://git-wip-us.apache.org/repos/asf/mina/blob/638f2abd/core/src/main/java/org/apache/mina/transport/udp/AbstractUdpServer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/udp/AbstractUdpServer.java b/core/src/main/java/org/apache/mina/transport/udp/AbstractUdpServer.java
index 3baee02..9c278cf 100644
--- a/core/src/main/java/org/apache/mina/transport/udp/AbstractUdpServer.java
+++ b/core/src/main/java/org/apache/mina/transport/udp/AbstractUdpServer.java
@@ -31,14 +31,27 @@ import org.apache.mina.service.server.AbstractIoServer;
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public abstract class AbstractUdpServer extends AbstractIoServer {
- /** the default session configuration */
- private UdpSessionConfig config;
-
/**
* Create an new AbsractUdpServer instance
+ *
+ * @param eventExecutor used for executing IoHandler event in another pool of thread (not in the low level I/O one).
+ * Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
+ * operations.
*/
protected AbstractUdpServer(IoHandlerExecutor ioHandlerExecutor) {
- super(ioHandlerExecutor);
+ super(new DefaultUdpSessionConfig(), ioHandlerExecutor);
+ }
+
+ /**
+ * Create an new AbsractUdpServer instance
+ *
+ * @param sessionConfig The configuration to use for this server
+ * @param eventExecutor used for executing IoHandler event in another pool of thread (not in the low level I/O one).
+ * Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
+ * operations.
+ */
+ protected AbstractUdpServer(UdpSessionConfig config, IoHandlerExecutor ioHandlerExecutor) {
+ super(config, ioHandlerExecutor);
this.config = new DefaultUdpSessionConfig();
}
@@ -48,4 +61,18 @@ public abstract class AbstractUdpServer extends AbstractIoServer {
public void initSecured(IoSession session) throws SSLException {
throw new RuntimeException("SSL is not supported for UDP");
}
+
+ /**
+ * {@inheritDoc}
+ */
+ public UdpSessionConfig getSessionConfig() {
+ return (UdpSessionConfig) config;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setSessionConfig(UdpSessionConfig config) {
+ this.config = config;
+ }
}
http://git-wip-us.apache.org/repos/asf/mina/blob/638f2abd/examples/src/main/java/org/apache/mina/examples/udpecho/NioUdpEchoServer.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/mina/examples/udpecho/NioUdpEchoServer.java b/examples/src/main/java/org/apache/mina/examples/udpecho/NioUdpEchoServer.java
index f8c767a..b8ec096 100644
--- a/examples/src/main/java/org/apache/mina/examples/udpecho/NioUdpEchoServer.java
+++ b/examples/src/main/java/org/apache/mina/examples/udpecho/NioUdpEchoServer.java
@@ -19,7 +19,6 @@
*/
package org.apache.mina.examples.udpecho;
-
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
@@ -33,114 +32,89 @@ import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.filterchain.ReadFilterChainController;
import org.apache.mina.filterchain.WriteFilterChainController;
import org.apache.mina.session.WriteRequest;
-import org.apache.mina.transport.nio.NioSelectorLoop;
import org.apache.mina.transport.nio.NioUdpServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
/**
* A UDP base echo server sending back every datagram received
*
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
-public class NioUdpEchoServer
-{
- static final Logger LOG = LoggerFactory.getLogger( NioUdpEchoServer.class );
-
+public class NioUdpEchoServer {
+ static final Logger LOG = LoggerFactory.getLogger(NioUdpEchoServer.class);
- public static void main( final String[] args )
- {
- LOG.info( "starting echo server" );
+ public static void main(final String[] args) {
+ LOG.info("starting echo server");
- final NioUdpServer server = new NioUdpServer( new NioSelectorLoop( "I/O", 0 ), null );
+ final NioUdpServer server = new NioUdpServer();
- // create the fitler chain for this service
- server.setFilters( new LoggingFilter( "LoggingFilter1" ), new IoFilter()
- {
+ // create the filter chain for this service
+ server.setFilters(new LoggingFilter("LoggingFilter1"), new IoFilter() {
@Override
- public void sessionOpened( final IoSession session )
- {
- LOG.info( "session {} open", session );
+ public void sessionOpened(final IoSession session) {
+ LOG.info("session {} open", session);
}
-
@Override
- public void sessionIdle( final IoSession session, final IdleStatus status )
- {
- LOG.info( "session {} idle", session );
+ public void sessionIdle(final IoSession session, final IdleStatus status) {
+ LOG.info("session {} idle", session);
}
-
@Override
- public void sessionClosed( final IoSession session )
- {
- LOG.info( "session {} open", session );
+ public void sessionClosed(final IoSession session) {
+ LOG.info("session {} open", session);
}
-
@Override
- public void messageWriting( final IoSession session, WriteRequest message,
- final WriteFilterChainController controller )
- {
+ public void messageWriting(final IoSession session, WriteRequest message,
+ final WriteFilterChainController controller) {
// we just push the message in the chain
- controller.callWriteNextFilter( message );
+ controller.callWriteNextFilter(message);
}
-
@Override
- public void messageReceived( final IoSession session, final Object message,
- final ReadFilterChainController controller )
- {
-
- if ( message instanceof ByteBuffer )
- {
- LOG.info( "echoing" );
- session.write( message );
+ public void messageReceived(final IoSession session, final Object message,
+ final ReadFilterChainController controller) {
+
+ if (message instanceof ByteBuffer) {
+ LOG.info("echoing");
+ session.write(message);
}
}
-
@Override
- public void messageSent( final IoSession session, final Object message )
- {
- LOG.info( "message {} sent", message );
+ public void messageSent(final IoSession session, final Object message) {
+ LOG.info("message {} sent", message);
}
- } );
+ });
- server.setIoHandler( new AbstractIoHandler()
- {
+ server.setIoHandler(new AbstractIoHandler() {
@Override
- public void sessionOpened( final IoSession session )
- {
- LOG.info( "session opened {}", session );
+ public void sessionOpened(final IoSession session) {
+ LOG.info("session opened {}", session);
final String welcomeStr = "welcome\n";
- final ByteBuffer bf = ByteBuffer.allocate( welcomeStr.length() );
- bf.put( welcomeStr.getBytes() );
+ final ByteBuffer bf = ByteBuffer.allocate(welcomeStr.length());
+ bf.put(welcomeStr.getBytes());
bf.flip();
- session.write( bf );
+ session.write(bf);
}
- } );
-
- try
- {
- final SocketAddress address = new InetSocketAddress( 9999 );
- server.bind( address );
- LOG.debug( "Running the server for 25 sec" );
- Thread.sleep( 25000 );
- LOG.debug( "Unbinding the UDP port" );
+ });
+
+ try {
+ final SocketAddress address = new InetSocketAddress(9999);
+ server.bind(address);
+ LOG.debug("Running the server for 25 sec");
+ Thread.sleep(25000);
+ LOG.debug("Unbinding the UDP port");
server.unbind();
- }
- catch ( final IOException e )
- {
- LOG.error( "I/O exception", e );
- }
- catch ( final InterruptedException e )
- {
- LOG.error( "Interrupted exception", e );
+ } catch (final IOException e) {
+ LOG.error("I/O exception", e);
+ } catch (final InterruptedException e) {
+ LOG.error("Interrupted exception", e);
}
}
}