View Javadoc

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          super(endpoint);
35          this.connector = (UdpConnector)endpoint.getConnector();
36      }
37  
38      protected void doConnect() throws Exception
39      {
40          // Test the connection
41          DatagramSocket socket = connector.getSocket(endpoint);
42          connector.releaseSocket(socket, endpoint);
43      }
44  
45      protected void doDisconnect() throws Exception
46      {
47          // nothing to do
48      }
49  
50      private DatagramPacket request(DatagramSocket socket, int timeout) throws IOException
51      {
52          int origTimeout = socket.getSoTimeout();
53          try
54          {
55              DatagramPacket packet = new DatagramPacket(new byte[connector.getReceiveBufferSize()],
56                  connector.getReceiveBufferSize());
57  
58              if(timeout > 0 && timeout != socket.getSoTimeout())
59              {
60                  socket.setSoTimeout(timeout);
61              }
62              socket.receive(packet);
63              return packet;
64          }
65          finally
66          {
67              if(socket.getSoTimeout()!= origTimeout)
68              {
69                  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          DatagramSocket socket = connector.getSocket(endpoint);
88          DatagramPacket result = request(socket, (int)timeout);
89          if (result == null)
90          {
91              return null;
92          }
93          return new DefaultMuleMessage(connector.getMessageAdapter(result), (Map)null);
94      }
95  
96      protected void doDispose()
97      {
98          // template method
99      }
100 
101 }