Coverage Report - org.mule.transport.udp.UdpMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
UdpMessageRequester
0%
0/31
0%
0/12
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transport.udp;
 8  
 
 9  
 import org.mule.api.MuleMessage;
 10  
 import org.mule.api.endpoint.InboundEndpoint;
 11  
 import org.mule.api.retry.RetryContext;
 12  
 import org.mule.transport.AbstractMessageRequester;
 13  
 
 14  
 import java.io.IOException;
 15  
 import java.net.DatagramPacket;
 16  
 import java.net.DatagramSocket;
 17  
 
 18  
 /**
 19  
  * Responsible for requesting MuleEvents as UDP packets on the network
 20  
  */
 21  
 
 22  
 public class UdpMessageRequester extends AbstractMessageRequester
 23  
 {
 24  
     
 25  
     protected final UdpConnector connector;
 26  
 
 27  
     public UdpMessageRequester(InboundEndpoint endpoint)
 28  
     {
 29  0
         super(endpoint);
 30  0
         this.connector = (UdpConnector)endpoint.getConnector();
 31  0
     }
 32  
 
 33  
     @Override
 34  
     protected void doConnect() throws Exception
 35  
     {
 36  
         // nothing, there is an optional validation in validateConnection()
 37  0
     }
 38  
 
 39  
     @Override
 40  
     public RetryContext validateConnection(RetryContext retryContext)
 41  
     {
 42  0
         DatagramSocket socket = null;
 43  
         try
 44  
         {
 45  0
             socket = connector.getSocket(endpoint);
 46  
 
 47  0
             retryContext.setOk();
 48  
         }
 49  0
         catch (Exception ex)
 50  
         {
 51  0
             retryContext.setFailed(ex);
 52  
         }
 53  
         finally
 54  
         {
 55  0
             if (socket != null)
 56  
             {
 57  
                 try
 58  
                 {
 59  0
                     connector.releaseSocket(socket, endpoint);
 60  
                 }
 61  0
                 catch (Exception e)
 62  
                 {
 63  0
                     if (logger.isDebugEnabled())
 64  
                     {
 65  0
                         logger.debug("Failed to release a socket " + socket, e);
 66  
                     }
 67  0
                 }
 68  
             }
 69  
         }
 70  
 
 71  0
         return retryContext;
 72  
 
 73  
     }
 74  
 
 75  
     @Override
 76  
     protected void doDisconnect() throws Exception
 77  
     {
 78  
         // nothing to do
 79  0
     }
 80  
 
 81  
     private DatagramPacket request(DatagramSocket socket, int timeout) throws IOException
 82  
     {
 83  0
         int origTimeout = socket.getSoTimeout();
 84  
         try
 85  
         {
 86  0
             DatagramPacket packet = new DatagramPacket(new byte[connector.getReceiveBufferSize()],
 87  
                 connector.getReceiveBufferSize());
 88  
 
 89  0
             if(timeout > 0 && timeout != socket.getSoTimeout())
 90  
             {
 91  0
                 socket.setSoTimeout(timeout);
 92  
             }
 93  0
             socket.receive(packet);
 94  0
             return packet;
 95  
         }
 96  
         finally
 97  
         {
 98  0
             if(socket.getSoTimeout()!= origTimeout)
 99  
             {
 100  0
                 socket.setSoTimeout(origTimeout);
 101  
             }
 102  
         }
 103  
     }
 104  
 
 105  
     /**
 106  
      * Make a specific request to the underlying transport
 107  
      *
 108  
      * @param timeout the maximum time the operation should block before returning.
 109  
      *            The call should return immediately if there is data available. If
 110  
      *            no data becomes available before the timeout elapses, null will be
 111  
      *            returned
 112  
      * @return the result of the request wrapped in a MuleMessage object. Null will be
 113  
      *         returned if no data was avaialable
 114  
      * @throws Exception if the call to the underlying protocal cuases an exception
 115  
      */
 116  
     @Override
 117  
     protected MuleMessage doRequest(long timeout) throws Exception
 118  
     {
 119  0
         DatagramSocket socket = connector.getSocket(endpoint);
 120  0
         DatagramPacket result = request(socket, (int)timeout);
 121  0
         if (result == null)
 122  
         {
 123  0
             return null;
 124  
         }
 125  0
         return createMuleMessage(result, endpoint.getEncoding());
 126  
     }
 127  
 
 128  
     @Override
 129  
     protected void doDispose()
 130  
     {
 131  
         // template method
 132  0
     }
 133  
 
 134  
 }