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; |
| 8 | |
| 9 | /** |
| 10 | * Socket implementation of the TTransport interface. To be commented soon! |
| 11 | * |
| 12 | * @author Mark Slee <mcslee@facebook.com> |
| 13 | */ |
| 14 | public class TSocket extends TIOStreamTransport { |
| 15 | |
| 16 | /** Wrapped Socket object */ |
| 17 | private Socket socket_ = null; |
| 18 | |
| 19 | /** Remote host */ |
| 20 | private String host_ = null; |
| 21 | |
| 22 | /** Remote port */ |
| 23 | private int port_ = 0; |
| 24 | |
| 25 | /** |
| 26 | * Constructor that takes an already created socket. |
| 27 | * |
| 28 | * @param socket Already created socket object |
| 29 | * @throws TTransportException if there is an error setting up the streams |
| 30 | */ |
| 31 | public TSocket(Socket socket) throws TTransportException { |
| 32 | socket_ = socket; |
| 33 | if (isOpen()) { |
| 34 | try { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 35 | inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024); |
| 36 | outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 37 | } catch (IOException iox) { |
| 38 | close(); |
| 39 | throw new TTransportException(iox); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Creates a new unconnected socket that will connect to the given host |
| 46 | * on the given port. |
| 47 | * |
| 48 | * @param host Remote host |
| 49 | * @param port Remote port |
| 50 | */ |
| 51 | public TSocket(String host, int port) { |
| 52 | socket_ = new Socket(); |
| 53 | host_ = host; |
| 54 | port_ = port; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns a reference to the underlying socket. Can be used to set |
| 59 | * socket options, etc. If an underlying socket does not exist yet, this |
| 60 | * will create one. |
| 61 | */ |
| 62 | public Socket getSocket() { |
| 63 | if (socket_ == null) { |
| 64 | socket_ = new Socket(); |
| 65 | } |
| 66 | return socket_; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Checks whether the socket is connected. |
| 71 | */ |
| 72 | public boolean isOpen() { |
| 73 | if (socket_ == null) { |
| 74 | return false; |
| 75 | } |
| 76 | return socket_.isConnected(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Connects the socket, creating a new socket object if necessary. |
| 81 | */ |
| 82 | public void open() throws TTransportException { |
| 83 | if (socket_ == null) { |
| 84 | if (host_.length() == 0) { |
| 85 | throw new TTransportException("Cannot open null host."); |
| 86 | } |
| 87 | if (port_ <= 0) { |
| 88 | throw new TTransportException("Cannot open without port."); |
| 89 | } |
| 90 | socket_ = new Socket(); |
| 91 | } |
| 92 | |
| 93 | if (isOpen()) { |
| 94 | throw new TTransportException("Socket already connected."); |
| 95 | } |
| 96 | |
| 97 | try { |
| 98 | socket_.connect(new InetSocketAddress(host_, port_)); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame^] | 99 | inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024); |
| 100 | outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 101 | } catch (IOException iox) { |
| 102 | close(); |
| 103 | throw new TTransportException(iox); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Closes the socket. |
| 109 | */ |
| 110 | public void close() { |
| 111 | // Close the underlying streams |
| 112 | super.close(); |
| 113 | |
| 114 | // Close the socket |
| 115 | if (socket_ != null) { |
| 116 | try { |
| 117 | socket_.close(); |
| 118 | } catch (IOException iox) { |
| 119 | System.err.println("WARNING: exception closing socket: " + |
| 120 | iox.getMessage()); |
| 121 | } |
| 122 | socket_ = null; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } |