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 | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 45 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 46 | if (isOpen()) { |
| 47 | try { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 48 | inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024); |
| 49 | outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 50 | } 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 Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 65 | 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 Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 77 | socket_ = new Socket(); |
| 78 | host_ = host; |
| 79 | port_ = port; |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 80 | 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 Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 95 | } |
| 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 Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 139 | inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024); |
| 140 | outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 141 | } 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 | } |