Skip to content
Merged
17 changes: 15 additions & 2 deletions python/its-interqueuemanager/its-iqm.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ endpoint = http://opentelemetry.example.com:1234
#password = secret

[local]
# Path to the UNIX socket to connect to the broker
socket-path = /run/mosquitto/mqtt.socket
# There are two ways to connect to the local broker, and either,
# not both, is required:
# - establish a TCP/IP connection to host:port
# - establish a TCP-like connection over the UNIX socket socket-path
# Hostname or IP of the local broker
host = 127.0.0.1
# TCP port the local broker listens on (non-TLS); default: 1883
port = 1883
# Whether to connect using TLS over TCP; default: use TLS, unless port == 1883
# tls = True|False
# Path of the websocket; if unset, do not use WebSockets; default: unset
#websocket_path = /mqtt
# Path to the UNIX socket to connect to; if specified, host, port, tls, and
# websocket_path are ignored
#socket-path = /run/mqtt-broker/socket
# Username and password to authenticate with against the local broker,
# empty or unset username for no authentication; default: unset
#username = user
Expand Down
3 changes: 2 additions & 1 deletion python/its-interqueuemanager/src/its_iqm/authority/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import logging
import iot3.core.mqtt
from its_iqm.helpers import str2bool


class Authority:
Expand All @@ -29,7 +30,7 @@ def __init__(
host=self.cfg["host"],
port=int(self.cfg["port"]),
websocket_path=self.cfg.get("websocket_path"),
tls=self.cfg.get("tls"),
tls=str2bool(self.cfg.get("tls")),
username=self.cfg.get("username"),
password=self.cfg.get("password"),
msg_cb=self.msg_cb,
Expand Down
31 changes: 31 additions & 0 deletions python/its-interqueuemanager/src/its_iqm/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Software Name: its-interqueuemanager
# SPDX-FileCopyrightText: Copyright (c) 2023 Orange
# SPDX-License-Identifier: MIT
# Author: Yann E. MORIN <yann.morin@orange.com>

from configparser import ConfigParser
from typing import Any, Optional


def str2bool(
value: Optional[str],
*,
fallback: Any = None,
):
"""Convert a string to a boolean

If value is None, default is returned. Otherwise, the boolean
corresponding to value is returned; value can be any of the
boolean values define by the module configparser, e.g. "False",
"True", etc…

The signature is similar to that of configparser.ConfigParser.get(),
with fallback a keyword-only.
"""
if value is None:
return fallback

try:
return ConfigParser.BOOLEAN_STATES[value]
except KeyError:
raise ValueError(f"Expected boolean value, got '{value}'")
14 changes: 11 additions & 3 deletions python/its-interqueuemanager/src/its_iqm/iqm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
# Author: Yann E. MORIN <yann.morin@orange.com>

from __future__ import annotations
import configparser
import iot3.core.mqtt
import iot3.core.otel
import logging
import random
import time
from . import authority
from . import filters
from its_iqm.helpers import str2bool


class IQM:
def __init__(
self: IQM,
cfg: configparser.ConfigParser,
cfg: dict,
):
logging.info("create")
self.cfg = cfg
Expand Down Expand Up @@ -91,6 +91,14 @@ def __init__(
self.filters[new_filter.type].append(new_filter)

logging.info("create local qm")
conn = dict()
try:
conn["socket_path"] = cfg["local"]["socket-path"]
except KeyError:
conn["host"] = cfg["local"]["host"]
conn["port"] = int(cfg["local"]["port"])
conn["tls"] = str2bool(cfg["local"].get("tls"))
conn["websocket_path"] = cfg["local"].get("websocket_path")

qm_data = {
"copy_qm": None,
Expand All @@ -101,9 +109,9 @@ def __init__(

self.local_qm = iot3.core.mqtt.MqttClient(
client_id=cfg["local"]["client_id"],
socket_path=cfg["local"]["socket-path"],
username=cfg["local"]["username"],
password=cfg["local"]["password"],
**conn,
msg_cb=self.qm_copy_cb,
msg_cb_data=qm_data,
span_ctxmgr_cb=self.span_cb,
Expand Down
3 changes: 2 additions & 1 deletion python/its-interqueuemanager/src/its_iqm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"password": None,
},
"local": {
"port": 1883,
"username": None,
"password": None,
"client_id": "iqm",
Expand Down Expand Up @@ -68,7 +69,7 @@ def main():
# configparser.ConfigParser() only accepts strings as values, but we
# need None for some defaults, so make it a true dict() of dicts()s,
# which is easier to work with.
cfg = {s: {k: cfg[s][k] for k in cfg[s]} for s in cfg if s != "DEFAULT"}
cfg = {s: {k: cfg[s][k] for k in cfg[s]} for s in cfg.sections()}

def _set_default(section, key, default):
if section not in cfg:
Expand Down
Loading