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.io.IOException;
22  import java.net.InetSocketAddress;
23  import java.util.concurrent.Executor;
24  import java.util.concurrent.Future;
25  
26  import javax.net.websocket.SendResult;
27  
28  import org.eclipse.jetty.io.EndPoint;
29  import org.eclipse.jetty.websocket.api.extensions.Frame;
30  import org.eclipse.jetty.websocket.api.extensions.IncomingFrames;
31  import org.eclipse.jetty.websocket.client.WebSocketClientFactory;
32  import org.eclipse.jetty.websocket.client.internal.DefaultWebSocketClient;
33  import org.eclipse.jetty.websocket.client.masks.Masker;
34  import org.eclipse.jetty.websocket.common.WebSocketFrame;
35  import org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection;
36  
37  /**
38   * Client side WebSocket physical connection.
39   */
40  public class WebSocketClientConnection extends AbstractWebSocketConnection
41  {
42      private final WebSocketClientFactory factory;
43      private final DefaultWebSocketClient client;
44      private final Masker masker;
45      private boolean connected;
46  
47      public WebSocketClientConnection(EndPoint endp, Executor executor, DefaultWebSocketClient client)
48      {
49          super(endp,executor,client.getFactory().getScheduler(),client.getPolicy(),client.getFactory().getBufferPool());
50          this.client = client;
51          this.factory = client.getFactory();
52          this.connected = false;
53          this.masker = client.getMasker();
54      }
55  
56      public DefaultWebSocketClient getClient()
57      {
58          return client;
59      }
60  
61      @Override
62      public InetSocketAddress getLocalAddress()
63      {
64          return getEndPoint().getLocalAddress();
65      }
66  
67      @Override
68      public InetSocketAddress getRemoteAddress()
69      {
70          return getEndPoint().getRemoteAddress();
71      }
72  
73      @Override
74      public void onClose()
75      {
76          super.onClose();
77          factory.sessionClosed(getSession());
78      }
79  
80      @Override
81      public void onOpen()
82      {
83          if (!connected)
84          {
85              factory.sessionOpened(getSession());
86              connected = true;
87          }
88          super.onOpen();
89      };
90  
91      @Override
92      public Future<SendResult> outgoingFrame(Frame frame) throws IOException
93      {
94          if (frame instanceof WebSocketFrame)
95          {
96              masker.setMask((WebSocketFrame)frame);
97          }
98          return super.outgoingFrame(frame);
99      }
100 
101     @Override
102     public void setNextIncomingFrames(IncomingFrames incoming)
103     {
104         getParser().setIncomingFramesHandler(incoming);
105     }
106 }