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