Skip to content

Commit

Permalink
widget logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hannorein committed Nov 7, 2023
1 parent 1ba1b67 commit 750db15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
21 changes: 14 additions & 7 deletions rebound/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */
Expand Down

0 comments on commit 750db15

Please sign in to comment.