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