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_MESSAGE 555

// 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_MESSAGE = 555

# 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()

# 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

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.1.0.tar.gz (17.2 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.1.0-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyrtma-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b3448657b446b777ed5c5b1e61a79e7faf2dbee6a4cdfeb788cf707d00edf355
MD5 7f90ab6f8f4248793e712c84a6d4f410
BLAKE2b-256 3c5dac42bc9d15b9cdd8f09a82231fccb21298024f91bfbef7ac6b2fa55aef59

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pyrtma-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a09f7ef5daaad9d07c3f451e853c2a8f31c6b4126c8a7d55e002af416a5a184
MD5 cdc2331824b446c503bf953f935b56d1
BLAKE2b-256 31b377bce27b22a295cfd3d51f92a92c46056bf891c2b72bace9add01e267d5b

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