Skip to content

Commit

Permalink
Merge pull request bobjacobsen#41 from Hierosoft/settimeout-and-keybo…
Browse files Browse the repository at this point in the history
…ardinterrupt

Add settimeout and re-raise KeyboardInterrupt (resolves #6)...
  • Loading branch information
bobjacobsen authored May 17, 2024
2 parents 8e9483a + e904af8 commit d3b618f
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 3 deletions.
4 changes: 2 additions & 2 deletions example_cdi_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
# endregion same code as other examples

s = TcpSocket()
# s.settimeout(30)
s.connect(settings['host'], settings['port'])


Expand Down Expand Up @@ -106,7 +107,6 @@ def memoryReadSuccess(memo):
this queues a new read until the entire CDI has been
returned. At that point, it invokes the XML processing below.
Args:
memo (_type_): _description_
"""
Expand Down Expand Up @@ -167,7 +167,7 @@ def startElement(self, name, attrs):
print(" Atributes: ", attrs.getNames())

def endElement(self, name):
print(name, "cpntent:", self._flushCharBuffer())
print(name, "content:", self._flushCharBuffer())
print("End: ", name)
pass

Expand Down
1 change: 1 addition & 0 deletions example_datagram_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
localNodeID = "05.01.01.01.03.01"
farNodeID = "09.00.99.03.00.35"
s = TcpSocket()
# s.settimeout(30)
s.connect(settings['host'], settings['port'])

print("RR, SR are raw socket interface receive and send;"
Expand Down
1 change: 1 addition & 0 deletions example_frame_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# endregion same code as other examples

s = TcpSocket()
# s.settimeout(30)
s.connect(settings['host'], settings['port'])

print("RR, SR are raw socket interface receive and send;"
Expand Down
1 change: 1 addition & 0 deletions example_memory_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
# endregion same code as other examples

s = TcpSocket()
# s.settimeout(30)
s.connect(settings['host'], settings['port'])

print("RR, SR are raw socket interface receive and send;"
Expand Down
1 change: 1 addition & 0 deletions example_message_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# endregion same code as other examples

s = TcpSocket()
# s.settimeout(30)
s.connect(settings['host'], settings['port'])

print("RR, SR are raw socket interface receive and send; RL,"
Expand Down
1 change: 1 addition & 0 deletions example_node_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
# endregion same code as other examples

s = TcpSocket()
# s.settimeout(30)
s.connect(settings['host'], settings['port'])

print("RR, SR are raw socket interface receive and send;"
Expand Down
1 change: 1 addition & 0 deletions example_remote_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@


s = TcpSocket()
# s.settimeout(30)
s.connect(settings['host'], settings['port'])


Expand Down
1 change: 1 addition & 0 deletions example_string_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@


s = TcpSocket()
# s.settimeout(30)
s.connect(settings['host'], settings['port'])

#######################
Expand Down
1 change: 1 addition & 0 deletions example_tcp_message_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# endregion same code as other examples

s = TcpSocket()
# s.settimeout(30)
print("Using settings:")
print(settings.dumps())
s.connect(settings['host'], settings['port'])
Expand Down
1 change: 0 additions & 1 deletion openlcb/canbus/canframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def __init__(self, arg1, arg2, arg3=[]):
alias = arg2
self.header = (control << 12) | (alias & 0xFFF) | 0x10_000_000
self.data = arg3

else:
print("could not decode NodeID ctor arguments")

Expand Down
12 changes: 12 additions & 0 deletions openlcb/canbus/canlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ def handleReceivedAMR(self, frame): # CanFrame
try:
del self.aliasToNodeID[alias]
del self.nodeIdToAlias[nodeID]
except KeyboardInterrupt:
raise
except:
pass

Expand All @@ -221,6 +223,8 @@ def handleReceivedData(self, frame): # CanFrame
try:
mapped = self.aliasToNodeID[frame.header & 0xFFF]
sourceID = mapped
except KeyboardInterrupt:
raise
except:
# special case for JMRI before 5.1.5 which sends
# VerifiedNodeID but not AMD
Expand Down Expand Up @@ -305,6 +309,8 @@ def handleReceivedData(self, frame): # CanFrame
try:
mapped = self.aliasToNodeID[destAlias]
destID = mapped
except KeyboardInterrupt:
raise
except:
destID = NodeID(self.nextInternallyAssignedNodeID)
logging.warning("message from unknown dest alias:"
Expand Down Expand Up @@ -370,6 +376,8 @@ def sendMessage(self, msg):
try:
sssAlias = self.nodeIdToAlias[msg.source]
header |= ((sssAlias) & 0xFFF)
except KeyboardInterrupt:
raise
except:
logging.warning(
"Did not know source = {} on datagram send"
Expand All @@ -379,6 +387,8 @@ def sendMessage(self, msg):
try:
dddAlias = self.nodeIdToAlias[msg.destination]
header |= ((dddAlias) & 0xFFF) << 12
except KeyboardInterrupt:
raise
except:
logging.warning(
"Did not know destination = {} on datagram send"
Expand Down Expand Up @@ -581,6 +591,8 @@ def decodeControlFrameFormat(self, frame):
try:
retval = ControlFrame((frame.header >> 12) & 0x2FFFF)
return retval # top 1 bit for out-of-band messages
except KeyboardInterrupt:
raise
except:
logging.warning("Could not decode header 0x{:08X}"
"".format(frame.header))
Expand Down
9 changes: 9 additions & 0 deletions openlcb/canbus/tcpsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ def __init__(self, sock=None):
else:
self.sock = sock

def settimeout(self, seconds):
"""Set the timeout for connect and transfer.
Args:
seconds (float): The number of seconds to wait before
a timeout error occurs.
"""
self.sock.settimeout(seconds)

def connect(self, host, port):
self.sock.connect((host, port))

Expand Down
2 changes: 2 additions & 0 deletions openlcb/datagramservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def datagramType(self, data):
return DatagramService.ProtocolID.Unrecognized
try:
retval = DatagramService.ProtocolID(data[0])
except KeyboardInterrupt:
raise
except:
return DatagramService.ProtocolID.Unrecognized
if retval is not None:
Expand Down
2 changes: 2 additions & 0 deletions openlcb/memoryservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ def arrayToString(self, data, length):
try:
temp = data.index(0)
zeroIndex = temp
except KeyboardInterrupt:
raise
except:
pass

Expand Down
9 changes: 9 additions & 0 deletions openlcb/tcplink/tcpsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ def __init__(self, sock=None):
else:
self.sock = sock

def settimeout(self, seconds):
"""Set the timeout for connect and transfer.
Args:
seconds (float): The number of seconds to wait before
a timeout error occurs.
"""
self.sock.settimeout(seconds)

def connect(self, host, port):
self.sock.connect((host, port))

Expand Down

0 comments on commit d3b618f

Please sign in to comment.