View Javadoc

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