-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added shutting down calls to the visualization server #241
Changes from 1 commit
f7474d9
a628c1c
1c09a99
a49c584
52a0bca
8b48732
d2d4940
e47906a
4dd240a
f77b84a
5d99580
63756c3
d2e91cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,22 +59,37 @@ class Server(object): | |
port : integer | ||
Defines the port on which the server will run. | ||
scene_file : name of the scene_file generated for visualization | ||
Used here to display the url | ||
A Valid PyDy generated scene file in 'directory'. | ||
directory : absolute path of a directory | ||
Absolute path to the directory which contains scene_file with | ||
all other static files. | ||
|
||
Example | ||
------- | ||
>>> server = Server(scene_file=_scene_json_file) | ||
>>> server.run_server() | ||
|
||
""" | ||
def __init__(self, port=8000, scene_file="Null"): | ||
self.port = port | ||
self.scene_file = scene_file | ||
self.directory = "static/" | ||
def __init__(self, scene_file, directory=None, port=None): | ||
if scene_file | ||
self.scene_file = scene_file | ||
|
||
if directory: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comparisons to singletons like None should always be done with is or is not: |
||
self.directory = directory | ||
else: | ||
self.directory = "static/" | ||
|
||
if port: | ||
self.port = port | ||
else: | ||
self.port = 8000 | ||
|
||
def run_server(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if port 8000 is taken? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It throws an error. This needs to be fixed. |
||
# Change dir to static first. | ||
os.chdir(self.directory) | ||
# Get a free port | ||
while self._check_port(self.port): | ||
self.port += 1 | ||
handler_class = SimpleHTTPRequestHandler | ||
server_class = StoppableHTTPServer | ||
protocol = "HTTP/1.0" | ||
|
@@ -92,6 +107,14 @@ def run_server(self): | |
signal.signal(signal.SIGINT, self._stop_server) | ||
self.httpd.serve() | ||
|
||
def _check_port(self, port): | ||
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
result = soc.connect_ex(('127.0.0.1', port)) | ||
if result == 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return result == 0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
return True | ||
else: | ||
return False | ||
|
||
def _stop_server(self, signal, frame): | ||
""" | ||
Confirms and stops the visulisation server | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the case if the port is taken.