Class KernelAdapter

java.lang.Object
java.lang.Thread
com.jme3.network.base.KernelAdapter
All Implemented Interfaces:
Runnable

public class KernelAdapter extends Thread
Wraps a single Kernel and forwards new messages to the supplied message dispatcher and new endpoint events to the connection dispatcher. This is used by DefaultServer to manage its kernel objects.

This adapter assumes a simple protocol where two bytes define a (short) object size with the object data to follow. Note: this limits the size of serialized objects to 32676 bytes... even though, for example, datagram packets can hold twice that. :P

Author:
Paul Speed
  • Constructor Details

    • KernelAdapter

      public KernelAdapter(DefaultServer server, Kernel kernel, MessageProtocol protocol, MessageListener<HostedConnection> messageDispatcher, boolean reliable)
      Creates an adapter for a kernel used by DefaultServer.
      Parameters:
      server - the owning server
      kernel - the kernel to wrap
      protocol - the message protocol used to decode envelopes
      messageDispatcher - the listener that receives decoded messages
      reliable - true if this adapter represents a reliable channel
  • Method Details

    • getKernel

      public Kernel getKernel()
      Returns the wrapped kernel.
      Returns:
      the wrapped kernel
    • initialize

      public void initialize()
      Initializes the wrapped kernel.
    • broadcast

      public void broadcast(Filter<? super Endpoint> filter, ByteBuffer data, boolean reliable, boolean copy)
      Broadcasts raw bytes through the wrapped kernel.
      Parameters:
      filter - the endpoint filter, or null to target all endpoints
      data - the bytes to broadcast
      reliable - true to request reliable transport
      copy - true to copy the buffer before dispatch
    • close

      public void close() throws InterruptedException
      Closes the adapter and terminates the wrapped kernel.
      Throws:
      InterruptedException - if interrupted while waiting for shutdown
    • reportError

      protected void reportError(Endpoint p, Object context, Exception e)
      Reports an adapter or kernel error and closes the endpoint when possible.
      Parameters:
      p - the endpoint involved in the failure
      context - additional context for the failure
      e - the error that occurred
    • getConnection

      protected HostedConnection getConnection(Endpoint p)
      Resolves a hosted connection from an endpoint.
      Parameters:
      p - the endpoint to resolve
      Returns:
      the hosted connection, or null if none exists
    • connectionClosed

      protected void connectionClosed(Endpoint p)
      Handles endpoint closure and clears any buffered message state.
      Parameters:
      p - the closed endpoint
    • dispatch

      protected void dispatch(Endpoint p, Message m)
      Note on threading for those writing their own server or adapter implementations. The rule that a single connection be processed by only one thread at a time is more about ensuring that the messages are delivered in the order that they are received than for any user-code safety. 99% of the time the user code should be writing for multithreaded access anyway.

      The issue with the messages is that if an implementation is using a general thread pool then it would be possible for a naive implementation to have one thread grab an Envelope from connection 1's and another grab the next Envelope. Since an Envelope may contain several messages, delivering the second thread's messages before or during the first's would be really confusing and hard to code for in user code.

      And that's why this note is here. DefaultServer does a rudimentary per-connection locking but it couldn't possibly guard against out of order Envelope processing.

      Parameters:
      p - the endpoint that sent the message
      m - the decoded message
    • getMessageBuffer

      protected MessageBuffer getMessageBuffer(Endpoint p)
      Returns the message buffer used to accumulate bytes for an endpoint.
      Parameters:
      p - the endpoint supplying bytes
      Returns:
      the message buffer for that endpoint
    • createAndDispatch

      protected void createAndDispatch(Envelope env)
      Decodes an envelope into messages and dispatches them.
      Parameters:
      env - the received envelope
    • createAndDispatch

      protected void createAndDispatch(EndpointEvent event)
      Handles a kernel endpoint event.
      Parameters:
      event - the endpoint event
    • flushEvents

      protected void flushEvents()
      Drains and processes all currently queued kernel events.
    • run

      public void run()
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread