Skip to content

Commit

Permalink
addon working
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Loftus committed Feb 15, 2024
1 parent d97224a commit 78feccc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
7 changes: 5 additions & 2 deletions core/addon_communication/ipc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ def addon_server_endpoint() -> Tuple[str, str, str]:
valid_commands = spec["valid_commands"]

try:
ip = ipaddress.ip_address(address)
if address == "localhost":
ip = ipaddress.ip_address(socket.gethostbyname(address))
else:
ip = ipaddress.ip_address(address)
assert ip.is_private, "Address is not a local IP address"
except ValueError:
raise ValueError(f"Invalid IP address: {address}")

return address, port, valid_commands


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
def bind_to_available_port(server_socket, start_port, end_port):
for port in range(start_port, end_port):
try:
server_socket.bind(('127.0.0.1', port))
server_socket.bind(('localhost', port))
return port
except OSError:
continue
Expand Down Expand Up @@ -60,12 +60,20 @@ def handle_command(command: str):
class IPC_Server():
port = None
server_socket = None
running = False
running = False
client_socket = None

def handle_client(self, client_socket: socket.socket):
data = client_socket.recv(1024)

messages = json.loads(data.decode().strip())
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()
client_socket.sendall(response)
return

result = ""
for message in messages:
result += handle_command(message)
Expand All @@ -76,7 +84,7 @@ def handle_client(self, client_socket: socket.socket):
def output_spec_file(self):
# write a json file to let clients know how to connect and what commands are available
spec = {
"address": "127.0.0.1",
"address": "localhost",
"port": str(self.get_port()),
"valid_commands": schema
}
Expand All @@ -97,7 +105,9 @@ def create_server(self):

self.server_socket.listen(1)
self.server_socket.settimeout(5)
print(f'\n\n\n\n\nSERVING TALON on {self.server_socket.getsockname()}')
print(f'\n\n\n\n\nSERVING TALON SERVER with {self.server_socket.getsockname()}')

self.running = True

while self.running:
try:
Expand All @@ -106,16 +116,18 @@ def create_server(self):

self.client_socket = client_socket
self.client_socket.settimeout(2)
self.handle_client(self.client_socket)
self.handle_client(self.client_socket)
except socket.timeout:
pass
print("timeout")
except Exception as e:
print(f"\n\n\n\nTALON NVDA 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
Expand Down
2 changes: 1 addition & 1 deletion sight-free-settings.talon
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ settings():
user.announce_mode_updates = true

# Display debugging output for NVDA
user.addon_debug = false
user.addon_debug = true

# Every given number of minutes, send a notification, prompting you to rest your eyes
# user.enable_break_timer = true
Expand Down

0 comments on commit 78feccc

Please sign in to comment.