1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.udp;
12
13 import org.mule.api.MuleException;
14 import org.mule.api.endpoint.ImmutableEndpoint;
15 import org.mule.api.endpoint.InboundEndpoint;
16 import org.mule.api.lifecycle.InitialisationException;
17 import org.mule.api.service.Service;
18 import org.mule.transport.AbstractConnector;
19
20 import java.net.DatagramSocket;
21
22 import org.apache.commons.pool.impl.GenericKeyedObjectPool;
23
24
25
26
27 public class UdpConnector extends AbstractConnector
28 {
29
30 public static final String UDP = "udp";
31 public static final int DEFAULT_SOCKET_TIMEOUT = INT_VALUE_NOT_SET;
32 public static final int DEFAULT_BUFFER_SIZE = 1024 * 16;
33 public static final String KEEP_SEND_SOCKET_OPEN_PROPERTY = "keepSendSocketOpen";
34
35 protected int sendTimeout = DEFAULT_SOCKET_TIMEOUT;
36 protected int receiveTimeout = DEFAULT_SOCKET_TIMEOUT;
37 protected int sendBufferSize = DEFAULT_BUFFER_SIZE;
38 protected int receiveBufferSize = DEFAULT_BUFFER_SIZE;
39 protected boolean keepSendSocketOpen = true;
40 protected boolean broadcast;
41 protected GenericKeyedObjectPool dispatcherSocketsPool = new GenericKeyedObjectPool();
42
43
44 protected void doInitialise() throws InitialisationException
45 {
46 dispatcherSocketsPool.setFactory(new UdpSocketFactory());
47 dispatcherSocketsPool.setTestOnBorrow(true);
48 dispatcherSocketsPool.setTestOnReturn(true);
49
50 dispatcherSocketsPool.setMaxActive(1);
51 }
52
53 protected void doDispose()
54 {
55 try
56 {
57 dispatcherSocketsPool.close();
58 }
59 catch (Exception e)
60 {
61 logger.warn("Failed to close dispatcher socket pool: " + e.getMessage());
62 }
63 }
64
65 protected void doConnect() throws Exception
66 {
67
68 }
69
70 protected void doDisconnect() throws Exception
71 {
72 dispatcherSocketsPool.clear();
73 }
74
75 protected void doStart() throws MuleException
76 {
77
78 }
79
80 protected void doStop() throws MuleException
81 {
82
83 }
84
85 public String getProtocol()
86 {
87 return UDP;
88 }
89
90 public int getSendTimeout()
91 {
92 return this.sendTimeout;
93 }
94
95 public void setSendTimeout(int timeout)
96 {
97 if (timeout < 0)
98 {
99 timeout = DEFAULT_SOCKET_TIMEOUT;
100 }
101 this.sendTimeout = timeout;
102 }
103
104 public int getReceiveTimeout()
105 {
106 return receiveTimeout;
107 }
108
109 public void setReceiveTimeout(int timeout)
110 {
111 if (timeout < 0)
112 {
113 timeout = DEFAULT_SOCKET_TIMEOUT;
114 }
115 this.receiveTimeout = timeout;
116 }
117
118 public int getSendBufferSize()
119 {
120 return sendBufferSize;
121 }
122
123 public void setSendBufferSize(int sendBufferSize)
124 {
125 if (sendBufferSize < 1)
126 {
127 sendBufferSize = DEFAULT_BUFFER_SIZE;
128 }
129 this.sendBufferSize = sendBufferSize;
130 }
131
132 public int getReceiveBufferSize()
133 {
134 return receiveBufferSize;
135 }
136
137 public void setReceiveBufferSize(int receiveBufferSize)
138 {
139 if (receiveBufferSize < 1)
140 {
141 receiveBufferSize = DEFAULT_BUFFER_SIZE;
142 }
143 this.receiveBufferSize = receiveBufferSize;
144 }
145
146 public boolean isBroadcast()
147 {
148 return broadcast;
149 }
150
151 public void setBroadcast(boolean broadcast)
152 {
153 this.broadcast = broadcast;
154 }
155
156
157 public boolean isKeepSendSocketOpen()
158 {
159 return keepSendSocketOpen;
160 }
161
162 public void setKeepSendSocketOpen(boolean keepSendSocketOpen)
163 {
164 this.keepSendSocketOpen = keepSendSocketOpen;
165 }
166
167
168
169
170
171
172
173
174 DatagramSocket getSocket(ImmutableEndpoint endpoint) throws Exception
175 {
176 return (DatagramSocket) dispatcherSocketsPool.borrowObject(endpoint);
177 }
178
179 void releaseSocket(DatagramSocket socket, ImmutableEndpoint endpoint) throws Exception
180 {
181 dispatcherSocketsPool.returnObject(endpoint, socket);
182 }
183
184
185 protected Object getReceiverKey(Service service, InboundEndpoint endpoint)
186 {
187 return endpoint.getEndpointURI().getAddress() + "/" + service.getName();
188 }
189 }