Skip to content
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

Remove sockJS and use websocket for the front-end. #1164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions openhtf/output/servers/dashboard_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from openhtf.output.web_gui import web_launcher
from openhtf.util import data
from openhtf.util import multicast
import sockjs.tornado
import tornado.web

_LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -128,10 +127,11 @@ class DashboardServer(web_gui_server.WebGuiServer):
"""Serves a list of known stations and an Angular frontend."""

def __init__(self, port):
dash_router = sockjs.tornado.SockJSRouter(DashboardPubSub, '/sub/dashboard')
routes = dash_router.urls + [
routes = [
('/station_list', StationListHandler),
('/sub/dashboard', DashboardPubSub),
]
_LOG.info('Initializing dashboard server.')
super(DashboardServer, self).__init__(routes, port)

def _get_config(self):
Expand Down
18 changes: 12 additions & 6 deletions openhtf/output/servers/pub_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import logging

from openhtf import util as htf_util
import sockjs.tornado
import tornado.websocket

_LOG = logging.getLogger(__name__)


class PubSub(sockjs.tornado.SockJSConnection):
class PubSub(tornado.websocket.WebSocketHandler):
"""Generic pub/sub based on SockJS connections."""

@htf_util.classproperty
Expand Down Expand Up @@ -53,22 +53,28 @@ def publish(cls, message, client_filter=None):
if (not client_filter) or client_filter(client):
client.send(message)

def on_open(self, info):
_LOG.debug('New subscriber from %s.', info.ip)
def open(self, *args, **kwargs):
with self._lock: # pylint: disable=not-context-manager
self.subscribers.add(self)
self.on_subscribe(info)
self.on_subscribe(None)

def on_close(self):
_LOG.debug('A client unsubscribed.')
_LOG.info('A client unsubscribed.')
with self._lock: # pylint: disable=not-context-manager
self.subscribers.remove(self)
self.on_unsubscribe()

def send(self, message):
self.write_message(message)

def on_subscribe(self, info):
"""Called when new clients subscribe. Subclasses can override."""
pass

def on_unsubscribe(self):
"""Called when clients unsubscribe. Subclasses can override."""
pass

def check_origin(self, origin: str) -> bool:
"""Allows all cross-origin traffic. Dangerous."""
return True
17 changes: 9 additions & 8 deletions openhtf/output/servers/station_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from openhtf.util import functions
from openhtf.util import multicast
from openhtf.util import timeouts
import sockjs.tornado
import tornado

CONF = configuration.CONF

Expand Down Expand Up @@ -225,7 +225,7 @@ def _to_dict_with_event(cls, test_state):
return test_state_dict, event


class DashboardPubSub(sockjs.tornado.SockJSConnection):
class DashboardPubSub(tornado.websocket.WebSocketHandler):
"""WebSocket endpoint for the list of available stations.

In this case, there is always exactly one available station: the station
Expand All @@ -244,9 +244,9 @@ def for_port(cls, port):
"""Returns a new subclass with the port set."""
return type(cls.__name__, (cls,), {'port': port})

def on_open(self, unused_info):
def open(self, unused_info):
"""Called by the base class when a client connects."""
self.send(self._make_message())
self.write_message(self._make_message())

@classmethod
def _make_message(cls):
Expand Down Expand Up @@ -595,11 +595,12 @@ def __init__(
station_watcher = StationWatcher(StationPubSub.publish_update)
station_watcher.start()

# Set up the SockJS endpoints.
# Set up the endpoints.
dashboard_class = DashboardPubSub.for_port(port)
dash_router = sockjs.tornado.SockJSRouter(dashboard_class, '/sub/dashboard')
station_router = sockjs.tornado.SockJSRouter(StationPubSub, '/sub/station')
routes = dash_router.urls + station_router.urls
routes = [
('/sub/dashboard', dashboard_class),
('/sub/station', StationPubSub),
]

# Set up the other endpoints.
routes.extend((
Expand Down
Loading