KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > udp > UdpMessageDispatcher


1 /*
2  * $Id: UdpMessageDispatcher.java 3982 2006-11-22 14:28:01Z lajos $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.udp;
12
13 import java.io.IOException JavaDoc;
14 import java.net.DatagramPacket JavaDoc;
15 import java.net.DatagramSocket JavaDoc;
16 import java.net.InetAddress JavaDoc;
17 import java.net.URI JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.mule.impl.MuleMessage;
21 import org.mule.providers.AbstractMessageDispatcher;
22 import org.mule.umo.UMOEvent;
23 import org.mule.umo.UMOException;
24 import org.mule.umo.UMOMessage;
25 import org.mule.umo.endpoint.UMOImmutableEndpoint;
26 import org.mule.umo.provider.UMOConnector;
27
28 /**
29  * <code>UdpMessageDispatcher</code> is responsible for dispatching MuleEvents as
30  * UDP packets on the network
31  */

32
33 public class UdpMessageDispatcher extends AbstractMessageDispatcher
34 {
35     protected final UdpConnector connector;
36     protected InetAddress JavaDoc inetAddress;
37     protected DatagramSocket JavaDoc socket;
38     protected int port;
39
40     public UdpMessageDispatcher(UMOImmutableEndpoint endpoint)
41     {
42         super(endpoint);
43         this.connector = (UdpConnector)endpoint.getConnector();
44     }
45
46     protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception JavaDoc
47     {
48         if (!connected.get())
49         {
50             URI JavaDoc uri = endpoint.getEndpointURI().getUri();
51             port = uri.getPort();
52             inetAddress = InetAddress.getByName(uri.getHost());
53             socket = createSocket(port, inetAddress);
54         }
55     }
56
57     protected void doDisconnect() throws Exception JavaDoc
58     {
59         try
60         {
61             if (socket != null)
62             {
63                 socket.close();
64             }
65         }
66         finally
67         {
68             socket = null;
69         }
70     }
71
72     protected DatagramSocket JavaDoc createSocket(int port, InetAddress JavaDoc inetAddress) throws IOException JavaDoc
73     {
74         DatagramSocket JavaDoc socket = new DatagramSocket JavaDoc();
75         socket.setReceiveBufferSize(connector.getBufferSize());
76         socket.setSendBufferSize(connector.getBufferSize());
77         socket.setSoTimeout(connector.getTimeout());
78         return socket;
79     }
80
81     protected synchronized void doDispatch(UMOEvent event) throws Exception JavaDoc
82     {
83         byte[] payload = event.getTransformedMessageAsBytes();
84         write(socket, payload);
85     }
86
87     protected void write(DatagramSocket JavaDoc socket, byte[] data) throws IOException JavaDoc
88     {
89         DatagramPacket JavaDoc packet = new DatagramPacket JavaDoc(data, data.length);
90         if (port >= 0)
91         {
92             packet.setPort(port);
93         }
94         packet.setAddress(inetAddress);
95         socket.send(packet);
96     }
97
98     protected synchronized UMOMessage doSend(UMOEvent event) throws Exception JavaDoc
99     {
100         doDispatch(event);
101         // If we're doing sync receive try and read return info from socket
102
if (event.getEndpoint().isRemoteSync())
103         {
104             DatagramPacket JavaDoc result = receive(socket, event.getTimeout());
105             if (result == null)
106             {
107                 return null;
108             }
109             return new MuleMessage(connector.getMessageAdapter(result), event.getMessage());
110         }
111         else
112         {
113             return event.getMessage();
114         }
115     }
116
117     private DatagramPacket JavaDoc receive(DatagramSocket JavaDoc socket, int timeout) throws IOException JavaDoc
118     {
119         int origTimeout = socket.getSoTimeout();
120         try
121         {
122             DatagramPacket JavaDoc packet = new DatagramPacket JavaDoc(new byte[connector.getBufferSize()],
123                 connector.getBufferSize());
124             socket.setSoTimeout(timeout);
125             socket.receive(packet);
126             return packet;
127         }
128         finally
129         {
130             socket.setSoTimeout(origTimeout);
131         }
132     }
133
134     /**
135      * Make a specific request to the underlying transport
136      *
137      * @param endpoint the endpoint to use when connecting to the resource
138      * @param timeout the maximum time the operation should block before returning.
139      * The call should return immediately if there is data available. If
140      * no data becomes available before the timeout elapses, null will be
141      * returned
142      * @return the result of the request wrapped in a UMOMessage object. Null will be
143      * returned if no data was avaialable
144      * @throws Exception if the call to the underlying protocal cuases an exception
145      */

146     protected synchronized UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception JavaDoc
147     {
148         DatagramPacket JavaDoc result = receive(socket, (int)timeout);
149         if (result == null)
150         {
151             return null;
152         }
153         return new MuleMessage(connector.getMessageAdapter(result), (Map JavaDoc)null);
154     }
155
156     public Object JavaDoc getDelegateSession() throws UMOException
157     {
158         return null;
159     }
160
161     public UMOConnector getConnector()
162     {
163         return connector;
164     }
165
166     protected void doDispose()
167     {
168         // template method
169
}
170 }
171
Popular Tags