11-25-2022, 02:26 AM
When you launch OAP, the API service is running and the tcp socket is open for connections. Connect via a client (let's pick openauto-pro-api/api_examples/python/KeyStrokes.py) and the client connects.
If OAP gets closed at this time, the client throws a traceback, but does not close:
If you open OAP back up and try to send data to the API, the client app crashes. This behavior makes sense because the client no longer has a connection to the API service.
I've been trying to figure out a way to gracefully handle/detect the API disconnect and attempt to reconnect automatically. I use these functions to detect if the API service is ready when my client scripts first launch, but if the server disconnects while the client is connected, it's not handled gracefully.
Some of my scripts run as a service and are configured to automatically restart upon script failure so I'm not too concerned about those. It is the scripts that cannot be handled by a service that I'd like to handle the reconnection with.
Does anybody have any ideas/pointers?
pi@raspberrypi:~ $ python3 scripts/openauto-pro-api/api_examples/python/KeyStrokes.py
received hello response, result: 1, oap version: 16.0, api version: 1.1
Enter key type (type break to exit):
If OAP gets closed at this time, the client throws a traceback, but does not close:
Traceback (most recent call last):
File "scripts/openauto-pro-api/api_examples/python/KeyStrokes.py", line 124, in <module>
main()
File "scripts/openauto-pro-api/api_examples/python/KeyStrokes.py", line 116, in main
active = client.wait_for_message()
File "/home/pi/scripts/openauto-pro-api/api_examples/python/common/Client.py", line 130, in wait_for_message
message = self.receive()
File "/home/pi/scripts/openauto-pro-api/api_examples/python/common/Client.py", line 108, in receive
flags) = struct.unpack('<III', header_data[:header_size])
struct.error: unpack requires a buffer of 12 bytes
If you open OAP back up and try to send data to the API, the client app crashes. This behavior makes sense because the client no longer has a connection to the API service.
MODE
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "scripts/openauto-pro-api/api_examples/python/KeyStrokes.py", line 88, in listen_for_key_events
key_event.SerializeToString())
File "/home/pi/scripts/openauto-pro-api/api_examples/python/common/Client.py", line 116, in send
self._socket.sendall(payload)
BrokenPipeError: [Errno 32] Broken pipe
I've been trying to figure out a way to gracefully handle/detect the API disconnect and attempt to reconnect automatically. I use these functions to detect if the API service is ready when my client scripts first launch, but if the server disconnects while the client is connected, it's not handled gracefully.
def IsAPIReady():
import socket
_api_ready = False
# Test the API socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1', 44405))
sock.close()
if result == 0:
_api_ready = True
print("OAP API is ready")
else:
print("Waiting for OAP API")
return _api_ready
def WaitForAPI():
import time
while not IsAPIReady():
time.sleep(2)
# Wait for socket to be open
WaitForAPI()
Some of my scripts run as a service and are configured to automatically restart upon script failure so I'm not too concerned about those. It is the scripts that cannot be handled by a service that I'd like to handle the reconnection with.
Does anybody have any ideas/pointers?