From 750db15e00b9bbdadb5c34280f5de74215259b63 Mon Sep 17 00:00:00 2001 From: Hanno Rein Date: Tue, 7 Nov 2023 09:06:13 -0500 Subject: [PATCH] widget logic --- rebound/simulation.py | 21 ++++++++++++++------- src/server.c | 4 ++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/rebound/simulation.py b/rebound/simulation.py index 0d406cb48..837c2a93f 100644 --- a/rebound/simulation.py +++ b/rebound/simulation.py @@ -196,13 +196,20 @@ def start_server(self, port=1234): self.process_messages() - def widget(self, port=1234, host="localhost", size=(500,500)): - port = int(port) - if self._server_data: - if self._server_data.contents.port != port: - raise RuntimeError("Server is running already on port %d but port %d was requested."%(self._server_data.contents.port, port)) - else: - self.start_server(port=port) + def widget(self, port=None, host="localhost", size=(500,500)): + if port is not None: + port = int(port) + if self._server_data: + if self._server_data.contents.port != port: + raise RuntimeError("Server is running already on port %d but port %d was requested."%(self._server_data.contents.port, port)) + if not self._server_data: + if port is not None: + self.start_server(port=port) + else: + self.start_server() + if not self._server_data: + raise RuntimeError("Server did not start up immediately. Try again in a few seconds.") + port = int(self._server_data.contents.port) from IPython.display import IFrame width, height = size display(IFrame("http://"+host+":"+"%d"%port, width, height)) diff --git a/src/server.c b/src/server.c index 41fb7c195..908f51f47 100644 --- a/src/server.c +++ b/src/server.c @@ -175,7 +175,7 @@ void* reb_server_start(void* args){ memset((char *) &serveraddr, 0, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); - serveraddr.sin_port = htons((unsigned short)data->port); + serveraddr.sin_port = htons(data->port); if (bind(data->socket, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0){ char error_msg[BUFSIZE]; snprintf(error_msg, BUFSIZE, "Error binding to port %d. Port might be in use.\n", data->port); @@ -199,7 +199,7 @@ void* reb_server_start(void* args){ data->ready = 1; childfd = accept(data->socket, (struct sockaddr *) &clientaddr, &clientlen); if (childfd < 0) { // Accept will fail if main thread is closing socket. - return 1; + return PTHREAD_CANCELED; } /* determine who sent the message */