Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 1 | package com.facebook.thrift.transport; |
| 2 | |
| 3 | import java.io.BufferedInputStream; |
| 4 | import java.io.BufferedOutputStream; |
| 5 | import java.io.IOException; |
| 6 | import java.net.InetSocketAddress; |
| 7 | import java.net.Socket; |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 8 | import java.net.SocketException; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Socket implementation of the TTransport interface. To be commented soon! |
| 12 | * |
| 13 | * @author Mark Slee <mcslee@facebook.com> |
| 14 | */ |
| 15 | public class TSocket extends TIOStreamTransport { |
| 16 | |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 17 | /** |
| 18 | * Wrapped Socket object |
| 19 | */ |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 20 | private Socket socket_ = null; |
| 21 | |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 22 | /** |
| 23 | * Remote host |
| 24 | */ |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 25 | private String host_ = null; |
| 26 | |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Remote port |
| 29 | */ |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 30 | private int port_ = 0; |
| 31 | |
| 32 | /** |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 33 | * Socket timeout |
| 34 | */ |
| 35 | private int timeout_ = 0; |
| 36 | |
| 37 | /** |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 38 | * 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 Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame^] | 45 | try { |
| 46 | socket_.setSoLinger(false, 0); |
| 47 | socket_.setTcpNoDelay(true); |
| 48 | } catch (SocketException sx) { |
| 49 | sx.printStackTrace(); |
| 50 | } |
| 51 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 52 | if (isOpen()) { |
| 53 | try { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 54 | inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024); |
| 55 | outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 56 | } 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 Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame^] | 71 | this(host, port, 0); |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 72 | } |
| 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 Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 83 | host_ = host; |
| 84 | port_ = port; |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 85 | timeout_ = timeout; |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame^] | 86 | 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 Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 101 | } |
| 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 Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame^] | 118 | * Returns a reference to the underlying socket. |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 119 | */ |
| 120 | public Socket getSocket() { |
| 121 | if (socket_ == null) { |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame^] | 122 | initSocket(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 123 | } |
| 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 Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 141 | if (isOpen()) { |
| 142 | throw new TTransportException("Socket already connected."); |
| 143 | } |
| 144 | |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame^] | 145 | 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 Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 156 | try { |
| 157 | socket_.connect(new InetSocketAddress(host_, port_)); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 158 | inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024); |
| 159 | outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 160 | } 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 | } |