View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.mux;
20  
21  import java.net.InetSocketAddress;
22  import java.util.concurrent.Executor;
23  import java.util.concurrent.atomic.AtomicBoolean;
24  
25  import org.eclipse.jetty.io.ByteBufferPool;
26  import org.eclipse.jetty.websocket.api.StatusCode;
27  import org.eclipse.jetty.websocket.api.SuspendToken;
28  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
29  import org.eclipse.jetty.websocket.api.WriteCallback;
30  import org.eclipse.jetty.websocket.api.extensions.Frame;
31  import org.eclipse.jetty.websocket.api.extensions.IncomingFrames;
32  import org.eclipse.jetty.websocket.common.CloseInfo;
33  import org.eclipse.jetty.websocket.common.ConnectionState;
34  import org.eclipse.jetty.websocket.common.LogicalConnection;
35  import org.eclipse.jetty.websocket.common.WebSocketSession;
36  import org.eclipse.jetty.websocket.common.io.IOState;
37  import org.eclipse.jetty.websocket.common.io.IOState.ConnectionStateListener;
38  
39  /**
40   * MuxChannel, acts as WebSocketConnection for specific sub-channel.
41   */
42  public class MuxChannel implements LogicalConnection, IncomingFrames, SuspendToken, ConnectionStateListener
43  {
44      private final long channelId;
45      private final Muxer muxer;
46      private final AtomicBoolean suspendToken;
47      private IOState ioState;
48      private WebSocketPolicy policy;
49      private WebSocketSession session;
50      private IncomingFrames incoming;
51  
52      public MuxChannel(long channelId, Muxer muxer)
53      {
54          this.channelId = channelId;
55          this.muxer = muxer;
56          this.policy = muxer.getPolicy().clonePolicy();
57  
58          this.suspendToken = new AtomicBoolean(false);
59          this.ioState = new IOState();
60          this.ioState.addListener(this);
61      }
62      
63      @Override
64      public Executor getExecutor()
65      {
66          // TODO Auto-generated method stub
67          return null;
68      }
69  
70      @Override
71      public void close()
72      {
73          close(StatusCode.NORMAL,null);
74      }
75  
76      @Override
77      public void close(int statusCode, String reason)
78      {
79          CloseInfo close = new CloseInfo(statusCode,reason);
80          // TODO: disconnect callback?
81          outgoingFrame(close.asFrame(),null);
82      }
83  
84      @Override
85      public void disconnect()
86      {
87          // TODO: disconnect the virtual end-point?
88      }
89  
90      @Override
91      public ByteBufferPool getBufferPool()
92      {
93          // TODO Auto-generated method stub
94          return null;
95      }
96  
97      public long getChannelId()
98      {
99          return channelId;
100     }
101 
102     @Override
103     public long getIdleTimeout()
104     {
105         // TODO Auto-generated method stub
106         return 0;
107     }
108 
109     @Override
110     public IOState getIOState()
111     {
112         // TODO Auto-generated method stub
113         return null;
114     }
115 
116     @Override
117     public InetSocketAddress getLocalAddress()
118     {
119         // TODO Auto-generated method stub
120         return null;
121     }
122 
123     @Override
124     public long getMaxIdleTimeout()
125     {
126         // TODO Auto-generated method stub
127         return 0;
128     }
129 
130     @Override
131     public WebSocketPolicy getPolicy()
132     {
133         return policy;
134     }
135 
136     @Override
137     public InetSocketAddress getRemoteAddress()
138     {
139         return muxer.getRemoteAddress();
140     }
141 
142     @Override
143     public WebSocketSession getSession()
144     {
145         return session;
146     }
147 
148     /**
149      * Incoming exceptions from Muxer.
150      */
151     @Override
152     public void incomingError(Throwable e)
153     {
154         incoming.incomingError(e);
155     }
156 
157     /**
158      * Incoming frames from Muxer
159      */
160     @Override
161     public void incomingFrame(Frame frame)
162     {
163         incoming.incomingFrame(frame);
164     }
165 
166     public boolean isActive()
167     {
168         return (ioState.isOpen());
169     }
170 
171     @Override
172     public boolean isOpen()
173     {
174         return isActive() && muxer.isOpen();
175     }
176 
177     @Override
178     public boolean isReading()
179     {
180         return true;
181     }
182 
183     public void onClose()
184     {
185     }
186 
187     @Override
188     public void onConnectionStateChange(ConnectionState state)
189     {
190         // TODO Auto-generated method stub
191 
192     }
193 
194     public void onOpen()
195     {
196         this.ioState.onOpened();
197     }
198 
199     /**
200      * Frames destined for the Muxer
201      */
202     @Override
203     public void outgoingFrame(Frame frame, WriteCallback callback)
204     {
205         muxer.output(channelId,frame,callback);
206     }
207 
208     @Override
209     public void resume()
210     {
211         if (suspendToken.getAndSet(false))
212         {
213             // TODO: Start reading again. (how?)
214         }
215     }
216 
217     @Override
218     public void setMaxIdleTimeout(long ms)
219     {
220         // TODO Auto-generated method stub
221 
222     }
223 
224     @Override
225     public void setNextIncomingFrames(IncomingFrames incoming)
226     {
227         this.incoming = incoming;
228     }
229 
230     @Override
231     public void setSession(WebSocketSession session)
232     {
233         this.session = session;
234         // session.setOutgoing(this);
235     }
236 
237     public void setSubProtocol(String subProtocol)
238     {
239     }
240 
241     @Override
242     public SuspendToken suspend()
243     {
244         suspendToken.set(true);
245         // TODO: how to suspend reading?
246         return this;
247     }
248 }