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.util.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 public class VirtualSocket extends Socket implements ChannelHandler {
29 private static final String INPUT_STREAM = "inputStream";
30 private static final String OUTPUT_STREAM = "outputStream";
32 private final ChannelInputStream chis = new ChannelInputStream();
33 private final ChannelOutputStream chos = new ChannelOutputStream();
34 private ChannelHandlerContext ctx;
37 public InputStream getInputStream() {
41 public OutputStream getOutputStream() {
45 public void handlerAdded(ChannelHandlerContext ctx) {
48 if (ctx.channel().pipeline().get(OUTPUT_STREAM) == null) {
49 ctx.channel().pipeline().addFirst(OUTPUT_STREAM, chos);
52 if (ctx.channel().pipeline().get(INPUT_STREAM) == null) {
53 ctx.channel().pipeline().addFirst(INPUT_STREAM, chis);
57 public void handlerRemoved(ChannelHandlerContext ctx) {
58 if (ctx.channel().pipeline().get(OUTPUT_STREAM) != null) {
59 ctx.channel().pipeline().remove(OUTPUT_STREAM);
62 if (ctx.channel().pipeline().get(INPUT_STREAM) != null) {
63 ctx.channel().pipeline().remove(INPUT_STREAM);
67 public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
68 // TODO exceptionCaught is deprecated transform this handler
69 ctx.fireExceptionCaught(throwable);
72 public VirtualSocket() {super();}
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();
88 throw new VirtualSocketException();
91 return getInetSocketAddress().getAddress();
95 public InetAddress getLocalAddress() {return null;}
98 public int getPort() {
99 return getInetSocketAddress().getPort();
102 private InetSocketAddress getInetSocketAddress() {
103 return (InetSocketAddress)getRemoteSocketAddress();
107 public int getLocalPort() {return -1;}
110 public SocketAddress getRemoteSocketAddress() {
111 return this.ctx.channel().remoteAddress();
115 public SocketAddress getLocalSocketAddress() {
116 return this.ctx.channel().localAddress();
120 public SocketChannel getChannel() {return null;}
123 public void setTcpNoDelay(boolean on) throws SocketException {}
126 public boolean getTcpNoDelay() throws SocketException {return false;}
129 public void setSoLinger(boolean on, int linger) throws SocketException {}
132 public int getSoLinger() throws SocketException {return -1;}
135 public void sendUrgentData(int data) throws IOException {}
138 public void setOOBInline(boolean on) throws SocketException {}
141 public boolean getOOBInline() throws SocketException {return false;}
144 public synchronized void setSoTimeout(int timeout) throws SocketException {}
147 public synchronized int getSoTimeout() throws SocketException {return -1;}
150 public synchronized void setSendBufferSize(int size) throws SocketException {}
153 public synchronized int getSendBufferSize() throws SocketException {return -1;}
156 public synchronized void setReceiveBufferSize(int size) throws SocketException {}
159 public synchronized int getReceiveBufferSize() throws SocketException {return -1;}
162 public void setKeepAlive(boolean on) throws SocketException {}
165 public boolean getKeepAlive() throws SocketException {return false;}
168 public void setTrafficClass(int tc) throws SocketException {}
171 public int getTrafficClass() throws SocketException {return -1;}
174 public void setReuseAddress(boolean on) throws SocketException {}
177 public boolean getReuseAddress() throws SocketException {return false;}
180 public synchronized void close() throws IOException {}
183 public void shutdownInput() throws IOException {}
186 public void shutdownOutput() throws IOException {}
189 public String toString() {
190 return "Virtual socket InetAdress["+getInetAddress()+"], Port["+getPort()+"]";
194 public boolean isConnected() {return false;}
197 public boolean isBound() {return false;}
200 public boolean isClosed() {return false;}
203 public boolean isInputShutdown() {return false;}
206 public boolean isOutputShutdown() {return false;}
209 public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {}