Merge "creating a default subnet"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / handler / ssh / virtualsocket / ChannelOutputStream.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.util.handler.ssh.virtualsocket;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelOutboundHandler;
15 import io.netty.channel.ChannelPromise;
16
17 import java.io.IOException;
18 import java.io.OutputStream;
19 import java.net.SocketAddress;
20
21 /**
22  * Class provides {@link OutputStream) functionality to users of virtual socket.
23  */
24 public class ChannelOutputStream extends OutputStream implements ChannelOutboundHandler {
25     private final Object lock = new Object();
26     private ByteBuf buff = Unpooled.buffer();
27     private ChannelHandlerContext ctx;
28
29     @Override
30     public void flush() throws IOException {
31         synchronized(lock) {
32             ctx.writeAndFlush(buff).awaitUninterruptibly();
33             buff = Unpooled.buffer();
34         }
35     }
36
37     @Override
38     public void write(int b) throws IOException {
39         synchronized(lock) {
40             buff.writeByte(b);
41         }
42     }
43
44     public void bind(ChannelHandlerContext ctx, SocketAddress localAddress,
45                      ChannelPromise promise) throws Exception {
46         ctx.bind(localAddress, promise);
47     }
48
49     public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
50                         SocketAddress localAddress, ChannelPromise promise)
51             throws Exception {
52         this.ctx = ctx;
53         ctx.connect(remoteAddress, localAddress, promise);
54     }
55
56     public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise)
57             throws Exception {
58         ctx.disconnect(promise);
59     }
60
61     public void close(ChannelHandlerContext ctx, ChannelPromise promise)
62             throws Exception {
63         ctx.close(promise);
64     }
65
66     public void deregister(ChannelHandlerContext ctx, ChannelPromise channelPromise)
67             throws Exception {
68         ctx.deregister(channelPromise);
69     }
70
71     public void read(ChannelHandlerContext ctx)
72             throws Exception {
73         ctx.read();
74     }
75
76     public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
77             throws Exception {
78         // pass
79     }
80
81     public void flush(ChannelHandlerContext ctx)
82             throws Exception {
83         // pass
84     }
85
86     public void handlerAdded(ChannelHandlerContext ctx)
87             throws Exception {
88     }
89
90     public void handlerRemoved(ChannelHandlerContext ctx)
91             throws Exception {
92     }
93
94     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
95             throws Exception {
96         ctx.fireExceptionCaught(cause);
97     }
98 }