1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 package nginx.unit.websocket;
18 
19 import java.io.IOException;
20 import java.nio.ByteBuffer;
21 import java.nio.channels.AsynchronousSocketChannel;
22 import java.nio.channels.CompletionHandler;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.Future;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
27 
28 /**
29  * Generally, just passes calls straight to the wrapped
30  * {@link AsynchronousSocketChannel}. In some cases exceptions may be swallowed
31  * to save them being swallowed by the calling code.
32  */
33 public class AsyncChannelWrapperNonSecure implements AsyncChannelWrapper {
34 
35     private static final Future<Void> NOOP_FUTURE = new NoOpFuture();
36 
37     private final AsynchronousSocketChannel socketChannel;
38 
AsyncChannelWrapperNonSecure( AsynchronousSocketChannel socketChannel)39     public AsyncChannelWrapperNonSecure(
40             AsynchronousSocketChannel socketChannel) {
41         this.socketChannel = socketChannel;
42     }
43 
44     @Override
read(ByteBuffer dst)45     public Future<Integer> read(ByteBuffer dst) {
46         return socketChannel.read(dst);
47     }
48 
49     @Override
read(ByteBuffer dst, A attachment, CompletionHandler<Integer,B> handler)50     public <B,A extends B> void read(ByteBuffer dst, A attachment,
51             CompletionHandler<Integer,B> handler) {
52         socketChannel.read(dst, attachment, handler);
53     }
54 
55     @Override
write(ByteBuffer src)56     public Future<Integer> write(ByteBuffer src) {
57         return socketChannel.write(src);
58     }
59 
60     @Override
write(ByteBuffer[] srcs, int offset, int length, long timeout, TimeUnit unit, A attachment, CompletionHandler<Long,B> handler)61     public <B,A extends B> void write(ByteBuffer[] srcs, int offset, int length,
62             long timeout, TimeUnit unit, A attachment,
63             CompletionHandler<Long,B> handler) {
64         socketChannel.write(
65                 srcs, offset, length, timeout, unit, attachment, handler);
66     }
67 
68     @Override
close()69     public void close() {
70         try {
71             socketChannel.close();
72         } catch (IOException e) {
73             // Ignore
74         }
75     }
76 
77     @Override
handshake()78     public Future<Void> handshake() {
79         return NOOP_FUTURE;
80     }
81 
82 
83     private static final class NoOpFuture implements Future<Void> {
84 
85         @Override
cancel(boolean mayInterruptIfRunning)86         public boolean cancel(boolean mayInterruptIfRunning) {
87             return false;
88         }
89 
90         @Override
isCancelled()91         public boolean isCancelled() {
92             return false;
93         }
94 
95         @Override
isDone()96         public boolean isDone() {
97             return true;
98         }
99 
100         @Override
get()101         public Void get() throws InterruptedException, ExecutionException {
102             return null;
103         }
104 
105         @Override
get(long timeout, TimeUnit unit)106         public Void get(long timeout, TimeUnit unit)
107                 throws InterruptedException, ExecutionException,
108                 TimeoutException {
109             return null;
110         }
111     }
112 }
113