-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3.9.0 - Add channel event streaming interface
- Loading branch information
Showing
15 changed files
with
274 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
major: 3 | ||
minor: 8 | ||
minor: 9 | ||
patch: 0 | ||
entry: runtimepy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
A module implementing a channel-event protocol. | ||
""" | ||
|
||
# built-in | ||
from contextlib import contextmanager | ||
from typing import BinaryIO, Iterator | ||
|
||
# internal | ||
from runtimepy.channel.event.header import PrimitiveEventHeader | ||
from runtimepy.primitives import AnyPrimitive | ||
|
||
|
||
class PrimitiveEvent: | ||
"""A class implementing a simple channel-even interface.""" | ||
|
||
def __init__(self, primitive: AnyPrimitive, identifier: int) -> None: | ||
"""Initialize this instance.""" | ||
|
||
self.primitive = primitive | ||
self.header = PrimitiveEventHeader.instance() | ||
PrimitiveEventHeader.init_header(self.header, identifier) | ||
self.prev_ns: int = 0 | ||
self.streaming = False | ||
|
||
@contextmanager | ||
def registered(self, stream: BinaryIO) -> Iterator[None]: | ||
"""Register a stream as a managed context.""" | ||
|
||
assert not self.streaming, "Already streaming!" | ||
|
||
def callback(_, __) -> None: | ||
"""Emit a change event to the stream.""" | ||
self._poll(stream, force=True) | ||
|
||
# Poll immediately. | ||
self.prev_ns = 0 | ||
self._poll(stream) | ||
|
||
raw = self.primitive | ||
ident = raw.register_callback(callback) | ||
|
||
self.streaming = True | ||
yield | ||
assert raw.remove_callback(ident) | ||
self.streaming = False | ||
|
||
def _poll(self, stream: BinaryIO, force: bool = False) -> int: | ||
""" | ||
Poll this event so that if the underlying channel has changed since the | ||
last write, we write another event. | ||
""" | ||
|
||
written = 0 | ||
|
||
# Check timestamp and update header if necessary. | ||
raw = self.primitive | ||
curr_ns = raw.last_updated_ns | ||
if force or curr_ns >= self.prev_ns: | ||
self.prev_ns = curr_ns | ||
self.header["timestamp"] = curr_ns | ||
|
||
# Write header then value. | ||
array = self.header.array | ||
written += array.to_stream(stream) | ||
written += raw.to_stream(stream, byte_order=array.byte_order) | ||
|
||
return written |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
A module implementing interfaces related to channel-protocol headers. | ||
""" | ||
|
||
# internal | ||
from runtimepy.codec.protocol import Protocol, ProtocolFactory | ||
from runtimepy.primitives import Uint16 | ||
|
||
IdType = Uint16 | ||
ID_SINGLE = IdType() | ||
|
||
|
||
class PrimitiveEventHeader(ProtocolFactory): | ||
"""A protocol for implementing channel events.""" | ||
|
||
@classmethod | ||
def initialize(cls, protocol: Protocol) -> None: | ||
"""Initialize this protocol.""" | ||
|
||
protocol.add_field("identifier", kind=IdType) | ||
protocol.add_field("timestamp", kind="uint64") | ||
|
||
@classmethod | ||
def init_header(cls, protocol: Protocol, identifier: int) -> None: | ||
"""Initialize a channel-event header.""" | ||
|
||
bounds = ID_SINGLE.kind.int_bounds | ||
assert bounds is not None | ||
assert bounds.validate(identifier), identifier | ||
protocol["identifier"] = identifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.