Bug 624 - Separate netty and exi specific classes from netconf-util.
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / ssh / virtualsocket / VirtualSocket.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.nettyutil.handler.ssh.virtualsocket;
10
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;
22
23 /**
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.
27  */
28 public class VirtualSocket extends Socket implements ChannelHandler {
29     private static final String INPUT_STREAM = "inputStream";
30     private static final String OUTPUT_STREAM = "outputStream";
31
32     private final ChannelInputStream chis = new ChannelInputStream();
33     private final ChannelOutputStream chos = new ChannelOutputStream();
34     private ChannelHandlerContext ctx;
35
36
37     public InputStream getInputStream() {
38         return this.chis;
39     }
40
41     public OutputStream getOutputStream() {
42         return this.chos;
43     }
44
45     public void handlerAdded(ChannelHandlerContext ctx) {
46         this.ctx = ctx;
47
48         if (ctx.channel().pipeline().get(OUTPUT_STREAM) == null) {
49             ctx.channel().pipeline().addFirst(OUTPUT_STREAM, chos);
50         }
51
52         if (ctx.channel().pipeline().get(INPUT_STREAM) == null) {
53             ctx.channel().pipeline().addFirst(INPUT_STREAM, chis);
54         }
55     }
56
57     public void handlerRemoved(ChannelHandlerContext ctx) {
58         if (ctx.channel().pipeline().get(OUTPUT_STREAM) != null) {
59             ctx.channel().pipeline().remove(OUTPUT_STREAM);
60         }
61
62         if (ctx.channel().pipeline().get(INPUT_STREAM) != null) {
63             ctx.channel().pipeline().remove(INPUT_STREAM);
64         }
65     }
66
67     public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
68         // TODO exceptionCaught is deprecated transform this handler
69         ctx.fireExceptionCaught(throwable);
70     }
71
72     public VirtualSocket() {super();}
73
74     @Override
75     public void connect(SocketAddress endpoint) throws IOException {}
76
77     @Override
78     public void connect(SocketAddress endpoint, int timeout) throws IOException {}
79
80     @Override
81     public void bind(SocketAddress bindpoint) throws IOException {}
82
83     @Override
84     public InetAddress getInetAddress() {
85         InetSocketAddress isa = getInetSocketAddress();
86
87         if (isa == null) {
88             throw new VirtualSocketException();
89         }
90
91         return getInetSocketAddress().getAddress();
92     }
93
94     @Override
95     public InetAddress getLocalAddress() {return null;}
96
97     @Override
98     public int getPort() {
99         return getInetSocketAddress().getPort();
100     }
101
102     private InetSocketAddress getInetSocketAddress() {
103         return (InetSocketAddress)getRemoteSocketAddress();
104     }
105
106     @Override
107     public int getLocalPort() {return -1;}
108
109     @Override
110     public SocketAddress getRemoteSocketAddress() {
111         return this.ctx.channel().remoteAddress();
112     }
113
114     @Override
115     public SocketAddress getLocalSocketAddress() {
116         return this.ctx.channel().localAddress();
117     }
118
119     @Override
120     public SocketChannel getChannel() {return null;}
121
122     @Override
123     public void setTcpNoDelay(boolean on) throws SocketException {}
124
125     @Override
126     public boolean getTcpNoDelay() throws SocketException {return false;}
127
128     @Override
129     public void setSoLinger(boolean on, int linger) throws SocketException {}
130
131     @Override
132     public int getSoLinger() throws SocketException {return -1;}
133
134     @Override
135     public void sendUrgentData(int data) throws IOException {}
136
137     @Override
138     public void setOOBInline(boolean on) throws SocketException {}
139
140     @Override
141     public boolean getOOBInline() throws SocketException {return false;}
142
143     @Override
144     public synchronized void setSoTimeout(int timeout) throws SocketException {}
145
146     @Override
147     public synchronized int getSoTimeout() throws SocketException {return -1;}
148
149     @Override
150     public synchronized void setSendBufferSize(int size) throws SocketException {}
151
152     @Override
153     public synchronized int getSendBufferSize() throws SocketException {return -1;}
154
155     @Override
156     public synchronized void setReceiveBufferSize(int size) throws SocketException {}
157
158     @Override
159     public synchronized int getReceiveBufferSize() throws SocketException {return -1;}
160
161     @Override
162     public void setKeepAlive(boolean on) throws SocketException {}
163
164     @Override
165     public boolean getKeepAlive() throws SocketException {return false;}
166
167     @Override
168     public void setTrafficClass(int tc) throws SocketException {}
169
170     @Override
171     public int getTrafficClass() throws SocketException {return -1;}
172
173     @Override
174     public void setReuseAddress(boolean on) throws SocketException {}
175
176     @Override
177     public boolean getReuseAddress() throws SocketException {return false;}
178
179     @Override
180     public synchronized void close() throws IOException {}
181
182     @Override
183     public void shutdownInput() throws IOException {}
184
185     @Override
186     public void shutdownOutput() throws IOException {}
187
188     @Override
189     public String toString() {
190         return "Virtual socket InetAdress["+getInetAddress()+"], Port["+getPort()+"]";
191     }
192
193     @Override
194     public boolean isConnected() {return false;}
195
196     @Override
197     public boolean isBound() {return false;}
198
199     @Override
200     public boolean isClosed() {return false;}
201
202     @Override
203     public boolean isInputShutdown() {return false;}
204
205     @Override
206     public boolean isOutputShutdown() {return false;}
207
208     @Override
209     public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {}
210 }