envision.server module

class envision.server.AllowCORSMixin[source]
options()[source]
set_default_headers()[source]
class envision.server.BroadcastWebSocket(application: tornado.web.Application, request: tornado.httputil.HTTPServerRequest, **kwargs: Any)[source]

This websocket receives the SMARTS state (the other end of the open websocket is held by the Envision Client (SMARTS)) and broadcasts it to all web clients that have open websockets via the StateWebSocket handler.

initialize(max_capacity_mb)[source]
on_close()[source]

Invoked when the WebSocket is closed.

If the connection was closed cleanly and a status code or reason phrase was supplied, these values will be available as the attributes self.close_code and self.close_reason.

Changed in version 4.0: Added close_code and close_reason attributes.

async on_message(message)[source]

Handle incoming messages on the WebSocket

This method must be overridden.

Changed in version 4.5: on_message can be a coroutine.

async open(simulation_id)[source]

Invoked when a new WebSocket is opened.

The arguments to open are extracted from the tornado.web.URLSpec regular expression, just like the arguments to tornado.web.RequestHandler.get.

open may be a coroutine. on_message will not be called until open has returned.

Changed in version 5.1: open may be a coroutine.

class envision.server.FileHandler(application: tornado.web.Application, request: tornado.httputil.HTTPServerRequest, **kwargs: Any)[source]
async get(id_)[source]
initialize(path_map: Dict[str, pathlib.Path] = {})[source]

FileHandler that serves file for a given ID.

async serve_chunked(path: pathlib.Path, chunk_size: int = 1048576)[source]
class envision.server.Frame(data: str, timestamp: int, next_=None)[source]
property data
property size
property timestamp
class envision.server.Frames(max_capacity_mb=500)[source]
append(frame: envision.server.Frame)[source]
property elapsed_time
property start_frame
property start_time
class envision.server.MainHandler(application: tornado.web.Application, request: tornado.httputil.HTTPServerRequest, **kwargs: Any)[source]
get()[source]
class envision.server.MapFileHandler(application: tornado.web.Application, request: tornado.httputil.HTTPServerRequest, **kwargs: Any)[source]
initialize(scenario_dirs: Sequence)[source]

FileHandler that serves file for a given ID.

class envision.server.ModelFileHandler(application: tornado.web.Application, request: tornado.httputil.HTTPServerRequest, **kwargs: Any)[source]
async get(id_)[source]
initialize()[source]

FileHandler that serves file for a given ID.

class envision.server.SimulationListHandler(application: tornado.web.Application, request: tornado.httputil.HTTPServerRequest, **kwargs: Any)[source]
async get()[source]
class envision.server.StateWebSocket(application: tornado.web.Application, request: tornado.httputil.HTTPServerRequest, **kwargs: Any)[source]
check_origin(origin)[source]

Override to enable support for allowing alternate origins.

The origin argument is the value of the Origin HTTP header, the url responsible for initiating this request. This method is not called for clients that do not send this header; such requests are always allowed (because all browsers that implement WebSockets support this header, and non-browser clients do not have the same cross-site security concerns).

Should return True to accept the request or False to reject it. By default, rejects all requests with an origin on a host other than this one.

This is a security protection against cross site scripting attacks on browsers, since WebSockets are allowed to bypass the usual same-origin policies and don’t use CORS headers.

Warning

This is an important security measure; don’t disable it without understanding the security implications. In particular, if your authentication is cookie-based, you must either restrict the origins allowed by check_origin() or implement your own XSRF-like protection for websocket connections. See these articles for more.

To accept all cross-origin traffic (which was the default prior to Tornado 4.0), simply override this method to always return True:

def check_origin(self, origin):
    return True

To allow connections from any subdomain of your site, you might do something like:

def check_origin(self, origin):
    parsed_origin = urllib.parse.urlparse(origin)
    return parsed_origin.netloc.endswith(".mydomain.com")

New in version 4.0.

get_compression_options()[source]

Override to return compression options for the connection.

If this method returns None (the default), compression will be disabled. If it returns a dict (even an empty one), it will be enabled. The contents of the dict may be used to control the following compression options:

compression_level specifies the compression level.

mem_level specifies the amount of memory used for the internal compression state.

These parameters are documented in details here: https://docs.python.org/3.6/library/zlib.html#zlib.compressobj

New in version 4.1.

Changed in version 4.5: Added compression_level and mem_level.

initialize()[source]
on_close()[source]

Invoked when the WebSocket is closed.

If the connection was closed cleanly and a status code or reason phrase was supplied, these values will be available as the attributes self.close_code and self.close_reason.

Changed in version 4.0: Added close_code and close_reason attributes.

async on_message(message)[source]

Handle incoming messages on the WebSocket

This method must be overridden.

Changed in version 4.5: on_message can be a coroutine.

async open(simulation_id)[source]

Invoked when a new WebSocket is opened.

The arguments to open are extracted from the tornado.web.URLSpec regular expression, just like the arguments to tornado.web.RequestHandler.get.

open may be a coroutine. on_message will not be called until open has returned.

Changed in version 5.1: open may be a coroutine.

class envision.server.WebClientRunLoop(frames, web_client_handler, timestep_sec, seek=None)[source]

The run loop is like a “video player” for the simulation. It supports seeking and playback. The run loop wraps the web client handler and pushes the frame messages to it as needed.

run_forever()[source]
seek(offset_seconds)[source]
stop()[source]
envision.server.main()[source]
envision.server.make_app(scenario_dirs: Sequence, max_capacity_mb: float)[source]
envision.server.on_shutdown()[source]
envision.server.run(scenario_dirs, max_capacity_mb=500, port=8081)[source]