00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.ssh.rmissh;
00032
00033 import java.io.*;
00034 import java.net.*;
00035 import java.nio.channels.*;
00036
00037 import org.apache.log4j.Logger;
00038 import org.objectweb.proactive.core.ssh.SshParameters;
00039 import org.objectweb.proactive.core.ssh.SshTunnel;
00040 import org.objectweb.proactive.core.ssh.SshTunnelFactory;
00041 import org.objectweb.proactive.core.ssh.TryCache;
00042 import org.objectweb.proactive.core.util.log.Loggers;
00043 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00044
00045
00052 public class SshSocket extends Socket {
00053 static Logger logger = ProActiveLogger.getLogger(Loggers.SSH);
00054 private SshTunnel _tunnel;
00055 private Socket _socket;
00056 static private TryCache _tryCache = null;
00057
00058 static private TryCache getTryCache() {
00059 if (_tryCache == null) {
00060 _tryCache = new TryCache();
00061 }
00062 return _tryCache;
00063 }
00064
00065 protected void finalize() throws Throwable {
00066 super.finalize();
00067 logger.debug("finalizing SshSocket for tunnel " + _tunnel.getPort());
00068 SshTunnelFactory.reportUnusedTunnel(_tunnel);
00069 _tunnel = null;
00070 _socket.close();
00071 _socket = null;
00072 }
00073
00074 public SshSocket(String host, int port) throws IOException {
00075 logger.debug("try socket to " + host + ":" + port);
00076 if (SshParameters.getTryNormalFirst() &&
00077 getTryCache().needToTry(host, port)) {
00078 try {
00079 InetSocketAddress address = new InetSocketAddress(host, port);
00080 _socket = new Socket();
00081 _socket.connect(address, SshParameters.getConnectTimeout());
00082 getTryCache().recordTrySuccess(host, port);
00083 logger.debug("success normal socket to " + host + ":" + port);
00084 return;
00085 } catch (Exception e) {
00086 logger.debug("failure normal socket to " + host + ":" + port);
00087 getTryCache().recordTryFailure(host, port);
00088 _socket = null;
00089 }
00090 }
00091 logger.debug("try ssh socket to " + host + ":" + port);
00092 _tunnel = SshTunnelFactory.createTunnel(host, port);
00093 InetSocketAddress address = new InetSocketAddress("127.0.0.1",
00094 _tunnel.getPort());
00095 _socket = new Socket();
00096 _socket.connect(address, SshParameters.getConnectTimeout());
00097 logger.debug("Opened TCP connection 127.0.0.1:" + _tunnel.getPort() +
00098 " -> " + host + ":" + port);
00099 }
00100
00101 public void connect() throws IOException {
00102
00103 }
00104
00105 public void connect(SocketAddress endpoint, int timeout)
00106 throws IOException {
00107
00108 }
00109
00110 public void bind(SocketAddress bindpoint) throws IOException {
00111
00112 }
00113
00114 public InetAddress getInetAddress() {
00115 try {
00116 return _tunnel.getInetAddress();
00117 } catch (UnknownHostException e) {
00118 return null;
00119 }
00120 }
00121
00122 public InetAddress getLocalAddress() {
00123 return _socket.getLocalAddress();
00124 }
00125
00126 public int getPort() {
00127 return _tunnel.getPort();
00128 }
00129
00130 public int getLocalPort() {
00131 return _socket.getLocalPort();
00132 }
00133
00134 public SocketAddress getRemoteSocketAddress() {
00135 return new InetSocketAddress(getInetAddress(), getPort());
00136 }
00137
00138 public SocketAddress getLocalSocketAddress() {
00139 return new InetSocketAddress(getLocalAddress(), getLocalPort());
00140 }
00141
00142 public SocketChannel getChannel() {
00143 return _socket.getChannel();
00144 }
00145
00146 public InputStream getInputStream() throws IOException {
00147 return _socket.getInputStream();
00148 }
00149
00150 public OutputStream getOutputStream() throws IOException {
00151 return _socket.getOutputStream();
00152 }
00153
00154 public void setTcpNoDelay(boolean on) throws SocketException {
00155 _socket.setTcpNoDelay(on);
00156 }
00157
00158 public boolean getTcpNoDelay() throws SocketException {
00159 return _socket.getTcpNoDelay();
00160 }
00161
00162 public void setSoLinger(boolean on, int linger) throws SocketException {
00163 _socket.setSoLinger(on, linger);
00164 }
00165
00166 public int getSoLinger() throws SocketException {
00167 return _socket.getSoLinger();
00168 }
00169
00170 public void sendUrgentData(int data) throws IOException {
00171 _socket.sendUrgentData(data);
00172 }
00173
00174 public void setOOBInline(boolean on) throws SocketException {
00175 _socket.setOOBInline(on);
00176 }
00177
00178 public boolean getOOBInline() throws SocketException {
00179 return _socket.getOOBInline();
00180 }
00181
00182 public synchronized void setSoTimeout(int timeout)
00183 throws SocketException {
00184 _socket.setSoTimeout(timeout);
00185 }
00186
00187 public synchronized int getSoTimeout() throws SocketException {
00188 return _socket.getSoTimeout();
00189 }
00190
00191 public synchronized void setSendBufferSize(int size)
00192 throws SocketException {
00193 _socket.setSendBufferSize(size);
00194 }
00195
00196 public synchronized int getSendBufferSize() throws SocketException {
00197 return _socket.getSendBufferSize();
00198 }
00199
00200 public synchronized void setReceiveBufferSize(int size)
00201 throws SocketException {
00202 _socket.setReceiveBufferSize(size);
00203 }
00204
00205 public synchronized int getReceiveBufferSize() throws SocketException {
00206 return _socket.getReceiveBufferSize();
00207 }
00208
00209 public void setKeepAlive(boolean on) throws SocketException {
00210 _socket.setKeepAlive(on);
00211 }
00212
00213 public boolean getKeepAlive() throws SocketException {
00214 return _socket.getKeepAlive();
00215 }
00216
00217 public void setTrafficClass(int tc) throws SocketException {
00218 _socket.setTrafficClass(tc);
00219 }
00220
00221 public int getTrafficClass() throws SocketException {
00222 return _socket.getTrafficClass();
00223 }
00224
00225 public void setReuseAddress(boolean on) throws SocketException {
00226 _socket.setReuseAddress(on);
00227 }
00228
00229 public boolean getReuseAddress() throws SocketException {
00230 return _socket.getReuseAddress();
00231 }
00232
00233 public synchronized void close() throws IOException {
00234 _socket.close();
00235 try {
00236 SshTunnelFactory.reportUnusedTunnel(_tunnel);
00237 } catch (Exception e) {
00238 e.printStackTrace();
00239 }
00240 _tunnel = null;
00241 }
00242
00243 public void shutdownInput() throws IOException {
00244 _socket.shutdownInput();
00245 }
00246
00247 public void shutdownOutput() throws IOException {
00248 _socket.shutdownOutput();
00249 }
00250
00251 public String toString() {
00252 return _socket.toString();
00253 }
00254
00255 public boolean isConnected() {
00256 return _socket.isConnected();
00257 }
00258
00259 public boolean isBound() {
00260 return _socket.isBound();
00261 }
00262
00263 public boolean isClosed() {
00264 return _socket.isClosed();
00265 }
00266
00267 public boolean isInputShutdown() {
00268 return _socket.isInputShutdown();
00269 }
00270
00271 public boolean isOutputShutdown() {
00272 return _socket.isOutputShutdown();
00273 }
00274 }