blob: 4922b04157520eda0bb6e5de15ca8a794f508db6 [file] [log] [blame]
Mark Slee83c52a82006-06-07 06:51:18 +00001package com.facebook.thrift.transport;
2
3import java.io.BufferedInputStream;
4import java.io.BufferedOutputStream;
5import java.io.IOException;
6import java.net.InetSocketAddress;
7import java.net.Socket;
Mark Slee5bcde6e2006-09-27 17:50:32 +00008import java.net.SocketException;
Mark Slee83c52a82006-06-07 06:51:18 +00009
10/**
11 * Socket implementation of the TTransport interface. To be commented soon!
12 *
13 * @author Mark Slee <mcslee@facebook.com>
14 */
15public class TSocket extends TIOStreamTransport {
16
Mark Slee5bcde6e2006-09-27 17:50:32 +000017 /**
18 * Wrapped Socket object
19 */
Mark Slee83c52a82006-06-07 06:51:18 +000020 private Socket socket_ = null;
21
Mark Slee5bcde6e2006-09-27 17:50:32 +000022 /**
23 * Remote host
24 */
Mark Slee83c52a82006-06-07 06:51:18 +000025 private String host_ = null;
26
Mark Slee5bcde6e2006-09-27 17:50:32 +000027 /**
28 * Remote port
29 */
Mark Slee83c52a82006-06-07 06:51:18 +000030 private int port_ = 0;
31
32 /**
Mark Slee5bcde6e2006-09-27 17:50:32 +000033 * Socket timeout
34 */
35 private int timeout_ = 0;
36
37 /**
Mark Slee83c52a82006-06-07 06:51:18 +000038 * Constructor that takes an already created socket.
39 *
40 * @param socket Already created socket object
41 * @throws TTransportException if there is an error setting up the streams
42 */
43 public TSocket(Socket socket) throws TTransportException {
44 socket_ = socket;
Mark Slee845fe3d2006-09-27 20:51:11 +000045 try {
46 socket_.setSoLinger(false, 0);
47 socket_.setTcpNoDelay(true);
48 } catch (SocketException sx) {
49 sx.printStackTrace();
50 }
51
Mark Slee83c52a82006-06-07 06:51:18 +000052 if (isOpen()) {
53 try {
Mark Slee530fd662006-08-09 00:05:18 +000054 inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
55 outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024);
Mark Slee83c52a82006-06-07 06:51:18 +000056 } catch (IOException iox) {
57 close();
58 throw new TTransportException(iox);
59 }
60 }
61 }
62
63 /**
64 * Creates a new unconnected socket that will connect to the given host
65 * on the given port.
66 *
67 * @param host Remote host
68 * @param port Remote port
69 */
70 public TSocket(String host, int port) {
Mark Slee845fe3d2006-09-27 20:51:11 +000071 this(host, port, 0);
Mark Slee5bcde6e2006-09-27 17:50:32 +000072 }
73
74 /**
75 * Creates a new unconnected socket that will connect to the given host
76 * on the given port.
77 *
78 * @param host Remote host
79 * @param port Remote port
80 * @param timeout Socket timeout
81 */
82 public TSocket(String host, int port, int timeout) {
Mark Slee83c52a82006-06-07 06:51:18 +000083 host_ = host;
84 port_ = port;
Mark Slee5bcde6e2006-09-27 17:50:32 +000085 timeout_ = timeout;
Mark Slee845fe3d2006-09-27 20:51:11 +000086 initSocket();
87 }
88
89 /**
90 * Initializes the socket object
91 */
92 private void initSocket() {
93 socket_ = new Socket();
94 try {
95 socket_.setSoLinger(false, 0);
96 socket_.setTcpNoDelay(true);
97 socket_.setSoTimeout(timeout_);
98 } catch (SocketException sx) {
99 sx.printStackTrace();
100 }
Mark Slee5bcde6e2006-09-27 17:50:32 +0000101 }
102
103 /**
104 * Sets the socket timeout
105 *
106 * @param timeout Milliseconds timeout
107 */
108 public void setTimeout(int timeout) {
109 timeout_ = timeout;
110 try {
111 socket_.setSoTimeout(timeout);
112 } catch (SocketException sx) {
113 sx.printStackTrace();
114 }
Mark Slee83c52a82006-06-07 06:51:18 +0000115 }
116
117 /**
Mark Slee845fe3d2006-09-27 20:51:11 +0000118 * Returns a reference to the underlying socket.
Mark Slee83c52a82006-06-07 06:51:18 +0000119 */
120 public Socket getSocket() {
121 if (socket_ == null) {
Mark Slee845fe3d2006-09-27 20:51:11 +0000122 initSocket();
Mark Slee83c52a82006-06-07 06:51:18 +0000123 }
124 return socket_;
125 }
126
127 /**
128 * Checks whether the socket is connected.
129 */
130 public boolean isOpen() {
131 if (socket_ == null) {
132 return false;
133 }
134 return socket_.isConnected();
135 }
136
137 /**
138 * Connects the socket, creating a new socket object if necessary.
139 */
140 public void open() throws TTransportException {
Mark Slee83c52a82006-06-07 06:51:18 +0000141 if (isOpen()) {
142 throw new TTransportException("Socket already connected.");
143 }
144
Mark Slee845fe3d2006-09-27 20:51:11 +0000145 if (host_.length() == 0) {
146 throw new TTransportException("Cannot open null host.");
147 }
148 if (port_ <= 0) {
149 throw new TTransportException("Cannot open without port.");
150 }
151
152 if (socket_ == null) {
153 initSocket();
154 }
155
Mark Slee83c52a82006-06-07 06:51:18 +0000156 try {
157 socket_.connect(new InetSocketAddress(host_, port_));
Mark Slee530fd662006-08-09 00:05:18 +0000158 inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
159 outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024);
Mark Slee83c52a82006-06-07 06:51:18 +0000160 } catch (IOException iox) {
161 close();
162 throw new TTransportException(iox);
163 }
164 }
165
166 /**
167 * Closes the socket.
168 */
169 public void close() {
170 // Close the underlying streams
171 super.close();
172
173 // Close the socket
174 if (socket_ != null) {
175 try {
176 socket_.close();
177 } catch (IOException iox) {
178 System.err.println("WARNING: exception closing socket: " +
179 iox.getMessage());
180 }
181 socket_ = null;
182 }
183 }
184
185}