mina-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Declan Cox" <declan....@gmail.com>
Subject Re: Sending to multiple UDP destinations
Date Wed, 10 Dec 2008 21:32:28 GMT
Hi Mina Users,After some thought about this I reached the conclusion that I
could solve this problem by simply writing my response on the IoAcceptor
session since this is essentially a 'connected' UDP session to the remote
port which originated the request. So I can basically eliminate the creation
of an NioDatagramConnector altogether, simplifying the code - always a good
thing. What I have now is:

ServerHandler excerpt:


    @Override
    public void messageReceived(IoSession session, Object object)
            throws Exception {
        ServiceEnvironmentMessage message = (ServiceEnvironmentMessage)
object;
        SocketAddress remoteAddress = session.getRemoteAddress();

        if (logger.isDebugEnabled()) {
            logger.debug(message.getMethodName()+" received from " +
remoteAddress.toString() + " : " + message);
        }

        ServiceEnvironmentMessage response =
messageDispatcher.handleMessage(message);

        if ( response != null ) {
            session.write(response);
        }
    }


..
The messageDispatcher.handleMessage() crafts the appropriate response (as
the result of a db lookup in this case) and I write the response on the same
session.


Still would like to hear any comments etc on this approach,

Thanks,
Declan.



On Sat, Dec 6, 2008 at 10:25 AM, Declan Cox <declan.cox@gmail.com> wrote:

> Hi,I am using MINA 2.0.0-M3 for an application that receives UDP request
> packets from multiple destinations and needs to send responses to the
> destination from which the request was issued. I have a NioDatagramAcceptor
> which receives the request processes it and then I create an
>  NioDatagramConnector to send the response. However since the connector
> essentially binds to a particular remote address I need to check the remote
> address of the incoming packet and potentially create a new connector if the
> current session is not 'bound' to the appropriate address. This results in
> lot of NioDatagramConnector instances being created even if I am only
> sending to two different remote addresses say (e.g., each consecutive
> request might be coming from an alternative host so I have to check it and
> potentially tear down the session and create a new one).
> Here is a code snippet for the sender (note the original code sent to just
> one well known remote address - this code is modified from the original
>  (hack if you like) to support multiple senders hence the flag
> useRemoteAddress - the remote address being passed down from the receiver
> session) :
>
> .....
>
>     public void connect(SocketAddress address) throws IOException {
>
>         IoConnector connector;
>
>         // if we are receiving from a new address then we need to tear down
> this
>         // session and create a new one.
>         //
>         if (session != null && !session.getRemoteAddress().equals(address))
> {
>             if (logger.isDebugEnabled()) {
>                 logger.debug("Session local : " +
> session.getLocalAddress());
>                 logger.debug("Session remote: " +
> session.getRemoteAddress());
>             }
>             quit();
>         }
>
>         if (session == null || !session.isConnected()) {
>
>             if (logger.isDebugEnabled()) {
>                 logger.debug("Initiating connection..");
>             }
>             connector = new NioDatagramConnector();
>
>             DefaultDatagramSessionConfig cfg = new
> DefaultDatagramSessionConfig();
>             cfg.setReuseAddress(true);
>             connector.getSessionConfig().setRe
>
>             connectorInstanceCount.addAndGet(1);
>
>             if (logger.isInfoEnabled()) {
>                 logger.info("NioDatagramConnector instances created: " +
> connectorInstanceCount.get() + " sending to remote address: " + address);
>             }
>
>             connector.setFilterChainBuilder(this.filterChainBuilder);
>
>             connector.setHandler(this);
>
>             ConnectFuture connectFuture = connector.connect(address);
>
>             connectFuture.awaitUninterruptibly();
>
>             session = connectFuture.getSession();
>         }
>     }
>
>     /**
>      * Send a message to the specified remote address (extracted from the
> received message)
>      * or to a pre-configured address.
>      *
>      * @param message       - message to send
>      * @param remoteAddress - remote port of that issued lookup request
>      * @throws IOException - if it cant connect
>      */
>     public void sendMessage(ServiceEnvironmentMessage message,
> SocketAddress remoteAddress) throws IOException {
>
>         SocketAddress destinationAddress;
>
>         if (isUseRemoteAddress() && remoteAddress != null) {
>             destinationAddress = remoteAddress;
>         } else {
>             destinationAddress = this.address;
>         }
>
>         connect(destinationAddress);
>
>         if (logger.isDebugEnabled()) {
>             responseCount.addAndGet(1);
>             StringBuilder sb = new StringBuilder();
>             sb
>                     .append("count ")
>                     .append(responseCount)
>                     .append(" : ")
>                     .append(message.getMethodName())
>                     .append(" to ")
>                     .append(destinationAddress)
>                     .append(" contents ")
>                     .append(message);
>             logger.debug(sb.toString());
>         }
>         session.write(message);
>     }
>
>     public void quit() {
>         if (session != null) {
>             if (session.isConnected()) {
>                 session.getCloseFuture().awaitUninterruptibly(500,
> TimeUnit.MILLISECONDS);
>             }
>             session.close();
>             session = null;
>         }
>     }
>
>
> I think what this basically boils down to is my very meagre understanding
> of how Mina works and should be used - I have trawled the forums and there's
> not that much on the UDP side of things.
>
> This is a fantastic framework by the way. Using mina we have been able to
> create Java apps that integrate with realtime C-based ASN.1/UDP apps to
> create neat intelligent network applications and we are only scratching the
> surface!
>
> Thanks in advance,
>
> Declan Cox
>
> Senior Software Engineer
> Tango Telecom Ltd.,
> www.tango.ie
>
>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message