Mark Slee | 7eb0d63 | 2007-03-01 00:00:27 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 7 | package com.facebook.thrift.transport; |
| 8 | |
| 9 | import java.io.BufferedInputStream; |
| 10 | import java.io.BufferedOutputStream; |
| 11 | import java.io.IOException; |
| 12 | import java.net.InetSocketAddress; |
| 13 | import java.net.Socket; |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 14 | import java.net.SocketException; |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * Socket implementation of the TTransport interface. To be commented soon! |
| 18 | * |
| 19 | * @author Mark Slee <mcslee@facebook.com> |
| 20 | */ |
| 21 | public class TSocket extends TIOStreamTransport { |
| 22 | |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 23 | /** |
| 24 | * Wrapped Socket object |
| 25 | */ |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 26 | private Socket socket_ = null; |
| 27 | |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Remote host |
| 30 | */ |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 31 | private String host_ = null; |
| 32 | |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 33 | /** |
| 34 | * Remote port |
| 35 | */ |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 36 | private int port_ = 0; |
| 37 | |
| 38 | /** |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 39 | * Socket timeout |
| 40 | */ |
| 41 | private int timeout_ = 0; |
| 42 | |
| 43 | /** |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 44 | * Constructor that takes an already created socket. |
| 45 | * |
| 46 | * @param socket Already created socket object |
| 47 | * @throws TTransportException if there is an error setting up the streams |
| 48 | */ |
| 49 | public TSocket(Socket socket) throws TTransportException { |
| 50 | socket_ = socket; |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame] | 51 | try { |
| 52 | socket_.setSoLinger(false, 0); |
| 53 | socket_.setTcpNoDelay(true); |
| 54 | } catch (SocketException sx) { |
| 55 | sx.printStackTrace(); |
| 56 | } |
| 57 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 58 | if (isOpen()) { |
| 59 | try { |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 60 | inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024); |
| 61 | outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 62 | } catch (IOException iox) { |
| 63 | close(); |
Mark Slee | b46c041 | 2007-02-21 04:54:38 +0000 | [diff] [blame] | 64 | throw new TTransportException(TTransportException.NOT_OPEN, iox); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates a new unconnected socket that will connect to the given host |
| 71 | * on the given port. |
| 72 | * |
| 73 | * @param host Remote host |
| 74 | * @param port Remote port |
| 75 | */ |
| 76 | public TSocket(String host, int port) { |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame] | 77 | this(host, port, 0); |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Creates a new unconnected socket that will connect to the given host |
| 82 | * on the given port. |
| 83 | * |
| 84 | * @param host Remote host |
| 85 | * @param port Remote port |
| 86 | * @param timeout Socket timeout |
| 87 | */ |
| 88 | public TSocket(String host, int port, int timeout) { |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 89 | host_ = host; |
| 90 | port_ = port; |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 91 | timeout_ = timeout; |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame] | 92 | initSocket(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Initializes the socket object |
| 97 | */ |
| 98 | private void initSocket() { |
| 99 | socket_ = new Socket(); |
| 100 | try { |
| 101 | socket_.setSoLinger(false, 0); |
| 102 | socket_.setTcpNoDelay(true); |
| 103 | socket_.setSoTimeout(timeout_); |
| 104 | } catch (SocketException sx) { |
| 105 | sx.printStackTrace(); |
| 106 | } |
Mark Slee | 5bcde6e | 2006-09-27 17:50:32 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Sets the socket timeout |
| 111 | * |
| 112 | * @param timeout Milliseconds timeout |
| 113 | */ |
| 114 | public void setTimeout(int timeout) { |
| 115 | timeout_ = timeout; |
| 116 | try { |
| 117 | socket_.setSoTimeout(timeout); |
| 118 | } catch (SocketException sx) { |
| 119 | sx.printStackTrace(); |
| 120 | } |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame] | 124 | * Returns a reference to the underlying socket. |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 125 | */ |
| 126 | public Socket getSocket() { |
| 127 | if (socket_ == null) { |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame] | 128 | initSocket(); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 129 | } |
| 130 | return socket_; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Checks whether the socket is connected. |
| 135 | */ |
| 136 | public boolean isOpen() { |
| 137 | if (socket_ == null) { |
| 138 | return false; |
| 139 | } |
| 140 | return socket_.isConnected(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Connects the socket, creating a new socket object if necessary. |
| 145 | */ |
| 146 | public void open() throws TTransportException { |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 147 | if (isOpen()) { |
Mark Slee | b46c041 | 2007-02-21 04:54:38 +0000 | [diff] [blame] | 148 | throw new TTransportException(TTransportException.ALREADY_OPEN, "Socket already connected."); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame] | 151 | if (host_.length() == 0) { |
Mark Slee | b46c041 | 2007-02-21 04:54:38 +0000 | [diff] [blame] | 152 | throw new TTransportException(TTransportException.NOT_OPEN, "Cannot open null host."); |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame] | 153 | } |
| 154 | if (port_ <= 0) { |
Mark Slee | b46c041 | 2007-02-21 04:54:38 +0000 | [diff] [blame] | 155 | throw new TTransportException(TTransportException.NOT_OPEN, "Cannot open without port."); |
Mark Slee | 845fe3d | 2006-09-27 20:51:11 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | if (socket_ == null) { |
| 159 | initSocket(); |
| 160 | } |
| 161 | |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 162 | try { |
| 163 | socket_.connect(new InetSocketAddress(host_, port_)); |
Mark Slee | 530fd66 | 2006-08-09 00:05:18 +0000 | [diff] [blame] | 164 | inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024); |
| 165 | outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 166 | } catch (IOException iox) { |
| 167 | close(); |
Mark Slee | b46c041 | 2007-02-21 04:54:38 +0000 | [diff] [blame] | 168 | throw new TTransportException(TTransportException.NOT_OPEN, iox); |
Mark Slee | 83c52a8 | 2006-06-07 06:51:18 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Closes the socket. |
| 174 | */ |
| 175 | public void close() { |
| 176 | // Close the underlying streams |
| 177 | super.close(); |
| 178 | |
| 179 | // Close the socket |
| 180 | if (socket_ != null) { |
| 181 | try { |
| 182 | socket_.close(); |
| 183 | } catch (IOException iox) { |
| 184 | System.err.println("WARNING: exception closing socket: " + |
| 185 | iox.getMessage()); |
| 186 | } |
| 187 | socket_ = null; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | } |