Class MyraServer

java.lang.Object
express.mvp.myra.server.MyraServer
All Implemented Interfaces:
AutoCloseable

public class MyraServer extends Object implements AutoCloseable
High-level server API for Myra Transport.

This class provides a simple server abstraction over the low-level transport, managing the accept loop, connection tracking, and I/O polling for multiple connected clients.

Architecture

┌─────────────────────────────────────────────────────────────┐
│                       MyraServer                            │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   ┌─────────────┐    ┌─────────────────────────────────┐   │
│   │   Accept    │    │         Connection Pool         │   │
│   │   Socket    │    │  ┌───────┐ ┌───────┐ ┌───────┐  │   │
│   └──────┬──────┘    │  │ Conn1 │ │ Conn2 │ │ Conn3 │  │   │
│          │           │  └───────┘ └───────┘ └───────┘  │   │
│          ▼           └─────────────────────────────────┘   │
│   ┌─────────────────────────────────────────────────────┐  │
│   │              Single I/O Thread                      │  │
│   │  • Polls server socket for accepts                  │  │
│   │  • Polls all client connections                     │  │
│   │  • Submits batched operations                       │  │
│   └─────────────────────────────────────────────────────┘  │
│                           │                                │
│                           ▼                                │
│   ┌─────────────────────────────────────────────────────┐  │
│   │           MyraServerHandler (callbacks)             │  │
│   │  • onConnect()                                      │  │
│   │  • onDataReceived()                                 │  │
│   │  • onDisconnect()                                   │  │
│   └─────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘

Token Encoding

I/O operations use 64-bit tokens with the following bit layout:

Token Bit Layout
BitsNameDescription
48-63OPOperation type (1=READ, 2=WRITE)
16-47CONN_IDConnection identifier
0-15REQ_IDRequest sequence number

Usage Example

MyraServerConfig config = new MyraServerConfig()
    .setHost("0.0.0.0")
    .setPort(8080)
    .setNumBuffers(256)
    .setBufferSize(65536);

MyraServerHandler handler = new MyraServerHandler() {
    @Override
    public void onDataReceived(TransportBackend conn,
            RegisteredBuffer buffer, int bytesRead) {
        // Echo back
        buffer.limit(bytesRead);
        conn.send(buffer, 0);
    }
};

try (MyraServer server = new MyraServer(config, handler)) {
    server.start();
    // Server runs until stopped
    Thread.sleep(Long.MAX_VALUE);
}

Thread Model

  • Single I/O thread handles all operations (accept, read, write)
  • CPU affinity can be configured for reduced latency
  • Handlers are invoked on the I/O thread (keep callbacks fast)
See Also:
  • Constructor Details

    • MyraServer

      public MyraServer(MyraServerConfig config, MyraServerHandler handler)
      Creates a new server with the specified configuration and handler.
      Parameters:
      config - server configuration (host, port, buffer settings)
      handler - event handler for connection and data events
  • Method Details

    • start

      public void start()
      Starts the server.

      This method spawns a background I/O thread that:

      1. Binds to the configured host:port
      2. Accepts incoming connections
      3. Polls for I/O events on all connections
      4. Dispatches events to the handler

      This method returns immediately after starting the thread.

    • awaitReady

      public boolean awaitReady(long timeout, TimeUnit unit) throws InterruptedException
      Waits for the server to be ready to accept connections.

      This method blocks until the server has completed initialization and is ready to accept incoming connections, or until the timeout expires.

      Parameters:
      timeout - the maximum time to wait
      unit - the time unit of the timeout argument
      Returns:
      true if the server is ready, false if the timeout elapsed
      Throws:
      InterruptedException - if interrupted while waiting
    • stop

      public void stop()
      Stops the server gracefully.

      Signals the I/O thread to stop and waits up to 5 seconds for it to terminate. Then closes all resources.

    • close

      public void close()
      Specified by:
      close in interface AutoCloseable