Commit Graph

11 Commits (09342acb0113cb11fc603c71f482a861e21c438b)

Author SHA1 Message Date
Dustin 09342acb01 Support multiple monitors
For heads-up displays with multiple monitors, we're going to want one
Firefox window on each.  To support this, we need to get a list of
connected monitors from the operating system and associate each with its
own window.  Since Firefox may start with multiple taps open
automatically, we first close all but one and associate it with the
first monitor.  Then, for each remaining monitor, we open a new window
to associate with it.

To maintain the monitor-window association, the `Session` structure has
a `HashMap`.  When a naigation request arrives, the Firefox window to
control is found by looking up the specified screen name in the map.
Since the Marionette protocol is stateful, we have to "switch to" the
desired window and then send the navigation command.

I have tried to design the monitor information lookup API so that it can
be swapped out at compile time for different operating systems.  For
now, only X11 is supported, but we could hypothetically support Wayland
or even Windows by implementing the appropriate `get_monitors` function
for those APIs.
2023-01-01 12:32:21 -06:00
Dustin 4eba92f4a0 Publish final URL after navigation
If the URL specified in a navigation command results in a redirect, we
want to publish the final destination, rather than the provided
location.  Thus, after navigation completes, we get the browser's
current URL and publish that instead.
2022-12-31 10:43:01 -06:00
Dustin 8431a33f20 marionette: Separate commands from connection
The `MarionetteConnection` structure provides the low-level interface to
communicate with the Marionette server via the TCP socket.  In contrast,
the `Marionette` structure provides the high-level interface to execute
Marionette commands.  Separating these will make the code a bit cleaner,
in my opinion.
2022-12-31 10:28:03 -06:00
Dustin 3b1be2d01c marionette: Handle error responses
Marionette commands can return error responses, e.g. if a command fails
or is invalid.  We want to propagate these back to the caller whenever
possible.  The error object is specified in the Marionette protocol
documentation, so we can model it appropriately.
2022-12-30 22:04:44 -06:00
Dustin 4820d0f6cd Begin MQTT control implementation
The pieces are starting to come together.  To control the browser via
MQTT messages, the `MqttClient` dispatches messages via a
`MessageHandler`, which parses them and makes the appropriate Marionette
requests.  The `MessageHandler` trait defines callback methods for each
MQTT control operation, which currently is just `navigate`.  The
operation type is determined by the MQTT topic on which the message was
received.

Several new types are necessary to make this work.  The `MessageHandler`
trait and implementation are of course the core, reacting to incoming
MQTT messages.  In order for the handler to be able to *send* MQTT
messages, though, it needs a reference to the Paho MQTT client.  The
`MqttPublisher` provides a convenient wrapper around the client, with
specific methods for each type of message to send.  Finally, there's the
`MessageType` enumeration, which works in conjunction with the
`TopicMatcher` to match topic names to message types using topic filter
patterns.
2022-12-30 19:06:27 -06:00
Dustin 41c87d87af mqtt: Add MqttClient::connect method
Separating the `connect` call out of the `MqttClient::new` function
makes is such that we do not have to create a new object for each
iteration of the initial connection loop.  Instead, we just create one
object and repeatedly call its `connect` method until it succeeds
2022-12-30 14:39:10 -06:00
Dustin cef128d1ef main: Add wait_signal function
Moving the signal handler setup and wait to a separate function will
allow us to eventually create platform-specific implementations.
Windows doesn't have SIGINT/SIGTERM, so we will need different logic if
we ever want to support that OS.
2022-12-30 14:09:24 -06:00
Dustin ee8ed0c644 Add basic MQTT client functionality
Naturally, we need a way to configure the MQTT connection parameters
(host, port, username, etc.).  For that, we'll use a TOML configuration
file, which is read at startup and deserialized into a structure owned
by the Session.

The Session object now has a `run` method, which establishes the MQTT
connection and then repeatedly waits for messages from the broker.  It
will continuously attempt to connect to the broker until it succeeds.
This way, if the broker is unavailable when the application starts, it
will eventually connect when it becomes available.  Once the initial
connection is established, the client will automatically reconnect if it
gets disconnected later.

Since the `run` method loops forever and never returns, we need to use a
separate Tokio task to manage it.  We keep the task handle so we can
cancel the task when the application shuts down.
2022-12-30 13:49:01 -06:00
Dustin ce2d77a32c Implement Display and Error for all errors
These standard traits should be implemented for all error types so they
can match `dyn Error`, etc.
2022-12-30 10:05:57 -06:00
Dustin c8386f9dee marionette: Handle concurrent communication
The Marionette protocol is designed to facilitate concurrent,
asynchronous messages.  Each request message includes a message
ID, and the corresponding response includes the same message ID.  This
allows several requests to be in flight at once.  In order for this to
be useful, the client needs to maintain a record of each request it has
sent so that it knows how to handle responses, even if they arrive out
of order.

To implement this functionality in *mqttmarionette*, the
`MarionetteConnection` structure spawns a Tokio task to handle all
incoming messages from the server.  When a message arrives, its ID is
looked up in a registry that maps message IDs to Tokio "oneshot"
channels.  If a channel is found in the map, the response is sent back
to the caller through the channel.

In order to handle incoming messages in a separate task, the TCP stream
has to be split into its read and write parts.  The receiver task cannot
be spawned, though, until after the first unsolicited message is read
from the socket, since a) there is no caller to send the message back to
and b) it does not follow the same encoding scheme as the rest of the
Marionette messages.  As such, I've refactored the
`MarionetteConnection` structure to handle the initial message in the
`connect` function and dropped the `handshake` method.  A new
`start_session` method is responsible for initiating the Marionette
session.
2022-12-30 10:02:15 -06:00
Dustin f3815e2b12 Initial commit 2022-12-30 09:10:05 -06:00