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 final ChannelInputStream chis = new ChannelInputStream();
30 private final ChannelOutputStream chos = new ChannelOutputStream();
31 private ChannelHandlerContext ctx;
34 public InputStream getInputStream() {
38 public OutputStream getOutputStream() {
42 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
45 if (ctx.channel().pipeline().get("outputStream") == null) {
46 ctx.channel().pipeline().addFirst("outputStream", chos);
49 if (ctx.channel().pipeline().get("inputStream") == null) {
50 ctx.channel().pipeline().addFirst("inputStream", chis);
54 public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
55 if (ctx.channel().pipeline().get("outputStream") != null) {
56 ctx.channel().pipeline().remove("outputStream");
59 if (ctx.channel().pipeline().get("inputStream") != null) {
60 ctx.channel().pipeline().remove("inputStream");
64 public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) throws Exception {
65 ctx.fireExceptionCaught(throwable);
68 public VirtualSocket() {super();}
71 public void connect(SocketAddress endpoint) throws IOException {}
74 public void connect(SocketAddress endpoint, int timeout) throws IOException {}
77 public void bind(SocketAddress bindpoint) throws IOException {}
80 public InetAddress getInetAddress() {
81 InetSocketAddress isa = getInetSocketAddress();
83 if (isa == null) throw new VirtualSocketException();
85 return getInetSocketAddress().getAddress();
89 public InetAddress getLocalAddress() {return null;}
92 public int getPort() {
93 return getInetSocketAddress().getPort();
96 private InetSocketAddress getInetSocketAddress() {
97 return (InetSocketAddress)getRemoteSocketAddress();
101 public int getLocalPort() {return -1;}
104 public SocketAddress getRemoteSocketAddress() {
105 return this.ctx.channel().remoteAddress();
109 public SocketAddress getLocalSocketAddress() {
110 return this.ctx.channel().localAddress();
114 public SocketChannel getChannel() {return null;}
117 public void setTcpNoDelay(boolean on) throws SocketException {}
120 public boolean getTcpNoDelay() throws SocketException {return false;}
123 public void setSoLinger(boolean on, int linger) throws SocketException {}
126 public int getSoLinger() throws SocketException {return -1;}
129 public void sendUrgentData(int data) throws IOException {}
132 public void setOOBInline(boolean on) throws SocketException {}
135 public boolean getOOBInline() throws SocketException {return false;}
138 public synchronized void setSoTimeout(int timeout) throws SocketException {}
141 public synchronized int getSoTimeout() throws SocketException {return -1;}
144 public synchronized void setSendBufferSize(int size) throws SocketException {}
147 public synchronized int getSendBufferSize() throws SocketException {return -1;}
150 public synchronized void setReceiveBufferSize(int size) throws SocketException {}
153 public synchronized int getReceiveBufferSize() throws SocketException {return -1;}
156 public void setKeepAlive(boolean on) throws SocketException {}
159 public boolean getKeepAlive() throws SocketException {return false;}
162 public void setTrafficClass(int tc) throws SocketException {}
165 public int getTrafficClass() throws SocketException {return -1;}
168 public void setReuseAddress(boolean on) throws SocketException {}
171 public boolean getReuseAddress() throws SocketException {return false;}
174 public synchronized void close() throws IOException {}
177 public void shutdownInput() throws IOException {}
180 public void shutdownOutput() throws IOException {}
183 public String toString() {
184 return "Virtual socket InetAdress["+getInetAddress()+"], Port["+getPort()+"]";
188 public boolean isConnected() {return false;}
191 public boolean isBound() {return false;}
194 public boolean isClosed() {return false;}
197 public boolean isInputShutdown() {return false;}
200 public boolean isOutputShutdown() {return false;}
203 public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {}