Class MyraServer
java.lang.Object
express.mvp.myra.server.MyraServer
- All Implemented Interfaces:
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:
| Bits | Name | Description |
|---|---|---|
| 48-63 | OP | Operation type (1=READ, 2=WRITE) |
| 16-47 | CONN_ID | Connection identifier |
| 0-15 | REQ_ID | Request 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 Summary
ConstructorsConstructorDescriptionMyraServer(MyraServerConfig config, MyraServerHandler handler) Creates a new server with the specified configuration and handler. -
Method Summary
Modifier and TypeMethodDescriptionbooleanawaitReady(long timeout, TimeUnit unit) Waits for the server to be ready to accept connections.voidclose()voidstart()Starts the server.voidstop()Stops the server gracefully.
-
Constructor Details
-
MyraServer
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:
- Binds to the configured host:port
- Accepts incoming connections
- Polls for I/O events on all connections
- Dispatches events to the handler
This method returns immediately after starting the thread.
-
awaitReady
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 waitunit- 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:
closein interfaceAutoCloseable
-