Skip to content

Commit eabad44

Browse files
authored
Merge pull request #99 from Hierosoft/send-receive-naming
Send Receive naming
2 parents 8a1909e + 409b4b1 commit eabad44

14 files changed

Lines changed: 120 additions & 102 deletions

changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
All notable changes to this project will be documented in this file.
2+
3+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
4+
5+
## [Unreleased] - 2026-09-14
6+
7+
### Changed
8+
- Rename `DatagramWriteMemo` to **`DatagramSendMemo`** and `DatagramReadMemo` to **`DatagramReceiveMemo`** to distinguish meaning of read/write from the higher network layers (For example, in "DatagramWriteMemo" "Write" had two different meanings--one for port flow direction, one for request type, as it could be associated with a MemoryReadMemo. Now a MemoryReadMemo is associated with a "DatagramSendMemo" denoting it was requested by us but we may be still requesting a read such as in that case).

examples/example_cdi_access.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def printDatagram(memo):
9191
"""A call-back for when datagrams received
9292
9393
Args:
94-
DatagramReadMemo: The datagram object
94+
DatagramReceiveMemo: The datagram object
9595
9696
Returns:
9797
bool: Always False (True would mean we sent a reply to the datagram,

examples/example_datagram_transfer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from openlcb.nodeid import NodeID # noqa:E402
2929
from openlcb.datagramservice import ( # noqa:E402
3030
DatagramService,
31-
DatagramWriteMemo,
31+
DatagramSendMemo,
3232
)
3333

3434
# specify connection information
@@ -74,15 +74,15 @@ def printMessage(message):
7474

7575

7676
# create a call-back for replies to write datagram
77-
def writeCallBackCheck(memo):
77+
def sendCallBackCheck(memo):
7878
print("Write complete call back")
7979

8080

8181
def datagramReceiver(memo):
8282
"""A call-back for when datagrams received
8383
8484
Args:
85-
DatagramReadMemo: The datagram object
85+
DatagramReceiveMemo: The datagram object
8686
8787
Returns:
8888
bool: Always True (means we sent the reply to this datagram)
@@ -118,12 +118,12 @@ def datagramWrite():
118118
import time
119119
time.sleep(1)
120120

121-
writeMemo = DatagramWriteMemo(
121+
sendMemo = DatagramSendMemo(
122122
NodeID(settings['farNodeID']),
123123
bytearray([0x20, 0x43, 0x00, 0x00, 0x00, 0x00, 0x14]),
124-
writeCallBackCheck
124+
sendCallBackCheck
125125
)
126-
datagramService.sendDatagram(writeMemo)
126+
datagramService.sendDatagram(sendMemo)
127127

128128

129129
thread = threading.Thread(target=datagramWrite)

examples/example_memory_length_query.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
from openlcb.canbus.canlink import CanLink # noqa: E402
2727
from openlcb.nodeid import NodeID # noqa: E402
2828
from openlcb.datagramservice import ( # noqa: E402
29-
# DatagramWriteMemo,
30-
# DatagramReadMemo,
29+
# DatagramSendMemo,
30+
# DatagramReceiveMemo,
3131
DatagramService,
3232
)
3333
from openlcb.memoryservice import ( # noqa: E402
@@ -82,7 +82,7 @@ def printDatagram(memo):
8282
"""create a call-back to print datagram contents when received
8383
8484
Args:
85-
memo (DatagramReadMemo): The datagram received
85+
memo (DatagramReceiveMemo): The datagram received
8686
8787
Returns:
8888
bool: Always False (True would mean we sent a reply to this datagram,

examples/example_memory_transfer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
from openlcb.canbus.canlink import CanLink # noqa: E402
2828
from openlcb.nodeid import NodeID # noqa: E402
2929
from openlcb.datagramservice import ( # noqa: E402
30-
# DatagramWriteMemo,
31-
# DatagramReadMemo,
30+
# DatagramSendMemo,
31+
# DatagramReceiveMemo,
3232
DatagramService,
3333
)
3434
from openlcb.memoryservice import ( # noqa: E402
@@ -83,7 +83,7 @@ def printDatagram(memo):
8383
"""create a call-back to print datagram contents when received
8484
8585
Args:
86-
memo (DatagramReadMemo): The datagram received
86+
memo (DatagramReceiveMemo): The datagram received
8787
8888
Returns:
8989
bool: Always False (True would mean we sent a reply to this datagram,

examples/example_node_implementation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def printDatagram(memo):
9999
"""create a call-back to print datagram contents when received
100100
101101
Args:
102-
memo (DatagramReadMemo): The datagram received
102+
memo (DatagramReceiveMemo): The datagram received
103103
104104
Returns:
105105
bool: Always False (True would mean we sent a reply to the datagram,

examples/example_node_memory_implementation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
)
4949
from openlcb.canbus.canlink import CanLink # noqa: E402
5050
from openlcb.nodeid import NodeID # noqa: E402
51-
from openlcb.datagramservice import DatagramReadMemo, DatagramService # noqa: E402, E501
51+
from openlcb.datagramservice import DatagramReceiveMemo, DatagramService # noqa: E402, E501
5252
from openlcb.memoryservice import MemoryService # noqa: E402
5353
from openlcb.message import Message # noqa: E402
5454
from openlcb.mti import MTI # noqa: E402
@@ -151,11 +151,11 @@ def printMessage(message: Message):
151151
assert_xml(cdi)
152152

153153

154-
def handleDatagram(memo: DatagramReadMemo):
154+
def handleDatagram(memo: DatagramReceiveMemo):
155155
"""create a call-back to print datagram contents when received
156156
157157
Args:
158-
memo (DatagramReadMemo): The datagram received
158+
memo (DatagramReceiveMemo): The datagram received
159159
160160
Returns:
161161
bool: Always False (True would mean we sent a reply to the datagram,

openlcb/datagramservice.py

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Provide a service interface for reading and writing Datagrams.
77
88
Writes to remote node:
9-
- Create a ``DatagramWriteMemo`` and submit via ``sendDatagram(_:)``
9+
- Create a ``DatagramSendMemo`` and submit via ``sendDatagram(_:)``
1010
- Get an OK or NotOK callback
1111
1212
Reads from remote node:
@@ -40,12 +40,12 @@
4040

4141

4242
def defaultIgnoreReply(memo: Union[Any, None]):
43-
# ^ DatagramWriteMemo is the type, but that is not defined yet
43+
# ^ DatagramSendMemo is the type, but that is not defined yet
4444
'''default handling of reply does nothing'''
4545
pass
4646

4747

48-
class DatagramWriteMemo:
48+
class DatagramSendMemo:
4949
'''Immutable memo carrying write request and two reply callbacks
5050
(In this context "Write" means sent to other node, even if
5151
associated with a MemoryReadMemo).
@@ -54,7 +54,7 @@ class DatagramWriteMemo:
5454
def __init__(self, destID: NodeID, data,
5555
okReply=defaultIgnoreReply,
5656
rejectedReply=defaultIgnoreReply):
57-
# type: (NodeID, bytearray, Callable[[Union[DatagramWriteMemo, None]], None], Callable[[Union[DatagramWriteMemo, None]], None]) -> None # noqa: E501
57+
# type: (NodeID, bytearray, Callable[[Union[DatagramSendMemo, None]], None], Callable[[Union[DatagramSendMemo, None]], None]) -> None # noqa: E501
5858
assert isinstance(destID, NodeID)
5959
self.destID = destID
6060
# NOTE: No srcID since always from this node ("Write" means send
@@ -63,8 +63,8 @@ def __init__(self, destID: NodeID, data,
6363
raise TypeError("Expected bytearray (formerly list[int]), got {}"
6464
.format(type(data).__name__))
6565
self.data: bytearray = data
66-
self.okReply: Callable[[Union[DatagramWriteMemo, None]], None] = okReply # noqa: E501
67-
self.rejectedReply: Callable[[Union[DatagramWriteMemo, None]], None] = rejectedReply # noqa: E501
66+
self.okReply: Callable[[Union[DatagramSendMemo, None]], None] = okReply # noqa: E501
67+
self.rejectedReply: Callable[[Union[DatagramSendMemo, None]], None] = rejectedReply # noqa: E501
6868

6969
def __eq__(lhs, rhs):
7070
if lhs.destID != rhs.destID:
@@ -74,7 +74,7 @@ def __eq__(lhs, rhs):
7474
return True
7575

7676

77-
class DatagramReadMemo:
77+
class DatagramReceiveMemo:
7878
'''Immutable memo carrying read result
7979
(In this context "Read" means received from other node,
8080
*not* associated with a MemoryReadMemo which, however, may be what
@@ -100,6 +100,16 @@ class DatagramService:
100100
Args:
101101
linkLayer (CanLink): Could actually be any link layer such as
102102
LinkMockLayer (for testing) or CanLink.
103+
104+
Attributes:
105+
pendingSendMemos: (formerly pendingWriteMemos) These are written
106+
to the port, but a MemorySendMemo may be associated with a
107+
MemoryReadMemo since the device instantiating the service is
108+
making the request; or may be used to send replies as well
109+
(See where MemoryService instantiates any MemorySendMemo to
110+
respond to space info request especially when the node
111+
instantiating MemoryService is a node other than a
112+
Configuration Tool).
103113
"""
104114

105115
class ProtocolID(Enum):
@@ -117,9 +127,9 @@ class ProtocolID(Enum):
117127
def __init__(self, linkLayer: LinkLayer):
118128
self.linkLayer: LinkLayer = linkLayer
119129
self.quiesced: bool = False
120-
self.currentOutstandingMemo: Union[DatagramWriteMemo, None] = None # noqa: E501
121-
self.pendingWriteMemos: List[DatagramWriteMemo] = []
122-
self._datagramReceivedListeners: List[Callable[[DatagramReadMemo], bool]] = [] # noqa: E501
130+
self.currentOutstandingMemo: Union[DatagramSendMemo, None] = None # noqa: E501
131+
self.pendingSendMemos: List[DatagramSendMemo] = []
132+
self._datagramReceivedListeners: List[Callable[[DatagramReceiveMemo], bool]] = [] # noqa: E501
123133

124134
def datagramType(self, data: Union[bytearray, List[int]]):
125135
"""Determine the protocol type of the content of the datagram.
@@ -154,42 +164,42 @@ def checkDestID(self, message, nodeID: NodeID):
154164
assert isinstance(nodeID, NodeID)
155165
return message.destination == nodeID
156166

157-
def sendDatagram(self, memo: DatagramWriteMemo):
158-
'''Queue a ``DatagramWriteMemo`` to send a datagram to another node
167+
def sendDatagram(self, memo: DatagramSendMemo):
168+
'''Queue a ``DatagramSendMemo`` to send a datagram to another node
159169
on the network.
160170
'''
161171
# Make a record of memo for reply
162-
self.pendingWriteMemos.append(memo)
172+
self.pendingSendMemos.append(memo)
163173

164174
# can only have one outstanding at a time, so check it there was
165175
# already one there.
166-
if len(self.pendingWriteMemos) == 1:
176+
if len(self.pendingSendMemos) == 1:
167177
self.sendDatagramMessage(memo)
168178

169-
def sendDatagramMessage(self, memo: DatagramWriteMemo):
179+
def sendDatagramMessage(self, memo: DatagramSendMemo):
170180
'''Send datagram message'''
171181
message = Message(MTI.Datagram, self.linkLayer.localNodeID,
172182
memo.destID, memo.data)
173183
self.linkLayer.sendMessage(message)
174184
self.currentOutstandingMemo = memo
175185

176186
def registerDatagramReceivedListener(
177-
self, listener: Callable[[DatagramReadMemo], bool]):
187+
self, listener: Callable[[DatagramReceiveMemo], bool]):
178188
'''Register a listener to be notified when each datagram arrives.
179189
180190
One and only one listener should reply positively or negatively to the
181191
datagram and return true.
182192
183193
Args:
184-
listener (Callable): A function that accepts a DatagramReadMemo
194+
listener (Callable): A function that accepts a DatagramReceiveMemo
185195
as an argument.
186196
'''
187197
logger.debug(
188198
"REGISTERING registerDatagramReceivedListener listener"
189199
f" {len(self._datagramReceivedListeners) + 1}")
190200
self._datagramReceivedListeners.append(listener)
191201

192-
def fireDatagramReceived(self, dg: DatagramReadMemo): # internal for tests
202+
def fireDatagramReceived(self, dg: DatagramReceiveMemo): # internal for tests
193203
"""Fire *datagram received* listeners."""
194204
logger.debug(
195205
f"FIRING listeners for datagram from {dg.srcID},"
@@ -236,15 +246,15 @@ def process(self, message: Message):
236246

237247
def handleDatagram(self, message: Message):
238248
'''create a read memo and pass to listeners'''
239-
memo = DatagramReadMemo(message.source, message.data)
249+
memo = DatagramReceiveMemo(message.source, message.data)
240250
self.fireDatagramReceived(memo)
241251
# ^ destination listener calls back to
242252
# positiveReplyToDatagram/negativeReplyToDatagram before returning
243253

244254
def handleDatagramReceivedOK(self, message: Message):
245255
'''OK reply to write'''
246256
# match to the memo and remove from queue
247-
memo = self.matchToWriteMemo(message) # type: DatagramWriteMemo|None
257+
memo = self.matchToWriteMemo(message) # type: DatagramSendMemo|None
248258

249259
# check for whether a match was found, indicating this was for us
250260
if memo is None:
@@ -307,16 +317,16 @@ def handleLinkRestarted(self, message: Message):
307317
return
308318
else:
309319
# are there any queued datagrams? If so, send first
310-
if len(self.pendingWriteMemos) > 0:
320+
if len(self.pendingSendMemos) > 0:
311321
self.sendNextDatagramFromQueue()
312322

313323
def matchToWriteMemo(self, message: Message):
314-
for memo in self.pendingWriteMemos:
324+
for memo in self.pendingSendMemos:
315325
if memo.destID != message.source:
316326
continue # keep looking
317327
# remove the found element - might need a try/except on this
318-
index = self.pendingWriteMemos.index(memo)
319-
del self.pendingWriteMemos[index]
328+
index = self.pendingSendMemos.index(memo)
329+
del self.pendingSendMemos[index]
320330

321331
return memo
322332

@@ -327,28 +337,28 @@ def matchToWriteMemo(self, message: Message):
327337

328338
def sendNextDatagramFromQueue(self):
329339
# is there a next datagram request?
330-
if len(self.pendingWriteMemos) > 0:
340+
if len(self.pendingSendMemos) > 0:
331341
# yes, get it, process it
332-
memo = self.pendingWriteMemos[0]
342+
memo = self.pendingSendMemos[0]
333343
self.sendDatagramMessage(memo)
334344

335-
def positiveReplyToDatagram(self, dg: DatagramReadMemo, flags: int = 0):
345+
def positiveReplyToDatagram(self, dg: DatagramReceiveMemo, flags: int = 0):
336346
"""Send a positive reply to a received datagram.
337347
338348
Args:
339-
dg (DatagramReadMemo): Datagram memo being responded to.
349+
dg (DatagramReceiveMemo): Datagram memo being responded to.
340350
flags (Optional[int]): Flag byte to be returned to sender, see
341351
Datagram Standard & Technical Note for meaning. Defaults to 0.
342352
"""
343353
message = Message(MTI.Datagram_Received_OK, self.linkLayer.localNodeID,
344354
dg.srcID, bytearray([flags]))
345355
self.linkLayer.sendMessage(message)
346356

347-
def negativeReplyToDatagram(self, dg: DatagramReadMemo, err: int):
357+
def negativeReplyToDatagram(self, dg: DatagramReceiveMemo, err: int):
348358
"""Send a negative reply to a received datagram.
349359
350360
Args:
351-
dg (DatagramReadMemo): Datagram memo being responded to.
361+
dg (DatagramReceiveMemo): Datagram memo being responded to.
352362
err (int): Error code(s) to be returned to sender,
353363
see Datagram Standard & Technical Note for meaning.
354364
"""

0 commit comments

Comments
 (0)