@@ -171,7 +171,7 @@ def start_listening(self, connected_port, localNodeID):
171171 # self._physicalLayer.registerFrameReceivedListener(
172172 # self._printFrame
173173 # )
174- # ^ Commented since canlink already adds CanLink's default
174+ # ^ Commented since CanLink constructor now registers its default
175175 # receiveListener to CanLinkPhysicalLayer & that's all we need
176176 # for this application.
177177
@@ -203,18 +203,21 @@ def start_listening(self, connected_port, localNodeID):
203203 self .listen () # Must listen for alias reservation responses
204204 # (sendAliasConnectionSequence will occur for another 200ms
205205 # once, then another 200ms on each alias collision if any)
206+ # - must also keep doing frame = pollFrame() and sending
207+ # if not None.
206208
207209 self ._callback_status ("physicalLayerUp..." )
208210 self ._physicalLayer .physicalLayerUp ()
209- while canLink .pollState () != CanLink .State .Permitted :
211+ self ._callback_status ("Waiting for alias reservation..." )
212+ while self ._canLink .pollState () != CanLink .State .Permitted :
210213 precise_sleep (.02 )
211-
212214 # ^ triggers fireListeners which calls CanLink's default
213215 # receiveListener by default since added on CanPhysicalLayer
214216 # arg of linkPhysicalLayer.
215217 # - Must happen *after* listen thread starts, since
216218 # generates ControlFrame.LinkUp and calls fireListeners
217219 # which calls sendAliasConnectionSequence on this thread!
220+ self ._callback_status ("Alias reservation complete." )
218221
219222 def listen (self ):
220223 self ._listen_thread = threading .Thread (
@@ -244,19 +247,49 @@ def _listen(self):
244247 # the alias from each node (collision or not)
245248 # to has to get the expected replies to the alias
246249 # reservation sequence below.
247- precise_sleep (.05 ) # Wait for physicalLayerUp to complete
250+ precise_sleep (.05 ) # Wait for physicalLayerUp non-network Message
248251 while True :
249252 # Wait 200 ms for all nodes to announce (and for alias
250253 # reservation to complete), as per section 6.2.1 of CAN
251254 # Frame Transfer Standard (sendMessage requires )
252255 logger .debug ("[_listen] _receive..." )
253256 try :
254- sends = self ._physicalLayer .popFrames ()
255- while sends :
257+ # Receive mode (switches to write mode on BlockingIOError
258+ # which is expected and used on purpose)
259+ # print("Waiting for _receive")
260+ received = self ._receive () # requires setblocking(False)
261+ print ("[_listen] received {} byte(s)" .format (len (received )),
262+ file = sys .stderr )
263+ # print(" RR: {}".format(received.strip()))
264+ # pass to link processor
265+ self ._physicalLayer .handleData (received )
266+ # ^ will trigger self._printFrame if that was added
267+ # via registerFrameReceivedListener during connect.
268+ precise_sleep (.01 ) # let processor sleep before read
269+ if time .perf_counter () - self ._connecting_t > .2 :
270+ if self ._canLink ._state != CanLink .State .Permitted :
271+ if ((self ._message_t is None )
272+ or (time .perf_counter () - self ._message_t
273+ > 1 )):
274+ logger .warning (
275+ "CanLink is not ready yet."
276+ " There must have been a collision"
277+ "--processCollision increments node alias"
278+ " in this case and tries again." )
279+ # else _on_link_state_change will be called
280+ # TODO: move *all* send calls to this loop.
281+ except BlockingIOError :
282+ # Nothing to receive right now, so perform all sends
283+ # This *must* occur (require socket.setblocking(False))
284+ # sends = self._physicalLayer.popFrames()
285+ # while sends:
286+ while True :
256287 # *Always* do send in the receive thread to
257288 # avoid overlapping calls to socket
258289 # (causes undefined behavior)!
259- frame = sends .pop ()
290+ frame = self ._physicalLayer .pollFrame ()
291+ if frame is None :
292+ break # allow receive to run!
260293 if isinstance (frame , CanFrame ):
261294 if self ._canLink .isDuplicateAlias (frame .alias ):
262295 logger .warning (
@@ -266,43 +299,23 @@ def _listen(self):
266299 .format (frame .alias ))
267300 continue
268301 logger .debug ("[_listen] _sendString..." )
269- self ._port .sendString (frame .encodeAsString ())
302+ packet = frame .encodeAsString ()
303+ assert isinstance (packet , str )
304+ print ("Sending {}" .format (packet ))
305+ self ._port .sendString (packet )
270306 if frame .afterSendState :
271307 self ._canLink .setState (frame .afterSendState )
272308 else :
273309 raise NotImplementedError (
274310 "Event type {} is not handled."
275311 .format (type (frame ).__name__ ))
276- received = self ._receive () # requires setblocking(False)
277312 # so that it doesn't block (or occur during) recv
278313 # (overlapping calls would cause undefined behavior)!
279- # TODO: move *all* send calls to this loop.
280- except BlockingIOError :
281314 # delay = random.uniform(.005,.02)
282315 # ^ random delay may help if send is on another thread
283316 # (but avoid that for stability and speed)
284317 precise_sleep (.01 )
285- continue
286- print ("[_listen] received {} byte(s)" .format (len (received )),
287- file = sys .stderr )
288- # print(" RR: {}".format(received.strip()))
289- # pass to link processor
290- self ._physicalLayer .handleData (received )
291- # ^ will trigger self._printFrame if that was added
292- # via registerFrameReceivedListener during connect.
293- precise_sleep (.01 ) # let processor sleep briefly before read
294- if time .perf_counter () - self ._connecting_t > .2 :
295- if self ._canLink ._state != CanLink .State .Permitted :
296- if ((self ._message_t is None )
297- or (time .perf_counter () - self ._message_t
298- > 1 )):
299- logger .warning (
300- "CanLink is not ready yet."
301- " There must have been a collision"
302- "--processCollision increments node alias"
303- " in this case and tries again." )
304- # else _on_link_state_change will be called
305-
318+ # raise RuntimeError("We should never get here")
306319 except RuntimeError as ex :
307320 caught_ex = ex
308321 # If _port is a TcpSocket:
0 commit comments