Class MetaContextBase
- java.lang.Object
-
- io.aether.net.fastMeta.MetaContextBase
-
- All Implemented Interfaces:
MetaContext,io.aether.utils.ToString
- Direct Known Subclasses:
AutoFlushContext
public class MetaContextBase extends java.lang.Object implements MetaContext
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.atomic.AtomicBooleanfirstWritableFlagprotected io.aether.utils.interfaces.ARunnableflushActionjava.lang.ObjectlocalApiprotected MetaContextparentprotected java.util.Queue<LazyPacket>toRemoteprotected it.unimi.dsi.fastutil.booleans.BooleanConsumerwritableListener-
Fields inherited from interface io.aether.net.fastMeta.MetaContext
CONTEXT_NOT_ACTIVE, CONTEXT_NOT_ACTIVE_AFUTURE, CONTEXT_NOT_ACTIVE_FUTURE, STUB
-
-
Constructor Summary
Constructors Constructor Description MetaContextBase()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected io.aether.utils.dataio.DataInOutacquireDataOut()FastMetaApi<?,?>applyLocalApiSwitch()Applies a previously requested local API switch at a command boundary.MetaContextconnectionContext()Returns the root context representing the underlying transport connection.protected MetaContextBasecreateChildContext()MetaContextfindContext(io.aether.utils.interfaces.AFunction<MetaContext,?> factory, java.lang.Object... keys)Finds or creates a child context identified by the supplied composite key.voidfireWritable(boolean writable)Publishes a writability-state change to listeners associated with this context.voidflush()Requests immediate processing of the outbound flush lifecycle.java.lang.ObjectgetLocalApi()Returns the local API implementation currently installed in this context.voidinvokeLocalMethodBefore(java.lang.String methodName, java.lang.String[] argsNames, java.lang.Object[] argsValues)Hook invoked immediately before a decoded local API method executes.booleanisActive()Reports whether this context is currently active.booleanisEmpty()Reports whether this context currently exposes flushable outbound data.protected booleanisLocallyActive()booleanisLocked()Reports whether outbound flush/notification processing is currently suppressed byMetaContext.lock().java.lang.AutoCloseablelock()Temporarily suppresses outbound flush/notification processing.protected voidnotifyDataAdded()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.voidonWritable(it.unimi.dsi.fastutil.booleans.BooleanConsumer listener)Registers a listener for writability changes.protected voidreleaseDataOut(io.aether.utils.dataio.DataInOut out)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.voidretire()protected voidrunFlushLifecycle()voidsendToRemote(LazyPacket packet)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.voidsetLocalApi(java.lang.Object localApi)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.<T,R extends RemoteApi>
voidswitchLocalApi(T localApi, FastMetaApi<T,R> meta)Requests replacement of the local API implementation at the next safe command boundary.io.aether.utils.AStringtoAString(io.aether.utils.AString sb)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.aether.net.fastMeta.MetaContext
close, getProperty, invokeLocalMethodAfter, invokeRemoteMethodAfter, makeRemote, onFlushData, sendToRemote
-
-
-
-
Field Detail
-
toRemote
protected final java.util.Queue<LazyPacket> toRemote
-
firstWritableFlag
protected final java.util.concurrent.atomic.AtomicBoolean firstWritableFlag
-
localApi
public java.lang.Object localApi
-
parent
protected MetaContext parent
-
writableListener
protected volatile it.unimi.dsi.fastutil.booleans.BooleanConsumer writableListener
-
flushAction
protected io.aether.utils.interfaces.ARunnable flushAction
-
-
Method Detail
-
isLocallyActive
protected boolean isLocallyActive()
-
setLocalMethodBeforeListener
public void setLocalMethodBeforeListener(io.aether.utils.interfaces.AConsumer<java.lang.String> listener)
Description copied from interface:MetaContextInstalls a listener invoked before a decoded local API method executes.- Specified by:
setLocalMethodBeforeListenerin interfaceMetaContext- Parameters:
listener- listener receiving the local method name, ornullto remove the listener
-
invokeLocalMethodBefore
public void invokeLocalMethodBefore(java.lang.String methodName, java.lang.String[] argsNames, java.lang.Object[] argsValues)Description copied from interface:MetaContextHook invoked immediately before a decoded local API method executes.- Specified by:
invokeLocalMethodBeforein interfaceMetaContext- Parameters:
methodName- local method nameargsNames- argument names, possiblynullargsValues- argument values, possiblynull
-
setActivePredicate
public void setActivePredicate(io.aether.utils.interfaces.ASupplierBoolean predicate)
Description copied from interface:MetaContextSets the predicate used to determine the local active state.- Specified by:
setActivePredicatein interfaceMetaContext- Parameters:
predicate- active-state predicate, ornullto use the implementation default
-
findContext
public MetaContext findContext(io.aether.utils.interfaces.AFunction<MetaContext,?> factory, java.lang.Object... keys)
Description copied from interface:MetaContextFinds 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.
- Specified by:
findContextin interfaceMetaContext- Parameters:
factory- initializer for a newly created child contextkeys- values forming the child-context key- Returns:
- existing or newly initialized child context
-
flush
public void flush()
Description copied from interface:MetaContextRequests immediate processing of the outbound flush lifecycle.Hierarchical implementations may delegate flushing to the connection root. A flush may be suppressed while
MetaContext.lock()is active.- Specified by:
flushin interfaceMetaContext
-
runFlushLifecycle
protected void runFlushLifecycle()
-
getLocalApi
public java.lang.Object getLocalApi()
Description copied from interface:MetaContextReturns the local API implementation currently installed in this context.A pending switch is not visible here until
MetaContext.applyLocalApiSwitch()applies it.- Specified by:
getLocalApiin interfaceMetaContext- Returns:
- current local API implementation, or
nullwhen none is installed
-
setLocalApi
public void setLocalApi(java.lang.Object localApi)
-
switchLocalApi
public <T,R extends RemoteApi> void switchLocalApi(T localApi, FastMetaApi<T,R> meta)
Description copied from interface:MetaContextRequests 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
MetaContext.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.
- Specified by:
switchLocalApiin interfaceMetaContext- Type Parameters:
T- local API typeR- matching remote API type- Parameters:
localApi- new local API implementationmeta- META that decodes commands forlocalApi
-
applyLocalApiSwitch
public FastMetaApi<?,?> applyLocalApiSwitch()
Description copied from interface:MetaContextApplies 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.
- Specified by:
applyLocalApiSwitchin interfaceMetaContext- Returns:
- META for decoding the remainder of the current payload, or
nullwhen no switch is pending
-
onFlush
public void onFlush(io.aether.utils.interfaces.ARunnable flushAction)
Description copied from interface:MetaContextInstalls the action invoked by this context's flush lifecycle.- Specified by:
onFlushin interfaceMetaContext- Parameters:
flushAction- action to run when the context is flushed
-
toAString
public io.aether.utils.AString toAString(io.aether.utils.AString sb)
- Specified by:
toAStringin interfaceio.aether.utils.ToString
-
isLocked
public boolean isLocked()
Description copied from interface:MetaContextReports whether outbound flush/notification processing is currently suppressed byMetaContext.lock().- Specified by:
isLockedin interfaceMetaContext- Returns:
truewhile flush suppression is active
-
lock
public java.lang.AutoCloseable lock()
Description copied from interface:MetaContextTemporarily 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.
- Specified by:
lockin interfaceMetaContext- Returns:
- scope whose close operation releases flush suppression
-
sendToRemote
public void sendToRemote(LazyPacket packet)
Description copied from interface:MetaContextQueues a lazily serialized packet for delivery to the remote endpoint.- Specified by:
sendToRemotein interfaceMetaContext- Parameters:
packet- packet to serialize during outbound draining
-
notifyDataAdded
protected void notifyDataAdded()
-
isEmpty
public boolean isEmpty()
Description copied from interface:MetaContextReports whether this context currently exposes flushable outbound data.An implementation may intentionally appear empty while flush suppression is active.
- Specified by:
isEmptyin interfaceMetaContext- Returns:
truewhen no outbound data is currently flushable
-
isActive
public boolean isActive()
Description copied from interface:MetaContextReports whether this context is currently active.Hierarchical implementations may require both the local context and all of its parents to be active.
- Specified by:
isActivein interfaceMetaContext- Returns:
truewhen the context is active
-
connectionContext
public MetaContext connectionContext()
Description copied from interface:MetaContextReturns the root context representing the underlying transport connection.A root context returns itself; child contexts resolve through their parent chain.
- Specified by:
connectionContextin interfaceMetaContext- Returns:
- connection-level root context
-
onWritable
public void onWritable(it.unimi.dsi.fastutil.booleans.BooleanConsumer listener)
Description copied from interface:MetaContextRegisters a listener for writability changes.Implementations should invoke the listener immediately with the current state and subsequently when that state changes.
- Specified by:
onWritablein interfaceMetaContext- Parameters:
listener- writability listener
-
onFirstWritable
public void onFirstWritable(io.aether.utils.interfaces.ARunnable listener)
Description copied from interface:MetaContextRegisters an action that runs once when writable state is first observed.- Specified by:
onFirstWritablein interfaceMetaContext- Parameters:
listener- action to run on first writability
-
fireWritable
public void fireWritable(boolean writable)
Description copied from interface:MetaContextPublishes a writability-state change to listeners associated with this context.- Specified by:
fireWritablein interfaceMetaContext- Parameters:
writable- new writability state
-
size
public int size()
Description copied from interface:MetaContextReturns the serialized size of outbound data currently exposed by this context.- Specified by:
sizein interfaceMetaContext- Returns:
- queued outbound byte count
-
acquireDataOut
protected io.aether.utils.dataio.DataInOut acquireDataOut()
-
releaseDataOut
protected void releaseDataOut(io.aether.utils.dataio.DataInOut out)
-
remoteDataToArray
public byte[] remoteDataToArray()
Description copied from interface:MetaContextDrains queued outbound packets into a newly allocated byte array.- Specified by:
remoteDataToArrayin interfaceMetaContext- Returns:
- serialized outbound bytes, possibly empty
-
remoteDataToArray
public void remoteDataToArray(io.aether.utils.dataio.DataOut out)
Description copied from interface:MetaContextDrains queued outbound packets into the supplied output buffer.- Specified by:
remoteDataToArrayin interfaceMetaContext- Parameters:
out- destination for serialized outbound bytes
-
createChildContext
protected MetaContextBase createChildContext()
-
retire
public void retire()
-
-