1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Copyright 2013 Red Hat, Inc. and/or its affiliates.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.xnio.conduits;
20
21 import org.xnio.ChannelListener;
22 import org.xnio.ChannelListeners;
23 import org.xnio.IoUtils;
24 import org.xnio.channels.CloseListenerSettable;
25 import org.xnio.channels.ReadListenerSettable;
26 import org.xnio.channels.SuspendableReadChannel;
27
28 /**
29  * A conduit read-ready handler.
30  *
31  * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
32  */

33 public interface ReadReadyHandler extends TerminateHandler {
34
35     /**
36      * Signify that reads are ready.
37      */

38     void readReady();
39
40     /**
41      * A read ready handler which calls channel listener(s).
42      *
43      * @param <C> the channel type
44      */

45     class ChannelListenerHandler<C extends SuspendableReadChannel & ReadListenerSettable<C> & CloseListenerSettable<C>> implements ReadReadyHandler {
46         private final C channel;
47
48         /**
49          * Construct a new instance.
50          *
51          * @param channel the channel
52          */

53         public ChannelListenerHandler(final C channel) {
54             this.channel = channel;
55         }
56
57         public void forceTermination() {
58             IoUtils.safeClose(channel);
59         }
60
61         public void readReady() {
62             final ChannelListener<? super C> readListener = channel.getReadListener();
63             if (readListener == null) {
64                 channel.suspendReads();
65             } else {
66                 ChannelListeners.invokeChannelListener(channel, readListener);
67             }
68         }
69
70         public void terminated() {
71             ChannelListeners.invokeChannelListener(channel, channel.getCloseListener());
72         }
73     }
74
75     /**
76      * A runnable task which invokes the {@link ReadReadyHandler#readReady()} method of the given handler.
77      */

78     class ReadyTask implements Runnable {
79
80         private final ReadReadyHandler handler;
81
82         /**
83          * Construct a new instance.
84          *
85          * @param handler the handler to invoke
86          */

87         public ReadyTask(final ReadReadyHandler handler) {
88             this.handler = handler;
89         }
90
91         public void run() {
92             handler.readReady();
93         }
94     }
95 }
96