blob: 544a1d5892198c975dc68a810ecc4197e5816e53 [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 Slee5bcde6e2006-09-27 17:50:32 +000045
Mark Slee83c52a82006-06-07 06:51:18 +000046 if (isOpen()) {
47 try {
Mark Slee530fd662006-08-09 00:05:18 +000048 inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
49 outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024);
Mark Slee83c52a82006-06-07 06:51:18 +000050 } catch (IOException iox) {
51 close();
52 throw new TTransportException(iox);
53 }
54 }
55 }
56
57 /**
58 * Creates a new unconnected socket that will connect to the given host
59 * on the given port.
60 *
61 * @param host Remote host
62 * @param port Remote port
63 */
64 public TSocket(String host, int port) {
Mark Slee5bcde6e2006-09-27 17:50:32 +000065 this(host, port, 500);
66 }
67
68 /**
69 * Creates a new unconnected socket that will connect to the given host
70 * on the given port.
71 *
72 * @param host Remote host
73 * @param port Remote port
74 * @param timeout Socket timeout
75 */
76 public TSocket(String host, int port, int timeout) {
Mark Slee83c52a82006-06-07 06:51:18 +000077 socket_ = new Socket();
78 host_ = host;
79 port_ = port;
Mark Slee5bcde6e2006-09-27 17:50:32 +000080 timeout_ = timeout;
81 }
82
83 /**
84 * Sets the socket timeout
85 *
86 * @param timeout Milliseconds timeout
87 */
88 public void setTimeout(int timeout) {
89 timeout_ = timeout;
90 try {
91 socket_.setSoTimeout(timeout);
92 } catch (SocketException sx) {
93 sx.printStackTrace();
94 }
Mark Slee83c52a82006-06-07 06:51:18 +000095 }
96
97 /**
98 * Returns a reference to the underlying socket. Can be used to set
99 * socket options, etc. If an underlying socket does not exist yet, this
100 * will create one.
101 */
102 public Socket getSocket() {
103 if (socket_ == null) {
104 socket_ = new Socket();
105 }
106 return socket_;
107 }
108
109 /**
110 * Checks whether the socket is connected.
111 */
112 public boolean isOpen() {
113 if (socket_ == null) {
114 return false;
115 }
116 return socket_.isConnected();
117 }
118
119 /**
120 * Connects the socket, creating a new socket object if necessary.
121 */
122 public void open() throws TTransportException {
123 if (socket_ == null) {
124 if (host_.length() == 0) {
125 throw new TTransportException("Cannot open null host.");
126 }
127 if (port_ <= 0) {
128 throw new TTransportException("Cannot open without port.");
129 }
130 socket_ = new Socket();
131 }
132
133 if (isOpen()) {
134 throw new TTransportException("Socket already connected.");
135 }
136
137 try {
138 socket_.connect(new InetSocketAddress(host_, port_));
Mark Slee530fd662006-08-09 00:05:18 +0000139 inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
140 outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024);
Mark Slee83c52a82006-06-07 06:51:18 +0000141 } catch (IOException iox) {
142 close();
143 throw new TTransportException(iox);
144 }
145 }
146
147 /**
148 * Closes the socket.
149 */
150 public void close() {
151 // Close the underlying streams
152 super.close();
153
154 // Close the socket
155 if (socket_ != null) {
156 try {
157 socket_.close();
158 } catch (IOException iox) {
159 System.err.println("WARNING: exception closing socket: " +
160 iox.getMessage());
161 }
162 socket_ = null;
163 }
164 }
165
166}