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