Interface MetaContext
-
- All Superinterfaces:
io.aether.utils.ToString
- All Known Implementing Classes:
AutoFlushContext,MetaContextBase,PooledAutoFlushContext
public interface MetaContext extends io.aether.utils.ToStringRuntime context shared by FastMeta local decoders and remote encoder views.The context owns outbound command buffering, lifecycle hooks, child contexts, transport state, and the currently active local API implementation. Remote API objects created from a context are typed encoder views over this same context rather than independent connections.
Local API replacement is command-boundary based. A requested replacement is deferred until the current decoded command has completed. The old local API remains active while its pending outbound bytes are flushed, and only then is the new local API installed. The META returned by the handoff is intended for unread bytes in that same input payload only; it must not be persisted as decoder state for later isolated transport payloads.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.IllegalStateExceptionCONTEXT_NOT_ACTIVEShared exception used when an operation is rejected because its transport context is inactive.static io.aether.utils.futures.AFutureCONTEXT_NOT_ACTIVE_AFUTUREstatic io.aether.utils.futures.ARFutureCONTEXT_NOT_ACTIVE_FUTUREPre-made future for inactive context errors — avoids allocation per rejected call.static MetaContextSTUB
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default FastMetaApi<?,?>applyLocalApiSwitch()Applies a previously requested local API switch at a command boundary.default io.aether.utils.futures.AFutureclose()Closes this context and releases resources owned by the implementation.default MetaContextconnectionContext()Returns the root context representing the underlying transport connection.MetaContextfindContext(io.aether.utils.interfaces.AFunction<MetaContext,?> factory, java.lang.Object... keys)Finds or creates a child context identified by the supplied composite key.default voidfireWritable(boolean writable)Publishes a writability-state change to listeners associated with this context.default voidflush()Requests immediate processing of the outbound flush lifecycle.java.lang.ObjectgetLocalApi()Returns the local API implementation currently installed in this context.default java.lang.ObjectgetProperty(int key)Returns transport- or implementation-specific metadata associated with this context.default voidinvokeLocalMethodAfter(java.lang.String methodName, io.aether.utils.futures.AFutureBase<?> result, java.lang.String[] argsNames, java.lang.Object[] argsValues)Hook invoked after a decoded local API method finishes its invocation step.default voidinvokeLocalMethodBefore(java.lang.String methodName, java.lang.String[] argsNames, java.lang.Object[] argsValues)Hook invoked immediately before a decoded local API method executes.default voidinvokeRemoteMethodAfter(java.lang.String methodName, io.aether.utils.futures.AFutureBase<?> result, java.lang.String[] argsNames, java.lang.Object[] argsValues)Hook invoked after a remote API method has been encoded.default booleanisActive()Reports whether this context is currently active.booleanisEmpty()Reports whether this context currently exposes flushable outbound data.booleanisLocked()Reports whether outbound flush/notification processing is currently suppressed bylock().java.lang.AutoCloseablelock()Temporarily suppresses outbound flush/notification processing.default <RT,RT2 extends RemoteApi>
RT2makeRemote(FastMetaApi<RT,RT2> meta)Creates a typed remote API encoder view backed by this context.default voidonFirstWritable(io.aether.utils.interfaces.ARunnable listener)Registers an action that runs once when writable state is first observed.voidonFlush(io.aether.utils.interfaces.ARunnable flushAction)Installs the action invoked by this context's flush lifecycle.default voidonFlushData(io.aether.utils.interfaces.AConsumer<byte[]> c)Registers a convenience flush handler that drains queued outbound data and passes each non-empty serialized payload to the supplied consumer.default voidonWritable(it.unimi.dsi.fastutil.booleans.BooleanConsumer listener)Registers a listener for writability changes.default byte[]remoteDataToArray()Drains queued outbound packets into a newly allocated byte array.voidremoteDataToArray(io.aether.utils.dataio.DataOut out)Drains queued outbound packets into the supplied output buffer.default voidsendToRemote(byte[] data)Queues an immutable snapshot of the supplied byte array for delivery to the remote endpoint.voidsendToRemote(LazyPacket data)Queues a lazily serialized packet for delivery to the remote endpoint.voidsetActivePredicate(io.aether.utils.interfaces.ASupplierBoolean predicate)Sets the predicate used to determine the local active state.voidsetLocalMethodBeforeListener(io.aether.utils.interfaces.AConsumer<java.lang.String> listener)Installs a listener invoked before a decoded local API method executes.intsize()Returns the serialized size of outbound data currently exposed by this context.default <T,R extends RemoteApi>
voidswitchLocalApi(T localApi, FastMetaApi<T,R> meta)Requests replacement of the local API implementation at the next safe command boundary.
-
-
-
Field Detail
-
STUB
static final MetaContext STUB
-
CONTEXT_NOT_ACTIVE
static final java.lang.IllegalStateException CONTEXT_NOT_ACTIVE
Shared exception used when an operation is rejected because its transport context is inactive.
-
CONTEXT_NOT_ACTIVE_FUTURE
static final io.aether.utils.futures.ARFuture CONTEXT_NOT_ACTIVE_FUTURE
Pre-made future for inactive context errors — avoids allocation per rejected call.
-
CONTEXT_NOT_ACTIVE_AFUTURE
static final io.aether.utils.futures.AFuture CONTEXT_NOT_ACTIVE_AFUTURE
-
-
Method Detail
-
makeRemote
default <RT,RT2 extends RemoteApi> RT2 makeRemote(FastMetaApi<RT,RT2> meta)
Creates a typed remote API encoder view backed by this context.The returned object does not replace context state. Multiple remote API views may coexist and share the same outbound queues, future tracking, and connection lifecycle.
- Type Parameters:
RT- matching local API typeRT2- remote API view type- Parameters:
meta- META describing the requested remote API view- Returns:
- typed remote API encoder backed by this context
-
sendToRemote
void sendToRemote(LazyPacket data)
Queues a lazily serialized packet for delivery to the remote endpoint.- Parameters:
data- packet to serialize during outbound draining
-
sendToRemote
default void sendToRemote(byte[] data)
Queues an immutable snapshot of the supplied byte array for delivery to the remote endpoint.The default implementation copies the input array before constructing the lazy packet, so callers may safely modify the original array after this method returns.
- Parameters:
data- serialized FastMeta bytes to queue
-
isLocked
boolean isLocked()
Reports whether outbound flush/notification processing is currently suppressed bylock().- Returns:
truewhile flush suppression is active
-
lock
java.lang.AutoCloseable lock()
Temporarily suppresses outbound flush/notification processing.This is not a mutual-exclusion lock and must not be used to serialize writers. Closing the returned scope releases suppression and allows queued data to re-enter the normal flush lifecycle. Implementations may reject nested or concurrent acquisition.
- Returns:
- scope whose close operation releases flush suppression
-
flush
default void flush()
Requests immediate processing of the outbound flush lifecycle.Hierarchical implementations may delegate flushing to the connection root. A flush may be suppressed while
lock()is active.
-
invokeLocalMethodBefore
default void invokeLocalMethodBefore(java.lang.String methodName, java.lang.String[] argsNames, java.lang.Object[] argsValues)Hook invoked immediately before a decoded local API method executes.- Parameters:
methodName- local method nameargsNames- argument names, possiblynullargsValues- argument values, possiblynull
-
invokeLocalMethodAfter
default void invokeLocalMethodAfter(java.lang.String methodName, io.aether.utils.futures.AFutureBase<?> result, java.lang.String[] argsNames, java.lang.Object[] argsValues)Hook invoked after a decoded local API method finishes its invocation step.- Parameters:
methodName- local method nameresult- future returned by the local invocationargsNames- argument names, possiblynullargsValues- argument values, possiblynull
-
invokeRemoteMethodAfter
default void invokeRemoteMethodAfter(java.lang.String methodName, io.aether.utils.futures.AFutureBase<?> result, java.lang.String[] argsNames, java.lang.Object[] argsValues)Hook invoked after a remote API method has been encoded.- Parameters:
methodName- remote method nameresult- future associated with the remote invocationargsNames- argument names, possiblynullargsValues- argument values, possiblynull
-
remoteDataToArray
default byte[] remoteDataToArray()
Drains queued outbound packets into a newly allocated byte array.- Returns:
- serialized outbound bytes, possibly empty
-
remoteDataToArray
void remoteDataToArray(io.aether.utils.dataio.DataOut out)
Drains queued outbound packets into the supplied output buffer.- Parameters:
out- destination for serialized outbound bytes
-
close
default io.aether.utils.futures.AFuture close()
Closes this context and releases resources owned by the implementation.- Returns:
- future completed when closing has finished
-
onWritable
default void onWritable(it.unimi.dsi.fastutil.booleans.BooleanConsumer listener)
Registers a listener for writability changes.Implementations should invoke the listener immediately with the current state and subsequently when that state changes.
- Parameters:
listener- writability listener
-
onFirstWritable
default void onFirstWritable(io.aether.utils.interfaces.ARunnable listener)
Registers an action that runs once when writable state is first observed.- Parameters:
listener- action to run on first writability
-
fireWritable
default void fireWritable(boolean writable)
Publishes a writability-state change to listeners associated with this context.- Parameters:
writable- new writability state
-
isEmpty
boolean isEmpty()
Reports whether this context currently exposes flushable outbound data.An implementation may intentionally appear empty while flush suppression is active.
- Returns:
truewhen no outbound data is currently flushable
-
size
int size()
Returns the serialized size of outbound data currently exposed by this context.- Returns:
- queued outbound byte count
-
isActive
default boolean isActive()
Reports whether this context is currently active.Hierarchical implementations may require both the local context and all of its parents to be active.
- Returns:
truewhen the context is active
-
setActivePredicate
void setActivePredicate(io.aether.utils.interfaces.ASupplierBoolean predicate)
Sets the predicate used to determine the local active state.- Parameters:
predicate- active-state predicate, ornullto use the implementation default
-
setLocalMethodBeforeListener
void setLocalMethodBeforeListener(io.aether.utils.interfaces.AConsumer<java.lang.String> listener)
Installs a listener invoked before a decoded local API method executes.- Parameters:
listener- listener receiving the local method name, ornullto remove the listener
-
connectionContext
default MetaContext connectionContext()
Returns the root context representing the underlying transport connection.A root context returns itself; child contexts resolve through their parent chain.
- Returns:
- connection-level root context
-
findContext
MetaContext findContext(io.aether.utils.interfaces.AFunction<MetaContext,?> factory, java.lang.Object... keys)
Finds or creates a child context identified by the supplied composite key.Repeated lookups with an equal key resolve to the same live child context. The factory is used only when a child must be created or recreated.
- Parameters:
factory- initializer for a newly created child contextkeys- values forming the child-context key- Returns:
- existing or newly initialized child context
-
getLocalApi
java.lang.Object getLocalApi()
Returns the local API implementation currently installed in this context.A pending switch is not visible here until
applyLocalApiSwitch()applies it.- Returns:
- current local API implementation, or
nullwhen none is installed
-
switchLocalApi
default <T,R extends RemoteApi> void switchLocalApi(T localApi, FastMetaApi<T,R> meta)
Requests replacement of the local API implementation at the next safe command boundary.The switch is deliberately deferred. The command that requests the switch always finishes using the local API and META with which it started. The new local implementation and its target META form one coherent switch request and must never be changed independently.
Generated local decoders call
applyLocalApiSwitch()after a complete command. If unread bytes remain in the same input payload, decoding may continue immediately withmeta, allowing a V0 command and commands from an unrelated V1 API to coexist in one payload.The target META is not persistent transport decoder state. Each later isolated UDP/TCP payload must select its initial META again from application-level negotiated state.
- Type Parameters:
T- local API typeR- matching remote API type- Parameters:
localApi- new local API implementationmeta- META that decodes commands forlocalApi
-
applyLocalApiSwitch
default FastMetaApi<?,?> applyLocalApiSwitch()
Applies a previously requested local API switch at a command boundary.The implementation must flush bytes produced by the old local API before replacing it so those bytes retain the routing and flush policy of the API that produced them.
The returned META is intended only for unread bytes remaining in the current input payload. It must not become persistent decoder state for subsequent transport payloads.
- Returns:
- META for decoding the remainder of the current payload, or
nullwhen no switch is pending
-
getProperty
default java.lang.Object getProperty(int key)
Returns transport- or implementation-specific metadata associated with this context.Keys
1and2are conventionally used for the remote address and remote port respectively. Unsupported keys returnnullby default.- Parameters:
key- property identifier- Returns:
- property value, or
nullwhen unavailable
-
onFlush
void onFlush(io.aether.utils.interfaces.ARunnable flushAction)
Installs the action invoked by this context's flush lifecycle.- Parameters:
flushAction- action to run when the context is flushed
-
onFlushData
default void onFlushData(io.aether.utils.interfaces.AConsumer<byte[]> c)
Registers a convenience flush handler that drains queued outbound data and passes each non-empty serialized payload to the supplied consumer.- Parameters:
c- consumer receiving non-empty drained payloads
-
-