Skip to content

Commit

Permalink
implement network interface detection and prioritization
Browse files Browse the repository at this point in the history
  • Loading branch information
itsknk committed Jul 21, 2024
1 parent 95f22ed commit 41a9f63
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions exo/networking/grpc/grpc_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
import netifaces
import psutil
import subprocess
from typing import List, Dict, Callable, Tuple, Coroutine
from ..discovery import Discovery
from ..peer_handle import PeerHandle
Expand Down Expand Up @@ -37,13 +38,40 @@ def __init__(self, node_id: str, node_port: int, listen_port: int, broadcast_por
self.cleanup_task = None

def get_network_interfaces(self):
interfaces = netifaces.interfaces()
if psutil.MACOS:
thunderbolt_interfaces = [iface for iface in interfaces if iface.startswith('thunderbolt')]
wifi_interfaces = [iface for iface in interfaces if iface.startswith('en')]
return thunderbolt_interfaces + wifi_interfaces # Prioritize Thunderbolt
# Use system_profiler to get detailed network information
network_info = subprocess.check_output(['system_profiler', 'SPNetworkDataType', '-json']).decode('utf-8')
network_data = json.loads(network_info)

thunderbolt_interfaces = []
wifi_interfaces = []
other_interfaces = []

for interface in network_data.get('SPNetworkDataType', []):
interface_name = interface.get('interface')
interface_type = interface.get('type', '').lower()
hardware = interface.get('hardware', '').lower()

if not interface_name:
continue

# Check for Thunderbolt-related keywords
if 'thunderbolt' in hardware or 'thunderbolt' in interface_type:
thunderbolt_interfaces.append(interface_name)
elif interface_type == 'wi-fi':
wifi_interfaces.append(interface_name)
else:
other_interfaces.append(interface_name)

if self.DEBUG >= 2:
print(f"Thunderbolt interfaces: {thunderbolt_interfaces}")
print(f"WiFi interfaces: {wifi_interfaces}")
print(f"Other interfaces: {other_interfaces}")

# Prioritize Thunderbolt, then WiFi, then others
return thunderbolt_interfaces + wifi_interfaces + other_interfaces
else:
return interfaces
return netifaces.interfaces()

async def start(self):
self.device_capabilities = device_capabilities()
Expand Down

0 comments on commit 41a9f63

Please sign in to comment.