All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.codec.http2.stream.Session Maven / Gradle / Ivy

There is a newer version: 4.0.3.2
Show newest version
package com.firefly.codec.http2.stream;

import java.util.Collection;
import java.util.Map;

import com.firefly.codec.http2.frame.DataFrame;
import com.firefly.codec.http2.frame.GoAwayFrame;
import com.firefly.codec.http2.frame.HeadersFrame;
import com.firefly.codec.http2.frame.PingFrame;
import com.firefly.codec.http2.frame.PriorityFrame;
import com.firefly.codec.http2.frame.ResetFrame;
import com.firefly.codec.http2.frame.SettingsFrame;
import com.firefly.utils.concurrent.Callback;
import com.firefly.utils.concurrent.Promise;

/**
 * 

* A {@link Session} represents the client-side endpoint of a HTTP/2 connection * to a single origin server. *

*

* Once a {@link Session} has been obtained, it can be used to open HTTP/2 * streams: *

* *
 * Session session = ...;
 * HeadersFrame frame = ...;
 * Promise<Stream> promise = ...
 * session.newStream(frame, promise, new Stream.Listener.Adapter()
 * {
 *     public void onHeaders(Stream stream, HeadersFrame frame)
 *     {
 *         // Reply received
 *     }
 * });
 * 
*

* A {@link Session} is the active part of the endpoint, and by calling its API * applications can generate events on the connection; conversely * {@link Session.Listener} is the passive part of the endpoint, and has * callbacks that are invoked when events happen on the connection. *

* * @see Session.Listener */ public interface Session { /** *

* Sends the given HEADERS {@code frame} to create a new {@link Stream}. *

* * @param frame * the HEADERS frame containing the HTTP headers * @param promise * the promise that gets notified of the stream creation * @param listener * the listener that gets notified of stream events */ public void newStream(HeadersFrame frame, Promise promise, Stream.Listener listener); /** *

* Sends the given PRIORITY {@code frame}. *

*

* If the {@code frame} references a {@code streamId} that does not exist * (for example {@code 0}), then a new {@code streamId} will be allocated, * to support unused anchor streams that act as parent for other * streams. *

* * @param frame * the PRIORITY frame to send * @param callback * the callback that gets notified when the frame has been sent * @return the new stream id generated by the PRIORITY frame, or the stream * id that it is already referencing */ public int priority(PriorityFrame frame, Callback callback); /** *

* Sends the given SETTINGS {@code frame} to configure the session. *

* * @param frame * the SETTINGS frame to send * @param callback * the callback that gets notified when the frame has been sent */ public void settings(SettingsFrame frame, Callback callback); /** *

* Sends the given PING {@code frame}. *

*

* PING frames may be used to test the connection integrity and to measure * round-trip time. *

* * @param frame * the PING frame to send * @param callback * the callback that gets notified when the frame has been sent */ public void ping(PingFrame frame, Callback callback); /** *

* Closes the session by sending a GOAWAY frame with the given error code * and payload. *

*

* The GOAWAY frame is sent only once; subsequent or concurrent attempts to * close the session will have no effect. *

* * @param error * the error code * @param payload * an optional payload (may be null) * @param callback * the callback that gets notified when the frame has been sent * @return true if the frame is being sent, false if the session was already * closed */ public boolean close(int error, String payload, Callback callback); /** * @return whether the session is not open */ public boolean isClosed(); /** * @return a snapshot of all the streams currently belonging to this session */ public Collection getStreams(); /** *

* Retrieves the stream with the given {@code streamId}. *

* * @param streamId * the stream id of the stream looked for * @return the stream with the given id, or null if no such stream exist */ public Stream getStream(int streamId); /** *

* A {@link Listener} is the passive counterpart of a {@link Session} and * receives events happening on a HTTP/2 connection. *

* * @see Session */ public interface Listener { /** *

* Callback method invoked when the preface has been received. *

* * @param session * the session * @return a (possibly empty or null) map containing SETTINGS * configuration options that are sent after the preface. */ public Map onPreface(Session session); /** *

* Callback method invoked when a new stream is being created upon * receiving a HEADERS frame representing a HTTP request. *

*

* Applications should implement this method to process HTTP requests, * typically providing a HTTP response via * {@link Stream#headers(HeadersFrame, Callback)}. *

*

* Applications can detect whether request DATA frames will be arriving * by testing {@link HeadersFrame#isEndStream()}. If the application is * interested in processing the DATA frames, it must return a * {@link Stream.Listener} implementation that overrides * {@link Stream.Listener#onData(Stream, DataFrame, Callback)}. *

* * @param stream * the newly created stream * @param frame * the HEADERS frame received * @return a {@link Stream.Listener} that will be notified of stream * events */ public Stream.Listener onNewStream(Stream stream, HeadersFrame frame); /** *

* Callback method invoked when a SETTINGS frame has been received. *

* * @param session * the session * @param frame * the SETTINGS frame received */ public void onSettings(Session session, SettingsFrame frame); /** *

* Callback method invoked when a PING frame has been received. *

* * @param session * the session * @param frame * the PING frame received */ public void onPing(Session session, PingFrame frame); /** *

* Callback method invoked when a RST_STREAM frame has been received for * an unknown stream. *

* * @param session * the session * @param frame * the RST_STREAM frame received * @see Stream.Listener#onReset(Stream, ResetFrame) */ public void onReset(Session session, ResetFrame frame); /** *

* Callback method invoked when a GOAWAY frame has been received. *

* * @param session * the session * @param frame * the GOAWAY frame received */ public void onClose(Session session, GoAwayFrame frame); /** *

* Callback method invoked when the idle timeout expired. *

* * @param session * the session * @return whether the session should be closed */ public boolean onIdleTimeout(Session session); /** *

* Callback method invoked when a failure has been detected for this * session. *

* * @param session * the session * @param failure * the failure */ public void onFailure(Session session, Throwable failure); /** *

* Empty implementation of {@link Stream.Listener}. *

*/ public static class Adapter implements Session.Listener { @Override public Map onPreface(Session session) { return null; } @Override public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) { return null; } @Override public void onSettings(Session session, SettingsFrame frame) { } @Override public void onPing(Session session, PingFrame frame) { } @Override public void onReset(Session session, ResetFrame frame) { } @Override public void onClose(Session session, GoAwayFrame frame) { } @Override public boolean onIdleTimeout(Session session) { return true; } @Override public void onFailure(Session session, Throwable failure) { } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy