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.client.io;
20  
21  import org.eclipse.jetty.util.FuturePromise;
22  import org.eclipse.jetty.websocket.api.Session;
23  import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
24  import org.eclipse.jetty.websocket.client.ClientUpgradeResponse;
25  import org.eclipse.jetty.websocket.client.WebSocketClient;
26  import org.eclipse.jetty.websocket.client.masks.Masker;
27  import org.eclipse.jetty.websocket.common.WebSocketSession;
28  import org.eclipse.jetty.websocket.common.events.EventDriver;
29  
30  /**
31   * Holder for the pending connect information.
32   */
33  public abstract class ConnectPromise extends FuturePromise<Session> implements Runnable
34  {
35      private final WebSocketClient client;
36      private final EventDriver driver;
37      private final ClientUpgradeRequest request;
38      private final Masker masker;
39      private UpgradeListener upgradeListener;
40      private ClientUpgradeResponse response;
41  
42      public ConnectPromise(WebSocketClient client, EventDriver driver, ClientUpgradeRequest request)
43      {
44          this.client = client;
45          this.driver = driver;
46          this.request = request;
47          this.masker = client.getMasker();
48      }
49  
50      @Override
51      public void failed(Throwable cause)
52      {
53          // Notify websocket of failure to connect
54          driver.onError(cause);
55  
56          // Notify promise/future of failure to connect
57          super.failed(cause);
58      }
59  
60      public WebSocketClient getClient()
61      {
62          return client;
63      }
64  
65      public EventDriver getDriver()
66      {
67          return this.driver;
68      }
69  
70      public Masker getMasker()
71      {
72          return masker;
73      }
74  
75      public ClientUpgradeRequest getRequest()
76      {
77          return this.request;
78      }
79  
80      public ClientUpgradeResponse getResponse()
81      {
82          return response;
83      }
84  
85      public UpgradeListener getUpgradeListener()
86      {
87          return upgradeListener;
88      }
89  
90      public void setResponse(ClientUpgradeResponse response)
91      {
92          this.response = response;
93      }
94  
95      public void setUpgradeListener(UpgradeListener upgradeListener)
96      {
97          this.upgradeListener = upgradeListener;
98      }
99  
100     public void succeeded(WebSocketSession session)
101     {
102         session.setUpgradeRequest(request);
103         session.setUpgradeResponse(response);
104         session.open();
105         super.succeeded(session);
106     }
107 }