View Javadoc
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          super(endpoint);
30          this.connector = (UdpConnector)endpoint.getConnector();
31      }
32  
33      @Override
34      protected void doConnect() throws Exception
35      {
36          // nothing, there is an optional validation in validateConnection()
37      }
38  
39      @Override
40      public RetryContext validateConnection(RetryContext retryContext)
41      {
42          DatagramSocket socket = null;
43          try
44          {
45              socket = connector.getSocket(endpoint);
46  
47              retryContext.setOk();
48          }
49          catch (Exception ex)
50          {
51              retryContext.setFailed(ex);
52          }
53          finally
54          {
55              if (socket != null)
56              {
57                  try
58                  {
59                      connector.releaseSocket(socket, endpoint);
60                  }
61                  catch (Exception e)
62                  {
63                      if (logger.isDebugEnabled())
64                      {
65                          logger.debug("Failed to release a socket " + socket, e);
66                      }
67                  }
68              }
69          }
70  
71          return retryContext;
72  
73      }
74  
75      @Override
76      protected void doDisconnect() throws Exception
77      {
78          // nothing to do
79      }
80  
81      private DatagramPacket request(DatagramSocket socket, int timeout) throws IOException
82      {
83          int origTimeout = socket.getSoTimeout();
84          try
85          {
86              DatagramPacket packet = new DatagramPacket(new byte[connector.getReceiveBufferSize()],
87                  connector.getReceiveBufferSize());
88  
89              if(timeout > 0 && timeout != socket.getSoTimeout())
90              {
91                  socket.setSoTimeout(timeout);
92              }
93              socket.receive(packet);
94              return packet;
95          }
96          finally
97          {
98              if(socket.getSoTimeout()!= origTimeout)
99              {
100                 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         DatagramSocket socket = connector.getSocket(endpoint);
120         DatagramPacket result = request(socket, (int)timeout);
121         if (result == null)
122         {
123             return null;
124         }
125         return createMuleMessage(result, endpoint.getEncoding());
126     }
127 
128     @Override
129     protected void doDispose()
130     {
131         // template method
132     }
133 
134 }