|
| 1 | +from ghpythonlib.componentbase import executingcomponent as component |
| 2 | +import socket |
| 3 | +import threading |
| 4 | +import json |
| 5 | +import scriptcontext as sc |
| 6 | +import Rhino.Geometry as rg |
| 7 | +import System.Drawing as sd |
| 8 | + |
| 9 | + |
| 10 | +class DFHTTPListener(component): |
| 11 | + def RunScript(self, i_load: bool, i_reset: bool, i_port: int, i_host: str): |
| 12 | + |
| 13 | + # Sticky defaults |
| 14 | + sc.sticky.setdefault('listen_addr', None) |
| 15 | + sc.sticky.setdefault('server_sock', None) |
| 16 | + sc.sticky.setdefault('server_started', False) |
| 17 | + sc.sticky.setdefault('cloud_buffer_raw', []) |
| 18 | + sc.sticky.setdefault('latest_cloud', None) |
| 19 | + sc.sticky.setdefault('status_message', "Waiting...") |
| 20 | + sc.sticky.setdefault('prev_load', False) |
| 21 | + |
| 22 | + # Handle Reset or host/port change |
| 23 | + addr = (i_host, i_port) |
| 24 | + if i_reset or sc.sticky['listen_addr'] != addr: |
| 25 | + # close old socket if any |
| 26 | + old = sc.sticky.get('server_sock') |
| 27 | + try: |
| 28 | + if old: old.close() |
| 29 | + except: pass |
| 30 | + |
| 31 | + sc.sticky['listen_addr'] = addr |
| 32 | + sc.sticky['server_sock'] = None |
| 33 | + sc.sticky['server_started'] = False |
| 34 | + sc.sticky['cloud_buffer_raw'] = [] |
| 35 | + sc.sticky['latest_cloud'] = None |
| 36 | + sc.sticky['status_message'] = "Reset" if i_reset else f"Addr → {i_host}:{i_port}" |
| 37 | + ghenv.Component.Message = sc.sticky['status_message'] |
| 38 | + |
| 39 | + # Client handler |
| 40 | + def handle_client(conn): |
| 41 | + buf = b'' |
| 42 | + with conn: |
| 43 | + while True: |
| 44 | + try: |
| 45 | + chunk = conn.recv(4096) |
| 46 | + if not chunk: |
| 47 | + break |
| 48 | + buf += chunk |
| 49 | + while b'\n' in buf: |
| 50 | + line, buf = buf.split(b'\n', 1) |
| 51 | + try: |
| 52 | + raw = json.loads(line.decode()) |
| 53 | + except Exception as e: |
| 54 | + sc.sticky['status_message'] = f"JSON error: {e}" |
| 55 | + ghenv.Component.Message = sc.sticky['status_message'] |
| 56 | + continue |
| 57 | + |
| 58 | + if isinstance(raw, list) and all(isinstance(pt, list) and len(pt)==6 for pt in raw): |
| 59 | + sc.sticky['cloud_buffer_raw'] = raw |
| 60 | + sc.sticky['status_message'] = f"Buffered {len(raw)} pts" |
| 61 | + else: |
| 62 | + sc.sticky['status_message'] = "Unexpected format" |
| 63 | + ghenv.Component.Message = sc.sticky['status_message'] |
| 64 | + except Exception as e: |
| 65 | + sc.sticky['status_message'] = f"Socket error: {e}" |
| 66 | + ghenv.Component.Message = sc.sticky['status_message'] |
| 67 | + break |
| 68 | + |
| 69 | + def accept_loop(srv_sock): |
| 70 | + while True: |
| 71 | + try: |
| 72 | + conn, _ = srv_sock.accept() |
| 73 | + threading.Thread(target=handle_client, args=(conn,), daemon=True).start() |
| 74 | + except: |
| 75 | + break |
| 76 | + |
| 77 | + #Start server |
| 78 | + def start_server(): |
| 79 | + try: |
| 80 | + srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 81 | + srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 82 | + srv.bind((i_host, i_port)) |
| 83 | + srv.listen() |
| 84 | + sc.sticky['server_sock'] = srv |
| 85 | + sc.sticky['server_started'] = True |
| 86 | + sc.sticky['status_message'] = f"Listening on {i_host}:{i_port}" |
| 87 | + ghenv.Component.Message = sc.sticky['status_message'] |
| 88 | + threading.Thread(target=accept_loop, args=(srv,), daemon=True).start() |
| 89 | + except Exception as e: |
| 90 | + sc.sticky['status_message'] = f"Server error: {e}" |
| 91 | + ghenv.Component.Message = sc.sticky['status_message'] |
| 92 | + |
| 93 | + if not sc.sticky['server_started']: |
| 94 | + start_server() |
| 95 | + |
| 96 | + if i_load and not sc.sticky['prev_load']: |
| 97 | + raw = sc.sticky['cloud_buffer_raw'] |
| 98 | + if raw: |
| 99 | + pc = rg.PointCloud() |
| 100 | + for x, y, z, r, g, b in raw: |
| 101 | + col = sd.Color.FromArgb(int(r), int(g), int(b)) |
| 102 | + pc.Add(rg.Point3d(x, y, z), col) |
| 103 | + sc.sticky['latest_cloud'] = pc |
| 104 | + sc.sticky['status_message'] = f"Retrieved {pc.Count} pts" |
| 105 | + else: |
| 106 | + sc.sticky['status_message'] = "No data buffered" |
| 107 | + ghenv.Component.Message = sc.sticky['status_message'] |
| 108 | + |
| 109 | + sc.sticky['prev_load'] = i_load |
| 110 | + |
| 111 | + return [sc.sticky['latest_cloud']] |
0 commit comments