View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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.client.internal.io;
20  
21  import java.util.concurrent.Executor;
22  
23  import org.eclipse.jetty.io.EndPoint;
24  import org.eclipse.jetty.util.Callback;
25  import org.eclipse.jetty.websocket.client.WebSocketClientFactory;
26  import org.eclipse.jetty.websocket.client.internal.DefaultWebSocketClient;
27  import org.eclipse.jetty.websocket.client.masks.Masker;
28  import org.eclipse.jetty.websocket.core.io.AbstractWebSocketConnection;
29  import org.eclipse.jetty.websocket.core.protocol.WebSocketFrame;
30  
31  /**
32   * Client side WebSocket physical connection.
33   */
34  public class WebSocketClientConnection extends AbstractWebSocketConnection
35  {
36      private final WebSocketClientFactory factory;
37      private final DefaultWebSocketClient client;
38      private final Masker masker;
39      private boolean connected;
40  
41      public WebSocketClientConnection(EndPoint endp, Executor executor, DefaultWebSocketClient client)
42      {
43          super(endp,executor,client.getFactory().getScheduler(),client.getPolicy(),client.getFactory().getBufferPool());
44          this.client = client;
45          this.factory = client.getFactory();
46          this.connected = false;
47          this.masker = client.getMasker();
48      }
49  
50      public DefaultWebSocketClient getClient()
51      {
52          return client;
53      }
54  
55      @Override
56      public void onClose()
57      {
58          super.onClose();
59          factory.sessionClosed(getSession());
60      };
61  
62      @Override
63      public void onOpen()
64      {
65          if (!connected)
66          {
67              factory.sessionOpened(getSession());
68              connected = true;
69          }
70          super.onOpen();
71      }
72  
73      @Override
74      public <C> void output(C context, Callback<C> callback, WebSocketFrame frame)
75      {
76          masker.setMask(frame);
77          super.output(context,callback,frame);
78      }
79  }