Skip to content

Commit

Permalink
Remove sockJS and use websocket for the front-end.
Browse files Browse the repository at this point in the history
This attempts to remove both tornado-sockjs from the HTF servers and sockJS from the front-end getting served.

SockJS is a websocket emulator used as a compatibility shim in environments that don't support the websocket API. However, the TornadoSockJS library is no longer being maintained.

PiperOrigin-RevId: 647392280
  • Loading branch information
tan01 authored and copybara-github committed Jul 8, 2024
1 parent 6060cd0 commit 196dc63
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
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

0 comments on commit 196dc63

Please sign in to comment.