Skip to main content

A Python client for RTMA/Dragonfly

Project description

pyrtma Python package

RTMA/Dragonfly client written in python with no external dependencies. Based on and compatible with Dragonfly Messaging

Installation

pyrtma is available on PyPI

$ pip install pyrtma

Usage

Launch Manager

$ python -m pyrtma.manager -a "127.0.0.1"

Create a message in message.h

The recommended way of creating messages is to define them in a C header file (.h file).

// message.h

// Module IDs
#define MID_PERSON_PUBLISHER 112
#define MID_PERSON_SUBSCRIBER 113

// Message IDs
#define MT_PERSON_MESSAGE 1234
#define MT_ANOTHER_EXAMPLE 5678

// We can define other constants as well
#define STR_SIZE 32 


// Message definitions
typedef struct {
	char name[STR_SIZE];
	int age;
} MDF_PERSON_MESSAGE;

typedef struct {
	char value_str[STR_SIZE];
	int value_int;
    float value_float;
    double value_double
} MDF_ANOTHER_EXAMPLE;

Run the following command to compile the header file into Python types and files. This will output a message.py file.

$ python -m pyrtma.compile -i message.h -o message.py
# message.py
import ctypes
import pyrtma
from pyrtma.constants import *

# User Constants: message.h
STR_SIZE = 32

# User Message IDs: message.h
MT_PERSON_MESSAGE = 1234
MT_ANOTHER_EXAMPLE = 5678

# User Module IDs: message.h
MID_PERSON_PUBLISHER = 112
MID_PERSON_SUBSCRIBER = 113

# User Type Definitions: message.h
# User Message Definitions: message.h


@pyrtma.msg_def
class MDF_PERSON_MESSAGE(pyrtma.MessageData):
    _pack_ = True
    _fields_ = [
        ("name", ctypes.c_char * STR_SIZE),
        ("age", ctypes.c_long)
    ]
    type_id = MT_PERSON_MESSAGE
    type_name = "PERSON_MESSAGE"



@pyrtma.msg_def
class MDF_ANOTHER_EXAMPLE(pyrtma.MessageData):
    _pack_ = True
    _fields_ = [
        ("value_str", ctypes.c_char * STR_SIZE),
        ("value_int", ctypes.c_long),
        ("value_float", ctypes.c_float),
        ("value_double", ctypes.c_double)
    ]
    type_id = MT_ANOTHER_EXAMPLE
    type_name = "ANOTHER_EXAMPLE"

Note: Messages can also be defined in a Python file without the compile step

Create a publisher module in publisher.py

# publisher.py
import message
import pyrtma
import time

mod = pyrtma.Client(module_id=message.MID_PERSON_PUBLISHER)

# Connect to the manager
mod.connect(server_name="127.0.0.1:7111")

# Create an instance of the person message and populate with data
msg = message.MDF_PERSON_MESSAGE()
msg.name = "Alice"  # str gets converted to c_char
msg.age = 42

# Send the person message every second
while True:
    print("Sending message")
    mod.send_message(msg)
    time.sleep(1)

Create a subscriber module in subscriber.py

# subscriber.py
import message
import pyrtma

# Keeping module_id blank makes Manager dynamically create a module id.
# This is default behavior.
mod = pyrtma.Client()

# Connect to the manager
mod.connect(server_name="127.0.0.1:7111")

# Subscribe to the person message and pyrtma's exit message (when manager is closed)
mod.subscribe([message.MT_PERSON_MESSAGE, pyrtma.MT_EXIT])

while True:
    try:
        # Read a message. We can specify a time. -1 means it will block until
        # a message is received
        msg = mod.read_message(timeout=-1)
        if msg is not None:

            # Find out what kind of message we received
            # We can check the message id
            if msg.type_id == message.MDF_PERSON_MESSAGE.type_id:
                name = msg.data.name  # c_char get converted to str
                age = msg.data.age
                print(f"Hello my name is {name} and I am {age} years old")

            # Or we can check the message name
            elif msg.name == "EXIT":
                print("Goodbye.")
                break

    except KeyboardInterrupt:
        break

Launch the publisher

$ python publisher.py

Launch the subscriber

You should see the message 'Hello my name is Alice and I am 42 years old' print in your shell

$ python subscriber.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrtma-1.2.1.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyrtma-1.2.1-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file pyrtma-1.2.1.tar.gz.

File metadata

  • Download URL: pyrtma-1.2.1.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for pyrtma-1.2.1.tar.gz
Algorithm Hash digest
SHA256 263850036b6dff836c646a33d426341a27902b7de04890c966d53493e3348d65
MD5 ec62925822986ff9d99bd5671230a9c5
BLAKE2b-256 6b4041d0fa3529afeef8e897bc1660bea2d9dccf8f46ad3f48bce21cde26593f

See more details on using hashes here.

File details

Details for the file pyrtma-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: pyrtma-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for pyrtma-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 076eb5fc3ac03497a8fc505eed6fb73e1cec2b578bc8a22acfec48d80cbd9834
MD5 3d5b4f6d666a2df597fb4880d29ad2b8
BLAKE2b-256 37278e6bee4db33d6926ae8001978d865af80e682280f3af9c2b163cad8d98a7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page