| 30 |
ashish |
1 |
/*
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
package org.apache.thrift.transport;
|
|
|
21 |
|
|
|
22 |
import org.slf4j.Logger;
|
|
|
23 |
import org.slf4j.LoggerFactory;
|
|
|
24 |
|
|
|
25 |
import java.io.BufferedInputStream;
|
|
|
26 |
import java.io.BufferedOutputStream;
|
|
|
27 |
import java.io.IOException;
|
|
|
28 |
import java.net.InetSocketAddress;
|
|
|
29 |
import java.net.Socket;
|
|
|
30 |
import java.net.SocketException;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Socket implementation of the TTransport interface. To be commented soon!
|
|
|
34 |
*
|
|
|
35 |
*/
|
|
|
36 |
public class TSocket extends TIOStreamTransport {
|
|
|
37 |
|
|
|
38 |
private static final Logger LOGGER = LoggerFactory.getLogger(TSocket.class.getName());
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Wrapped Socket object
|
|
|
42 |
*/
|
|
|
43 |
private Socket socket_ = null;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Remote host
|
|
|
47 |
*/
|
|
|
48 |
private String host_ = null;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Remote port
|
|
|
52 |
*/
|
|
|
53 |
private int port_ = 0;
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Socket timeout
|
|
|
57 |
*/
|
|
|
58 |
private int timeout_ = 0;
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Constructor that takes an already created socket.
|
|
|
62 |
*
|
|
|
63 |
* @param socket Already created socket object
|
|
|
64 |
* @throws TTransportException if there is an error setting up the streams
|
|
|
65 |
*/
|
|
|
66 |
public TSocket(Socket socket) throws TTransportException {
|
|
|
67 |
socket_ = socket;
|
|
|
68 |
try {
|
|
|
69 |
socket_.setSoLinger(false, 0);
|
|
|
70 |
socket_.setTcpNoDelay(true);
|
|
|
71 |
} catch (SocketException sx) {
|
|
|
72 |
LOGGER.warn("Could not configure socket.", sx);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
if (isOpen()) {
|
|
|
76 |
try {
|
|
|
77 |
inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
|
|
|
78 |
outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024);
|
|
|
79 |
} catch (IOException iox) {
|
|
|
80 |
close();
|
|
|
81 |
throw new TTransportException(TTransportException.NOT_OPEN, iox);
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Creates a new unconnected socket that will connect to the given host
|
|
|
88 |
* on the given port.
|
|
|
89 |
*
|
|
|
90 |
* @param host Remote host
|
|
|
91 |
* @param port Remote port
|
|
|
92 |
*/
|
|
|
93 |
public TSocket(String host, int port) {
|
|
|
94 |
this(host, port, 0);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Creates a new unconnected socket that will connect to the given host
|
|
|
99 |
* on the given port.
|
|
|
100 |
*
|
|
|
101 |
* @param host Remote host
|
|
|
102 |
* @param port Remote port
|
|
|
103 |
* @param timeout Socket timeout
|
|
|
104 |
*/
|
|
|
105 |
public TSocket(String host, int port, int timeout) {
|
|
|
106 |
host_ = host;
|
|
|
107 |
port_ = port;
|
|
|
108 |
timeout_ = timeout;
|
|
|
109 |
initSocket();
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Initializes the socket object
|
|
|
114 |
*/
|
|
|
115 |
private void initSocket() {
|
|
|
116 |
socket_ = new Socket();
|
|
|
117 |
try {
|
|
|
118 |
socket_.setSoLinger(false, 0);
|
|
|
119 |
socket_.setTcpNoDelay(true);
|
|
|
120 |
socket_.setSoTimeout(timeout_);
|
|
|
121 |
} catch (SocketException sx) {
|
|
|
122 |
LOGGER.error("Could not configure socket.", sx);
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Sets the socket timeout
|
|
|
128 |
*
|
|
|
129 |
* @param timeout Milliseconds timeout
|
|
|
130 |
*/
|
|
|
131 |
public void setTimeout(int timeout) {
|
|
|
132 |
timeout_ = timeout;
|
|
|
133 |
try {
|
|
|
134 |
socket_.setSoTimeout(timeout);
|
|
|
135 |
} catch (SocketException sx) {
|
|
|
136 |
LOGGER.warn("Could not set socket timeout.", sx);
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Returns a reference to the underlying socket.
|
|
|
142 |
*/
|
|
|
143 |
public Socket getSocket() {
|
|
|
144 |
if (socket_ == null) {
|
|
|
145 |
initSocket();
|
|
|
146 |
}
|
|
|
147 |
return socket_;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Checks whether the socket is connected.
|
|
|
152 |
*/
|
|
|
153 |
public boolean isOpen() {
|
|
|
154 |
if (socket_ == null) {
|
|
|
155 |
return false;
|
|
|
156 |
}
|
|
|
157 |
return socket_.isConnected();
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Connects the socket, creating a new socket object if necessary.
|
|
|
162 |
*/
|
|
|
163 |
public void open() throws TTransportException {
|
|
|
164 |
if (isOpen()) {
|
|
|
165 |
throw new TTransportException(TTransportException.ALREADY_OPEN, "Socket already connected.");
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
if (host_.length() == 0) {
|
|
|
169 |
throw new TTransportException(TTransportException.NOT_OPEN, "Cannot open null host.");
|
|
|
170 |
}
|
|
|
171 |
if (port_ <= 0) {
|
|
|
172 |
throw new TTransportException(TTransportException.NOT_OPEN, "Cannot open without port.");
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
if (socket_ == null) {
|
|
|
176 |
initSocket();
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
try {
|
|
|
180 |
socket_.connect(new InetSocketAddress(host_, port_));
|
|
|
181 |
inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
|
|
|
182 |
outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024);
|
|
|
183 |
} catch (IOException iox) {
|
|
|
184 |
close();
|
|
|
185 |
throw new TTransportException(TTransportException.NOT_OPEN, iox);
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* Closes the socket.
|
|
|
191 |
*/
|
|
|
192 |
public void close() {
|
|
|
193 |
// Close the underlying streams
|
|
|
194 |
super.close();
|
|
|
195 |
|
|
|
196 |
// Close the socket
|
|
|
197 |
if (socket_ != null) {
|
|
|
198 |
try {
|
|
|
199 |
socket_.close();
|
|
|
200 |
} catch (IOException iox) {
|
|
|
201 |
LOGGER.warn("Could not close socket.", iox);
|
|
|
202 |
}
|
|
|
203 |
socket_ = null;
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
}
|