1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.tcp; |
12 | |
|
13 | |
import org.mule.impl.MuleMessage; |
14 | |
import org.mule.providers.AbstractPollingMessageReceiver; |
15 | |
import org.mule.providers.ConnectException; |
16 | |
import org.mule.providers.tcp.i18n.TcpMessages; |
17 | |
import org.mule.umo.UMOComponent; |
18 | |
import org.mule.umo.UMOMessage; |
19 | |
import org.mule.umo.endpoint.UMOEndpoint; |
20 | |
import org.mule.umo.lifecycle.InitialisationException; |
21 | |
import org.mule.umo.provider.UMOConnector; |
22 | |
import org.mule.umo.provider.UMOMessageAdapter; |
23 | |
import org.mule.util.StringUtils; |
24 | |
|
25 | |
import java.io.BufferedInputStream; |
26 | |
import java.io.DataInputStream; |
27 | |
import java.net.InetAddress; |
28 | |
import java.net.Socket; |
29 | |
import java.net.URI; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class TcpStreamingMessageReceiver extends AbstractPollingMessageReceiver |
41 | |
{ |
42 | 0 | protected Socket clientSocket = null; |
43 | 0 | protected DataInputStream dataIn = null; |
44 | 0 | protected TcpProtocol protocol = null; |
45 | |
|
46 | |
public TcpStreamingMessageReceiver(UMOConnector connector, |
47 | |
UMOComponent component, |
48 | |
UMOEndpoint endpoint) throws InitialisationException |
49 | |
{ |
50 | 0 | super(connector, component, endpoint); |
51 | 0 | protocol = ((TcpConnector)connector).getTcpProtocol(); |
52 | 0 | } |
53 | |
|
54 | |
protected void doDispose() |
55 | |
{ |
56 | |
|
57 | 0 | } |
58 | |
|
59 | |
protected void doConnect() throws ConnectException |
60 | |
{ |
61 | 0 | URI uri = endpoint.getEndpointURI().getUri(); |
62 | 0 | String host = StringUtils.defaultIfEmpty(uri.getHost(), "localhost"); |
63 | |
|
64 | |
try |
65 | |
{ |
66 | 0 | logger.debug("Attempting to connect to server socket"); |
67 | 0 | InetAddress inetAddress = InetAddress.getByName(host); |
68 | 0 | clientSocket = new Socket(inetAddress, uri.getPort()); |
69 | 0 | TcpConnector connector = (TcpConnector)this.connector; |
70 | 0 | clientSocket.setReceiveBufferSize(connector.getReceiveBufferSize()); |
71 | 0 | clientSocket.setSendBufferSize(connector.getSendBufferSize()); |
72 | 0 | clientSocket.setSoTimeout(connector.getReceiveTimeout()); |
73 | |
|
74 | 0 | dataIn = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream())); |
75 | 0 | logger.debug("Connected to server socket"); |
76 | |
} |
77 | 0 | catch (Exception e) |
78 | |
{ |
79 | 0 | throw new ConnectException(TcpMessages.failedToBindToUri(uri), e, this); |
80 | 0 | } |
81 | 0 | } |
82 | |
|
83 | |
protected void doDisconnect() throws Exception |
84 | |
{ |
85 | |
try |
86 | |
{ |
87 | 0 | if (clientSocket != null && !clientSocket.isClosed()) |
88 | |
{ |
89 | 0 | clientSocket.shutdownInput(); |
90 | 0 | clientSocket.shutdownOutput(); |
91 | 0 | clientSocket.close(); |
92 | |
} |
93 | 0 | } |
94 | |
finally |
95 | |
{ |
96 | 0 | clientSocket = null; |
97 | 0 | dataIn = null; |
98 | 0 | logger.info("Closed tcp client socket"); |
99 | 0 | } |
100 | 0 | } |
101 | |
|
102 | |
public void poll() throws Exception |
103 | |
{ |
104 | |
|
105 | 0 | setFrequency(0); |
106 | |
|
107 | 0 | byte[] data = (byte[])protocol.read(dataIn); |
108 | 0 | if (data != null) |
109 | |
{ |
110 | 0 | UMOMessageAdapter adapter = connector.getMessageAdapter(data); |
111 | 0 | UMOMessage message = new MuleMessage(adapter); |
112 | 0 | routeMessage(message, endpoint.isSynchronous()); |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
} |