Skip to content

Default plugins

Paolo Bosetti edited this page Jul 16, 2024 · 1 revision

In brief

Starting with version 0.9.0, mads-source, mads-filter, and mads-sink have default plugins, i.e. if you call these commands without a valid plugin name, a default plugin will be loaded providing basic functionality. In perspective, these default plugins will render some commands as mads-feedback and mads-bridge obsolete (and will thus be removed).

Calling

Call any of the three commands omitting the plugin argument, for example:

mads sink

Details

mads sink

The default plugin for this command is installed in lib/feedback.plugin. As the name suggests, it acts as the mads-feedback command, and uses the same section in the mads.ini file. This plugin prints on the standard output any message received from any topic to which this agent is subscribed. The print format is:

<topic>: <json dump>

mads filter

The default plugin for this command is installed in lib/bridge.plugin. As the name suggests, it acts as the mads-bridge command, and uses the same section in the mads.ini file. This plugin is designed to provide an easy interface with scripting languages.

A given scripting language only needs to open a pipe to this command and then:

  1. read a newline terminated string from its standard output
  2. parse that string as json
  3. elaborate this input json object
  4. create a new output json object
  5. write a string representation of the output json (UTF-8, new-line terminated) to the process standard input

As an example, in Python3 you would do something similar:

#!/usr/bin/env python3
import subprocess
import json

command = "./products/bin/mads-filter"
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, stdin=subprocess.PIPE, shell=True)

while True:
  # read input from the agent
  json_input = json.loads(process.stdout.readline())
  # change the input somehow
  json_input["reply"] = True
  # Dump the json as string
  json_str = json.dumps(json_input, separators=(',', ':'))
  print(json_str)
  # write the json to the agent stdin (converting it to bytes and appending \n)
  process.stdin.write(json_str.encode('UTF-8') + b'\n')
  process.stdin.flush()

In this way, any filter that does not need the speed and efficiency of C++ can be implemented as a script.

mads source

The default plugin for this command is installed in lib/publish.plugin. As the name suggests, it allows to publish JSON objects on the network, reading from standard input. It can serve as a quick debugging/prototyping tool or as an interface to scripting languages.

When you start it as mads source, it waits for input (on stdin). The input must be a valid JSON string, new-line terminated, i.e. there must be no new-lines within the JSON string! For example, the string:

{"a": [1,2,3]}

is valid, while the string:

{
  "a": [
    1,
    2,
    3
  ]
}

is not valid.

This can be used interactively on the console (launch mads source, then type a JSON string on the console and terminate it with return) or programmatically from a scripting language by opening a pipe to the process, as outlined in the mads filter section above. In this case, of course, the process pipe only has access to the process stdin (and the process does not provide any stdout).

Implementation

The default plugins are implemented in the src/plugin directory; these are compiled as .plugin shared objects and installed in the lib installation folder.

Clone this wiki locally