We would like to establish one or more WebSocket connections on top of
an existing WebSocket channel between a device and a bridge. For
instance, this could be useful in the scenario where a remote user, who
has access to the bridge, wishes to open a remote terminal exposed by
the device on a specific port and make it accessible through a WebSocket
connection. The implementation of this functionality is described as
follows:
edgehog-device-runtime-forwarder/src/messages.rs has been extended
to reflect the changes in the proto.rs file, which contains the
Protobuf definition of the data exchanged
between the device and the bridge. Since this extension introduces the
WebSocket message type, now messages.rs contains the necessary structs
and impl blocks responsible for handling such messages.
edgehog-device-runtime-forwarder/src/connections_manager.rs also
handles the sending and receiving of WebSocket messages on an already
established WebSocket connection between the device and the bridge
(refer to the handle_ws() method). To establish a WebSocket
connection, the bridge first sends a triplet of HTTP messages, with the
third being an HTTP upgrade request to the WebSocket protocol. Once
this request is received, the device creates an internal WebSocket
connection using TTYD, a tool used
for sharing a remote terminal over a WebSocket interface. To simplify
the connections' collection stored within the ConnectionsManager
struct, both the HTTP and WebSocket connections are stored in the same
collection (avoiding the problem of managing two separate collections
for each type of connection). To this extent, the WebSocket connection
is given the same ID as the corresponding HTTP upgrade request.
edgehog-device-runtime-forwarder/src/connection.rs has been split into
submodules, containing now two builder traits: TransportBuilder and
Transport. These traits (declared in mod.rs) define the build() and next()
methods, respectively, which are necessary to construct and handle each
connection. Specifically, two builders that implement the TransportBuilder
trait have been defined to create an HTTP and a WebSocket connection,
respectively implemented in the connection/http.rs and connection/websocket.rs.
These builders are used when spawning the Tokio task responsible for handling
a single Connection. The Connection is now a struct that is generic
on a type that implements the Transport trait. This is why the Http
and WebSocket structs both implement this trait. The Http struct
sends an HTTP request and waits for a response, while the WebSocket
struct continuously sends and receives data to/from TTYD.
We would like to establish one or more WebSocket connections on top of an existing WebSocket channel between a device and a bridge. For instance, this could be useful in the scenario where a remote user, who has access to the bridge, wishes to open a remote terminal exposed by the device on a specific port and make it accessible through a WebSocket connection. The implementation of this functionality is described as follows:
edgehog-device-runtime-forwarder/src/messages.rs
has been extended to reflect the changes in theproto.rs
file, which contains theProtobuf
definition of the data exchanged between the device and the bridge. Since this extension introduces the WebSocket message type, nowmessages.rs
contains the necessary structs andimpl
blocks responsible for handling such messages.edgehog-device-runtime-forwarder/src/connections_manager.rs
also handles the sending and receiving of WebSocket messages on an already established WebSocket connection between the device and the bridge (refer to thehandle_ws()
method). To establish a WebSocket connection, the bridge first sends a triplet of HTTP messages, with the third being an HTTPupgrade
request to the WebSocket protocol. Once this request is received, the device creates an internal WebSocket connection usingTTYD
, a tool used for sharing a remote terminal over a WebSocket interface. To simplify the connections' collection stored within theConnectionsManager
struct, both the HTTP and WebSocket connections are stored in the same collection (avoiding the problem of managing two separate collections for each type of connection). To this extent, the WebSocket connection is given the same ID as the corresponding HTTP upgrade request.edgehog-device-runtime-forwarder/src/connection.rs
has been split into submodules, containing now two builder traits:TransportBuilder
andTransport
. These traits (declared inmod.rs
) define thebuild()
andnext()
methods, respectively, which are necessary to construct and handle each connection. Specifically, two builders that implement theTransportBuilder
trait have been defined to create an HTTP and a WebSocket connection, respectively implemented in theconnection/http.rs
andconnection/websocket.rs
. These builders are used when spawning the Tokio task responsible for handling a singleConnection
. TheConnection
is now a struct that is generic on a type that implements theTransport
trait. This is why theHttp
andWebSocket
structs both implement this trait. TheHttp
struct sends an HTTP request and waits for a response, while theWebSocket
struct continuously sends and receives data to/from TTYD.