View Javadoc

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