2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.nettyutil.handler.ssh.virtualsocket;
11 import io.netty.channel.ChannelHandler;
12 import io.netty.channel.ChannelHandlerContext;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.OutputStream;
16 import java.net.InetAddress;
17 import java.net.InetSocketAddress;
18 import java.net.Socket;
19 import java.net.SocketAddress;
20 import java.net.SocketException;
21 import java.nio.channels.SocketChannel;
24 * Handler class providing Socket functionality to OIO client application. By using VirtualSocket user can
25 * use OIO application in asynchronous environment and NIO EventLoop. Using VirtualSocket OIO applications
26 * are able to use full potential of NIO environment.
28 //TODO: refactor - socket should be created when connection is established
29 public class VirtualSocket extends Socket implements ChannelHandler {
30 private static final String INPUT_STREAM = "inputStream";
31 private static final String OUTPUT_STREAM = "outputStream";
33 private final ChannelInputStream chais = new ChannelInputStream();
34 private final ChannelOutputStream chaos = new ChannelOutputStream();
35 private ChannelHandlerContext ctx;
38 public InputStream getInputStream() {
42 public OutputStream getOutputStream() {
46 public void handlerAdded(ChannelHandlerContext ctx) {
49 if (ctx.channel().pipeline().get(OUTPUT_STREAM) == null) {
50 ctx.channel().pipeline().addFirst(OUTPUT_STREAM, chaos);
53 if (ctx.channel().pipeline().get(INPUT_STREAM) == null) {
54 ctx.channel().pipeline().addFirst(INPUT_STREAM, chais);
58 public void handlerRemoved(ChannelHandlerContext ctx) {
59 if (ctx.channel().pipeline().get(OUTPUT_STREAM) != null) {
60 ctx.channel().pipeline().remove(OUTPUT_STREAM);
63 if (ctx.channel().pipeline().get(INPUT_STREAM) != null) {
64 ctx.channel().pipeline().remove(INPUT_STREAM);
68 public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
69 // TODO exceptionCaught is deprecated transform this handler
70 ctx.fireExceptionCaught(throwable);
75 public void connect(SocketAddress endpoint) throws IOException {}
78 public void connect(SocketAddress endpoint, int timeout) throws IOException {}
81 public void bind(SocketAddress bindpoint) throws IOException {}
84 public InetAddress getInetAddress() {
85 InetSocketAddress isa = getInetSocketAddress();
86 return isa.getAddress();
90 public InetAddress getLocalAddress() {return null;}
93 public int getPort() {
94 return getInetSocketAddress().getPort();
97 private InetSocketAddress getInetSocketAddress() {
98 return (InetSocketAddress)getRemoteSocketAddress();
102 public int getLocalPort() {return -1;}
105 public SocketAddress getRemoteSocketAddress() {
106 return this.ctx.channel().remoteAddress();
110 public SocketAddress getLocalSocketAddress() {
111 return this.ctx.channel().localAddress();
115 public SocketChannel getChannel() {return null;}
118 public void setTcpNoDelay(boolean on) throws SocketException {}
121 public boolean getTcpNoDelay() throws SocketException {return false;}
124 public void setSoLinger(boolean on, int linger) throws SocketException {}
127 public int getSoLinger() throws SocketException {return -1;}
130 public void sendUrgentData(int data) throws IOException {}
133 public void setOOBInline(boolean on) throws SocketException {}
136 public boolean getOOBInline() throws SocketException {return false;}
139 public synchronized void setSoTimeout(int timeout) throws SocketException {}
142 public synchronized int getSoTimeout() throws SocketException {return -1;}
145 public synchronized void setSendBufferSize(int size) throws SocketException {}
148 public synchronized int getSendBufferSize() throws SocketException {return -1;}
151 public synchronized void setReceiveBufferSize(int size) throws SocketException {}
154 public synchronized int getReceiveBufferSize() throws SocketException {return -1;}
157 public void setKeepAlive(boolean on) throws SocketException {}
160 public boolean getKeepAlive() throws SocketException {return false;}
163 public void setTrafficClass(int tc) throws SocketException {}
166 public int getTrafficClass() throws SocketException {return -1;}
169 public void setReuseAddress(boolean on) throws SocketException {}
172 public boolean getReuseAddress() throws SocketException {return false;}
175 public synchronized void close() throws IOException {}
178 public void shutdownInput() throws IOException {}
181 public void shutdownOutput() throws IOException {}
184 public String toString() {
185 return "VirtualSocket{" + getInetAddress() + ":" + getPort() + "}";
189 public boolean isConnected() {return false;}
192 public boolean isBound() {return false;}
195 public boolean isClosed() {return false;}
198 public boolean isInputShutdown() {return false;}
201 public boolean isOutputShutdown() {return false;}
204 public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {}