66Provide a service interface for reading and writing Datagrams.
77
88Writes 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
1212Reads from remote node:
4040
4141
4242def 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