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.spdy.client.http;
20  
21  import java.nio.channels.AsynchronousCloseException;
22  import java.util.Set;
23  
24  import org.eclipse.jetty.client.HttpChannel;
25  import org.eclipse.jetty.client.HttpConnection;
26  import org.eclipse.jetty.client.HttpDestination;
27  import org.eclipse.jetty.client.HttpExchange;
28  import org.eclipse.jetty.spdy.api.GoAwayInfo;
29  import org.eclipse.jetty.spdy.api.Session;
30  import org.eclipse.jetty.util.Callback;
31  import org.eclipse.jetty.util.ConcurrentHashSet;
32  
33  public class HttpConnectionOverSPDY extends HttpConnection
34  {
35      private final Set<HttpChannel> channels = new ConcurrentHashSet<>();
36      private final Session session;
37  
38      public HttpConnectionOverSPDY(HttpDestination destination, Session session)
39      {
40          super(destination);
41          this.session = session;
42      }
43  
44      @Override
45      protected void send(HttpExchange exchange)
46      {
47          normalizeRequest(exchange.getRequest());
48          // One connection maps to N channels, so for each exchange we create a new channel
49          HttpChannel channel = new HttpChannelOverSPDY(getHttpDestination(), this, session);
50          channels.add(channel);
51          channel.associate(exchange);
52          channel.send();
53      }
54  
55      protected void release(HttpChannel channel)
56      {
57          channels.remove(channel);
58      }
59  
60      @Override
61      public void close()
62      {
63          // First close then abort, to be sure that the connection cannot be reused
64          // from an onFailure() handler or by blocking code waiting for completion.
65          getHttpDestination().close(this);
66          session.goAway(new GoAwayInfo(), Callback.Adapter.INSTANCE);
67          abort(new AsynchronousCloseException());
68      }
69  
70      private void abort(Throwable failure)
71      {
72          for (HttpChannel channel : channels)
73          {
74              HttpExchange exchange = channel.getHttpExchange();
75              if (exchange != null)
76                  exchange.getRequest().abort(failure);
77          }
78          channels.clear();
79      }
80  }