Class ServerEngine<Q extends MessageHeader,R extends MessageHeader>

java.lang.Object
org.jdrupes.httpcodec.Engine
org.jdrupes.httpcodec.ServerEngine<Q,R>
Type Parameters:
Q - the message header type handled by the decoder (the request)
R - the message header type handled be the encoder (the response)

public class ServerEngine<Q extends MessageHeader,R extends MessageHeader> extends Engine
An engine that can be used as a server.

It has an associated request decoder and response encoder. Using a ServerEngine has two main advantages over using an encoder and decoder directly. It links encoder and decoder (see Encoder.setPeerDecoder(Decoder) and Decoder.setPeerEncoder(Encoder)) and it replaces the encoder and decoder if the encoded result indicates a switch.

  • Constructor Details

    • ServerEngine

      public ServerEngine(Decoder<Q,R> requestDecoder, Encoder<R,Q> responseEncoder)
      Creates a new instance.
      Parameters:
      requestDecoder - the decoder for the request
      responseEncoder - the encoder for the response
  • Method Details

    • requestDecoder

      Returns the request decoder.
      Returns:
      the request decoder
    • responseEncoder

      Returns the response encoder.
      Returns:
      the response encoder
    • decoding

      public Class<? extends MessageHeader> decoding()
      Returns the type of the messages decoded by this server.
      Returns:
      the value
    • encoding

      public Class<? extends MessageHeader> encoding()
      Returns the type of the messages encoded by this server.
      Returns:
      the value
    • decode

      public Decoder.Result<R> decode(ByteBuffer in, Buffer out, boolean endOfInput) throws ProtocolException
      Decodes a request sent to the server.
      Parameters:
      in - the data to decode
      out - the decoded data
      endOfInput - true if this invocation finishes the message
      Returns:
      the result
      Throws:
      ProtocolException - if the input violates the protocol
    • encode

      public void encode(R messageHeader)
      Encodes a response generated by the server.

      This method must be used instead of the encoder’s method if the encoder and decoder should adapt to a protocol switch automatically.

      Parameters:
      messageHeader - the message header
      See Also:
    • encode

      Invokes the encoder’s encode method.

      This method must be used instead of encoder’s method if the encoder and decoder should adapt to a protocol switch automatically.

      Parameters:
      out - the decoded data
      Returns:
      the result
      See Also:
    • encode

      public Codec.Result encode(Buffer in, ByteBuffer out, boolean endOfInput)
      Invokes the encoder’s encode method.

      This method must be used instead of decoding the encoder’s method directly to allow derived server classes to adapt to any information contained in the message.

      Parameters:
      in - the data to encode
      out - the encoded data
      endOfInput - true if this invocation finishes the message
      Returns:
      the result
      See Also:
    • currentRequest

      Returns the last fully decoded request if it exists.
      Returns:
      the request