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