Coverage Report - org.mule.transport.udp.UdpMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
UdpMessageRequester
0%
0/21
0%
0/8
1.833
 
 1  
 /*
 2  
  * $Id: UdpMessageRequester.java 11433 2008-03-20 03:43:57Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transport.udp;
 12  
 
 13  
 import org.mule.DefaultMuleMessage;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.endpoint.InboundEndpoint;
 16  
 import org.mule.transport.AbstractMessageRequester;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.net.DatagramPacket;
 20  
 import java.net.DatagramSocket;
 21  
 import java.util.Map;
 22  
 
 23  
 /**
 24  
  * Responsible for requesting MuleEvents as UDP packets on the network
 25  
  */
 26  
 
 27  
 public class UdpMessageRequester extends AbstractMessageRequester
 28  
 {
 29  
     
 30  
     protected final UdpConnector connector;
 31  
 
 32  
     public UdpMessageRequester(InboundEndpoint endpoint)
 33  
     {
 34  0
         super(endpoint);
 35  0
         this.connector = (UdpConnector)endpoint.getConnector();
 36  0
     }
 37  
 
 38  
     protected void doConnect() throws Exception
 39  
     {
 40  
         // Test the connection
 41  0
         DatagramSocket socket = connector.getSocket(endpoint);
 42  0
         connector.releaseSocket(socket, endpoint);
 43  0
     }
 44  
 
 45  
     protected void doDisconnect() throws Exception
 46  
     {
 47  
         // nothing to do
 48  0
     }
 49  
 
 50  
     private DatagramPacket request(DatagramSocket socket, int timeout) throws IOException
 51  
     {
 52  0
         int origTimeout = socket.getSoTimeout();
 53  
         try
 54  
         {
 55  0
             DatagramPacket packet = new DatagramPacket(new byte[connector.getReceiveBufferSize()],
 56  
                 connector.getReceiveBufferSize());
 57  
 
 58  0
             if(timeout > 0 && timeout != socket.getSoTimeout())
 59  
             {
 60  0
                 socket.setSoTimeout(timeout);
 61  
             }
 62  0
             socket.receive(packet);
 63  0
             return packet;
 64  
         }
 65  
         finally
 66  
         {
 67  0
             if(socket.getSoTimeout()!= origTimeout)
 68  
             {
 69  0
                 socket.setSoTimeout(origTimeout);
 70  
             }
 71  
         }
 72  
     }
 73  
 
 74  
     /**
 75  
      * Make a specific request to the underlying transport
 76  
      *
 77  
      * @param timeout the maximum time the operation should block before returning.
 78  
      *            The call should return immediately if there is data available. If
 79  
      *            no data becomes available before the timeout elapses, null will be
 80  
      *            returned
 81  
      * @return the result of the request wrapped in a MuleMessage object. Null will be
 82  
      *         returned if no data was avaialable
 83  
      * @throws Exception if the call to the underlying protocal cuases an exception
 84  
      */
 85  
     protected MuleMessage doRequest(long timeout) throws Exception
 86  
     {
 87  0
         DatagramSocket socket = connector.getSocket(endpoint);
 88  0
         DatagramPacket result = request(socket, (int)timeout);
 89  0
         if (result == null)
 90  
         {
 91  0
             return null;
 92  
         }
 93  0
         return new DefaultMuleMessage(connector.getMessageAdapter(result), (Map)null);
 94  
     }
 95  
 
 96  
     protected void doDispose()
 97  
     {
 98  
         // template method
 99  0
     }
 100  
 
 101  
 }