Package nexj.core.rpc.udp

Source Code of nexj.core.rpc.udp.UDPReceiver

// Copyright 2010 NexJ Systems Inc. This software is licensed under the terms of the Eclipse Public License 1.0
package nexj.core.rpc.udp;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import nexj.core.integration.ContextReceiver;
import nexj.core.meta.Primitive;
import nexj.core.meta.integration.channel.udp.UDPChannel;
import nexj.core.rpc.TransferObject;
import nexj.core.runtime.InvocationContext;
import nexj.core.util.Binary;

/**
* UDP receiver.
*/
public class UDPReceiver extends ContextReceiver implements UDPListener
{
   // associations
  
   /**
    * The channel metadata object.
    */
   protected UDPChannel m_channel;

   // operations

   /**
    * Sets the channel metadata object.
    * @param channel The channel metadata object to set.
    */
   public void setChannel(UDPChannel channel)
   {
      m_channel = channel;
   }

   /**
    * @return The channel metadata object.
    */
   public UDPChannel getChannel()
   {
      return m_channel;
   }

   /**
    * @see nexj.core.rpc.udp.UDPListener#onMessage(byte[], int, int, java.net.SocketAddress)
    */
   public void onMessage(final byte[] data, final int nOfs, final int nCount, final SocketAddress address)
   {
      run(new ContextRunnable()
      {
         public boolean isEnabled() throws Throwable
         {
            return true;
         }

         public String getClientAddress() throws Throwable
         {
            InetSocketAddress iaddr = (InetSocketAddress)address;

            return "udp:" + iaddr.getAddress().getHostAddress() + ':' + iaddr.getPort();
         }

         public String getUser() throws Throwable
         {
            return m_channel.getDefaultUser();
         }

         public void run(InvocationContext context) throws Throwable
         {
            if (m_channel.getQueue() != null || isBound(m_channel, context))
            {
               Object body;
              
               if (m_channel.getEncoding() != null)
               {
                  body = new String(data, nOfs, nCount, m_channel.getEncoding());
               }
               else
               {
                  if (nOfs == 0 && nCount == data.length)
                  {
                     body = new Binary(data);
                  }
                  else
                  {
                     byte[] buf = new byte[nCount];

                     System.arraycopy(data, nOfs, buf, 0, nCount);
                     body = new Binary(buf);
                  }
               }

               if (m_channel.getQueue() != null)
               {
                  TransferObject tobj = new TransferObject(1);

                  tobj.setClassName("MessageQueue");
                  tobj.setValue(UDPSender.BODY, body);
                  context.getUnitOfWork().addMessage(m_channel.getQueue(), tobj);
               }

               if (isBound(m_channel, context))
               {
                  TransferObject tobj = new TransferObject(3);
                  InetSocketAddress iaddr = (InetSocketAddress)address;

                  tobj.setClassName("UDP");
                  tobj.setValue(UDPSender.HOST, iaddr.getAddress().getHostAddress());
                  tobj.setValue(UDPSender.PORT, Primitive.createInteger(iaddr.getPort()));
                  tobj.setValue(UDPSender.BODY, body);

                  receive(tobj, m_channel, context);
               }
            }
         }

         public void err(Throwable t, InvocationContext context) throws Throwable
         {
         }
      }, m_channel, "UDP");
   }
}
TOP

Related Classes of nexj.core.rpc.udp.UDPReceiver

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.