View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.server;
20  
21  import java.net.InetSocketAddress;
22  import java.nio.ByteBuffer;
23  import java.util.concurrent.Executor;
24  import java.util.concurrent.atomic.AtomicBoolean;
25  
26  import org.eclipse.jetty.io.ByteBufferPool;
27  import org.eclipse.jetty.io.Connection;
28  import org.eclipse.jetty.io.EndPoint;
29  import org.eclipse.jetty.util.BufferUtil;
30  import org.eclipse.jetty.util.thread.Scheduler;
31  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
32  import org.eclipse.jetty.websocket.api.extensions.IncomingFrames;
33  import org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection;
34  
35  public class WebSocketServerConnection extends AbstractWebSocketConnection implements Connection.UpgradeTo
36  {
37      private final AtomicBoolean opened = new AtomicBoolean(false);
38  
39      public WebSocketServerConnection(EndPoint endp, Executor executor, Scheduler scheduler, WebSocketPolicy policy, ByteBufferPool bufferPool)
40      {
41          super(endp,executor,scheduler,policy,bufferPool);
42          if (policy.getIdleTimeout() > 0)
43          {
44              endp.setIdleTimeout(policy.getIdleTimeout());
45          }
46      }
47  
48      @Override
49      public InetSocketAddress getLocalAddress()
50      {
51          return getEndPoint().getLocalAddress();
52      }
53  
54      @Override
55      public InetSocketAddress getRemoteAddress()
56      {
57          return getEndPoint().getRemoteAddress();
58      }
59  
60      @Override
61      public void onUpgradeTo(ByteBuffer prefilled)
62      {
63          prefill(prefilled);
64      }
65  
66      @Override
67      public void onOpen()
68      {
69          boolean beenOpened = opened.getAndSet(true);
70          if (!beenOpened)
71          {
72              getSession().open();
73          }
74          super.onOpen();
75      }
76      
77      @Override
78      public void setNextIncomingFrames(IncomingFrames incoming)
79      {
80          getParser().setIncomingFramesHandler(incoming);
81      }
82  }