Skip to content

Commit

Permalink
server finalizations
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Loftus committed Feb 18, 2024
1 parent efd89e2 commit da1e7bc
Showing 1 changed file with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tones
import globalPluginHandler
import os, json, socket, threading
import globalVars

# Exhaustive list of valid commands
schema = [
Expand All @@ -17,7 +18,8 @@
"debug"
]

SPEC_PATH = os.path.expanduser("~\\AppData\\Roaming\\nvda\\talon_server_spec.json")
# Handles both portable and installed versions of NVDA
SPEC_PATH = os.path.join(globalVars.appArgs.configPath, "talon_server_spec.json")

# Bind to an open port in case the specified port is not available
def bind_to_available_port(server_socket, start_port, end_port):
Expand Down Expand Up @@ -68,17 +70,17 @@ def handle_client(self, client_socket: socket.socket):

try:
messages = json.loads(data.decode().strip())
except json.JSONDecodeError:
print("Invalid JSON")
response = f"HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\n\r\nInvalid JSON".encode()
except json.JSONDecodeError as e:
print(f"RECEIVED INVALID JSON FROM TALON: {e}")
response = f"TALON SERVER ERROR: {e}".encode()
client_socket.sendall(response)
return

result = ""
for message in messages:
result += handle_command(message)

response = f"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nMessage received: Result: {result}".encode() # Create a HTTP response
response = f"TALON COMMAND PROCESSED: {result}".encode()
client_socket.sendall(response)

def output_spec_file(self):
Expand All @@ -104,31 +106,28 @@ def create_server(self):
self.output_spec_file()

self.server_socket.listen(1)
self.server_socket.settimeout(5)
print(f'\n\n\n\n\nSERVING TALON SERVER with {self.server_socket.getsockname()}')
# Need a time short enough that we can reboot NVDA and the old socket will be closed and won't interfere
self.server_socket.settimeout(0.5)
print(f'\n\n\n\n\nTALON SERVER SERVING ON {self.server_socket.getsockname()}')

self.running = True

while self.running:
try:
client_socket, addr = self.server_socket.accept()
print(f"\n\n\n\n\nConnection from {addr}")

client_socket, _ = self.server_socket.accept()
self.client_socket = client_socket
self.client_socket.settimeout(2)
self.client_socket.settimeout(0.3)
self.handle_client(self.client_socket)
except socket.timeout:
pass
except Exception as e:
print(f"\n\n\n\nTALON NVDA CRASH: {e}")
print(f"\n\n\n\nTALON SERVER CRASH: {e}")
self.stop()
break
finally:
if self.client_socket:
self.client_socket.close()

print("\n\n\n\n\nSERVER STOPPED")

def stop(self):
self.running = False
if self.server_socket:
Expand All @@ -137,6 +136,7 @@ def stop(self):
self.client_socket.close()
if os.path.exists(SPEC_PATH):
os.remove(SPEC_PATH)
print("\n\n\n\n\nTALON SERVER STOPPED")

class GlobalPlugin(globalPluginHandler.GlobalPlugin):

Expand Down

0 comments on commit da1e7bc

Please sign in to comment.