View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.http2.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.http2.ErrorCode;
29  import org.eclipse.jetty.http2.api.Session;
30  import org.eclipse.jetty.util.Callback;
31  import org.eclipse.jetty.util.ConcurrentHashSet;
32  
33  public class HttpConnectionOverHTTP2 extends HttpConnection
34  {
35      private final Set<HttpChannel> channels = new ConcurrentHashSet<>();
36      private final Session session;
37  
38      public HttpConnectionOverHTTP2(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 HttpChannelOverHTTP2(getHttpDestination(), this, session);
50          channels.add(channel);
51          if (channel.associate(exchange))
52              channel.send();
53          else
54              channel.release();
55      }
56  
57      protected void release(HttpChannel channel)
58      {
59          channels.remove(channel);
60      }
61  
62      @Override
63      public void close()
64      {
65          // First close then abort, to be sure that the connection cannot be reused
66          // from an onFailure() handler or by blocking code waiting for completion.
67          getHttpDestination().close(this);
68          session.close(ErrorCode.NO_ERROR.code, null, Callback.NOOP);
69          abort(new AsynchronousCloseException());
70      }
71  
72      private void abort(Throwable failure)
73      {
74          for (HttpChannel channel : channels)
75          {
76              HttpExchange exchange = channel.getHttpExchange();
77              if (exchange != null)
78                  exchange.getRequest().abort(failure);
79          }
80          channels.clear();
81      }
82  }