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;
20
21 import org.xnio.channels.CloseListenerSettable;
22 import org.xnio.conduits.ConduitStreamSinkChannel;
23 import org.xnio.conduits.ConduitStreamSourceChannel;
24 import org.xnio.conduits.StreamSinkConduit;
25 import org.xnio.conduits.StreamSourceConduit;
26
27 import static org.xnio._private.Messages.msg;
28
29 /**
30  * A connection between peers.
31  *
32  * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
33  */

34 public abstract class StreamConnection extends Connection implements CloseListenerSettable<StreamConnection> {
35
36     private ConduitStreamSourceChannel sourceChannel;
37     private ConduitStreamSinkChannel sinkChannel;
38     private ChannelListener<? super StreamConnection> closeListener;
39
40     /**
41      * Construct a new instance.
42      *
43      * @param thread the I/O thread
44      */

45     protected StreamConnection(final XnioIoThread thread) {
46         super(thread);
47     }
48
49     public void setCloseListener(final ChannelListener<? super StreamConnection> listener) {
50         this.closeListener = listener;
51     }
52
53     public ChannelListener<? super StreamConnection> getCloseListener() {
54         return closeListener;
55     }
56
57     public ChannelListener.Setter<? extends StreamConnection> getCloseSetter() {
58         return new Setter<StreamConnection>(this);
59     }
60
61     /**
62      * Set the source conduit for this channel.  The source channel will automatically be updated.
63      *
64      * @param conduit the source conduit for this channel
65      */

66     protected void setSourceConduit(StreamSourceConduit conduit) {
67         this.sourceChannel = conduit == null ? null : new ConduitStreamSourceChannel(this, conduit);
68     }
69
70     /**
71      * Set the sink conduit for this channel.  The sink channel will automatically be updated.
72      *
73      * @param conduit the sink conduit for this channel
74      */

75     protected void setSinkConduit(StreamSinkConduit conduit) {
76         this.sinkChannel = conduit == null ? null : new ConduitStreamSinkChannel(this, conduit);
77     }
78
79     void invokeCloseListener() {
80         ChannelListeners.invokeChannelListener(this, closeListener);
81     }
82
83     private static <T> T notNull(T orig) throws IllegalStateException {
84         if (orig == null) {
85             throw msg.channelNotAvailable();
86         }
87         return orig;
88     }
89
90     /**
91      * Get the source channel.
92      *
93      * @return the source channel
94      */

95     public ConduitStreamSourceChannel getSourceChannel() {
96         return notNull(sourceChannel);
97     }
98
99     /**
100      * Get the sink channel.
101      *
102      * @return the sink channel
103      */

104     public ConduitStreamSinkChannel getSinkChannel() {
105         return notNull(sinkChannel);
106     }
107 }
108