Discovering network appliances
Tuesday, March 28 2017 01:12 GMT
Posted by: MembraneTags:programmingraspberry pi
A toaster is an appliance: it does one job well and is easily replaced if broken. A PC is not an appliance: it does many jobs, often not well, and is for most a nightmare to replace. But what if we could divide all of a PC's jobs among a set of reliable networked appliances?
The word appliance might conjure images of washing, blending, or garbage disposing gizmos, but in the area of computing we can use the concept to describe a device with the best quality of any appliance in your kitchen: it has one job to do, and does it perfectly every time with the absolute minimum help from you. At Membrane HQ, we have dozens of computing devices performing a wide array of tasks, and treat as many of them as possible like appliances. Raspberry Pi boards, in particular, are extremely useful for this type of work because they're easily programmable and can connect to other devices via Ethernet, module boards, or custom soldering. Armed with a board that can be programmed with arbitrary logic and then put in control of an arbitrary slave device, it's possible to create an appliance for absolutely any task if we put our minds to it. And what with Raspberry Pi boards being so inexpensive, we gain even more desirable appliance-like attributes: we can easily replace one that breaks, and we can get more of them if we have too much work for only one to handle.
A system of agents
Each appliance in our system runs an agent: a software process responsible for controlling its functions. An agent has certain important responsibilities to fulfill if its host appliance is to be a contributing member to the system.
- The agent must be able control our desired functions on its host device. Usually, this means someone with access to the device has installed and configured the agent software. For example, an agent in charge of streaming video from a set of media files would need to be configured with access to a directory containing those files.
- Since we'd like to direct our agents remotely, each agent must have a way to accept commands over the network. For easier setup of appliances, we'd also like each agent to make itself automatically discoverable on the network.
- When we're not able to control an agent due to a network disruption, the agent must be autonomous enough to continue doing its task until communication re-establishes.
Taken together, our set of expectations adds up to what we want from an appliance: able to do its job autonomously as long as it's powered up, while also accepting commands over the network. It requires competent programming effort to create reliable agent software for each appliance, but if done correctly the result is worth it. By building a network of autonomous agents, each with its own capabilities and controlled from a master console, we enable untold numbers of interesting applications.
Membrane agents are set up to listen on the network for commands. One of those commands is ReportStatus, a simple request for the receiver to report status information to an included URL. When an agent wishes to find others on the network, it broadcasts this command and all receivers report back.
Sending a broadcast message to discover agents. Any agent on the same network receives the message and responds with status data, including its list of capabilities.
An agent's status report includes a list of its supported capabilities, each of which indicates the presence of a related set of commands. For example, an agent with an attached camera might report that it has the CameraControl capability, meaning it accepts commands such as CaptureImage and CaptureVideo. Another agent with an attached display might possess the DisplayControl capability, meaning it accepts commands such as ShowImage and PlayVideo. Yet a third agent might have access to a camera as well as a display, and therefore accept both sets of commands.
Of Pi's and PC's
We've spent the article so far imagining our ideal system of connected appliances: a set of agent processes, each making itself available on the network while being able to control whatever interesting devices are present on its hardware. Talking about ideals is fun, but to bring our vision into reality we need to get specific about ways and means.
The first task we'll ask of our system is to grant us control over set of displays. We'd like to send a display commands that cause it to play video from a media server. Once simple playback commands are in place, more complex functions such as loops and shuffles can be built on that foundation.
Each display in the system is an ordinary computer monitor. These seem to be getting less and less expensive every time I look; right now I'm seeing one site that has 24-inch LED backlight monitors on sale for $109.99.
We use a Raspberry Pi device to drive the display on each monitor via an HDMI connection. To program our agent process on the Raspberry Pi, we've selected a stack of free software.
- Raspbian: As the official Linux distribution for the Raspberry Pi, Raspbian serves as our base operating system. Linux has a good track record for stability, and also supports just about any platform we'd want to use for programming agents.
- Node.js: A runtime environment that grants a programmer control over many system functions, including ones important to us such as networking and launching child processes. Programming in Node.js uses an asynchronous, event-driven model that we find ideal for a system of agents.
- Omxplayer: A video player application that runs on Raspbian. Omxplayer supports streaming media playback via RTMP, RTSP, and HTTP Live Streaming, thereby letting us play videos from a media server instead of requiring local storage on each Raspberry Pi device.
We want to control the Raspberry Pi displays using a graphical interface. To do so, we've chosen to implement an agent on the PC, again making use of a free software stack.
- Simple DirectMedia Layer: SDL provides our application with a window and rendering routines as well as other core functions, and supports the major desktop operating systems: Windows, macOS, and Linux. SDL claims Android and iOS support as well, which should come in handy when the time comes for mobile apps.
- FreeType: Applications need text, but rendering fonts can be a finicky business. The FreeType library helps us by parsing TTF (True Type Font) files and providing the resulting bitmaps. As for the fonts themselves, Google Fonts offers quite a variety of fonts for general use.
- FFmpeg: Our display control functions involve streaming audio and video, and FFmpeg has long been the state of the art for media processing. In this system, it helps by preparing video files for streaming, pulling thumbnails from videos, decoding and re-encoding media data, and more.
Membrane Control: a work in progress
The node application for our Raspberry Pi agent already has much of its software in place. In demo videos, we show how it's possible to control video playback and camera capture from a tablet application.
Membrane Control, our PC agent application, is in development. A few thoughts about its design were briefly described in a previous blog post. At that time, we had Membrane Control set up with an "Address" bar and a "Connect" button, expecting the user to enter the name of an agent to control. However, we have now moved to the discovery scheme outlined above, removing the need for manual entry of names.
If we start Membrane Control with no Raspberry Pi agents, it shows an empty node map.
After starting two agents, Membrane Control automatically discovers them. In this case, we see that it shows one agent with both media server and display control capability, and another one with just media server capability. To move toward our goal of easy control over video playback, the next thing this interface needs is a way to browse items from those media servers. From there, we can look for available display control agents and let the user send them commands for playback.
- Software agent (Wikipedia)