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.http2.client;
20  
21  import org.eclipse.jetty.http2.FlowControlStrategy;
22  import org.eclipse.jetty.http2.HTTP2Session;
23  import org.eclipse.jetty.http2.IStream;
24  import org.eclipse.jetty.http2.api.Session;
25  import org.eclipse.jetty.http2.api.Stream;
26  import org.eclipse.jetty.http2.frames.HeadersFrame;
27  import org.eclipse.jetty.http2.frames.PushPromiseFrame;
28  import org.eclipse.jetty.http2.generator.Generator;
29  import org.eclipse.jetty.io.EndPoint;
30  import org.eclipse.jetty.util.Callback;
31  import org.eclipse.jetty.util.log.Log;
32  import org.eclipse.jetty.util.log.Logger;
33  import org.eclipse.jetty.util.thread.Scheduler;
34  
35  public class HTTP2ClientSession extends HTTP2Session
36  {
37      private static final Logger LOG = Log.getLogger(HTTP2ClientSession.class);
38  
39      public HTTP2ClientSession(Scheduler scheduler, EndPoint endPoint, Generator generator, Session.Listener listener, FlowControlStrategy flowControl)
40      {
41          super(scheduler, endPoint, generator, listener, flowControl, 1);
42      }
43  
44      @Override
45      public void onHeaders(HeadersFrame frame)
46      {
47          if (LOG.isDebugEnabled())
48              LOG.debug("Received {}", frame);
49  
50          int streamId = frame.getStreamId();
51          IStream stream = getStream(streamId);
52          if (stream == null)
53          {
54              if (LOG.isDebugEnabled())
55                  LOG.debug("Ignoring {}, stream #{} not found", frame, streamId);
56          }
57          else
58          {
59              stream.process(frame, Callback.NOOP);
60              notifyHeaders(stream, frame);
61          }
62      }
63  
64      private void notifyHeaders(IStream stream, HeadersFrame frame)
65      {
66          Stream.Listener listener = stream.getListener();
67          if (listener == null)
68              return;
69          try
70          {
71              listener.onHeaders(stream, frame);
72          }
73          catch (Throwable x)
74          {
75              LOG.info("Failure while notifying listener " + listener, x);
76          }
77      }
78  
79      @Override
80      public void onPushPromise(PushPromiseFrame frame)
81      {
82          if (LOG.isDebugEnabled())
83              LOG.debug("Received {}", frame);
84  
85          int streamId = frame.getStreamId();
86          int pushStreamId = frame.getPromisedStreamId();
87          IStream stream = getStream(streamId);
88          if (stream == null)
89          {
90              if (LOG.isDebugEnabled())
91                  LOG.debug("Ignoring {}, stream #{} not found", frame, streamId);
92          }
93          else
94          {
95              IStream pushStream = createRemoteStream(pushStreamId);
96              pushStream.process(frame, Callback.NOOP);
97              Stream.Listener listener = notifyPush(stream, pushStream, frame);
98              pushStream.setListener(listener);
99          }
100     }
101 
102     private Stream.Listener notifyPush(IStream stream, IStream pushStream, PushPromiseFrame frame)
103     {
104         Stream.Listener listener = stream.getListener();
105         if (listener == null)
106             return null;
107         try
108         {
109             return listener.onPush(pushStream, frame);
110         }
111         catch (Throwable x)
112         {
113             LOG.info("Failure while notifying listener " + listener, x);
114             return null;
115         }
116     }
117 }