Skip to content

Commit

Permalink
#187 Modified implementation to construct domain participants on the …
Browse files Browse the repository at this point in the history
…default domain (#188)

* #187 Modified implementation to construct domain participants on the default domain (as determined by the Cyclone DDS configuration) if domain id is not specified. Updated examples and tooling to use the default domain by default.

* #187 Restoring original timeout value.

* #187 Corrected type hinting in DomainParticipant constructor.
  • Loading branch information
gmartin82 authored Apr 25, 2023
1 parent 3a274df commit 3ca2d19
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 30 deletions.
1 change: 1 addition & 0 deletions clayer/pysertype.c
Original file line number Diff line number Diff line change
Expand Up @@ -2383,6 +2383,7 @@ PyMODINIT_FUNC PyInit__clayer(void) {

PyModule_AddObject(module, "DDS_INFINITY", PyLong_FromLongLong(DDS_INFINITY));
PyModule_AddObject(module, "UINT32_MAX", PyLong_FromUnsignedLong(UINT32_MAX));
PyModule_AddObject(module, "DDS_DOMAIN_DEFAULT", PyLong_FromUnsignedLong(DDS_DOMAIN_DEFAULT));
#ifdef DDS_HAS_TYPE_DISCOVERY
Py_INCREF(Py_True);
PyModule_AddObject(module, "HAS_TYPE_DISCOVERY", Py_True);
Expand Down
6 changes: 3 additions & 3 deletions cyclonedds/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import ctypes as ct
from typing import List, Optional

from .internal import c_call, dds_c_t
from .internal import c_call, dds_c_t, dds_domain_default
from .core import Entity, DDSException, Listener
from .topic import Topic
from .qos import _CQos, LimitedScopeQos, DomainParticipantQos, Qos
Expand Down Expand Up @@ -71,13 +71,13 @@ class DomainParticipant(Entity):
It serves as root entity for all other entities.
"""

def __init__(self, domain_id: int = 0, qos: Optional[Qos] = None,
def __init__(self, domain_id: int = dds_domain_default, qos: Optional[Qos] = None,
listener: Optional[Listener] = None):
"""Initialize a DomainParticipant.
Parameters
----------
domain_id: int, optional, default 0
domain_id: int, optional, default as per Cyclone DDS configuration
The DDS Domain to use
qos: cyclonedds.qos.Qos, optional, default None
Apply DomainParticipant Qos.
Expand Down
1 change: 1 addition & 0 deletions cyclonedds/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,4 @@ class sample_buffer(ct.Structure): # noqa N801
uint32_max: int = _clayer.UINT32_MAX
feature_type_discovery = _clayer.HAS_TYPE_DISCOVERY
feature_topic_discovery = _clayer.HAS_TOPIC_DISCOVERY
dds_domain_default: int = _clayer.DDS_DOMAIN_DEFAULT
9 changes: 6 additions & 3 deletions cyclonedds/tools/cli/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import time
from typing import Any
from typing import Any, Optional
from cyclonedds import qos, domain, sub, topic

from ..utils import LiveData


def subscribe(
live: LiveData,
domain_id: int,
domain_id: Optional[int],
topic_name: str,
datatype: Any,
topicqos: qos.Qos,
readerqos: qos.Qos,
):
dp = domain.DomainParticipant(domain_id)
if domain_id is None:
dp = domain.DomainParticipant()
else:
dp = domain.DomainParticipant(domain_id)
tp = topic.Topic(dp, topic_name, datatype, qos=topicqos)
rd = sub.DataReader(dp, tp, qos=readerqos)

Expand Down
2 changes: 2 additions & 0 deletions cyclonedds/tools/cli/ddsperf.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@
@click.option(
"-i",
"--domain-id",
"--id",
metavar="<ID>",
type=int,
help="Use domain ID instead of the default domain",
)
@click.option(
Expand Down
23 changes: 16 additions & 7 deletions cyclonedds/tools/cli/discovery/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
import time
import re
from typing import List
from typing import List, Optional
from cyclonedds import core, domain, builtin, dynamic, util, internal
from datetime import datetime, timedelta
from collections import defaultdict
Expand All @@ -14,14 +14,17 @@


def ls_discovery(
live: LiveData, domain_id: int, runtime: timedelta, topic: str, show_qos: bool
live: LiveData, domain_id: Optional[int], runtime: timedelta, topic: str, show_qos: bool
) -> List[DParticipant]:
try:
topic_re = re.compile(f"^{topic}$")
except re.error:
topic_re = re.compile(f"^{re.escape(topic)}$")

dp = domain.DomainParticipant(domain_id)
if domain_id is None:
dp = domain.DomainParticipant()
else:
dp = domain.DomainParticipant(domain_id)

rdp = builtin.BuiltinDataReader(dp, builtin.BuiltinTopicDcpsParticipant)
rcp = core.ReadCondition(
Expand Down Expand Up @@ -153,14 +156,17 @@ def ls_discovery(


def ps_discovery(
live: LiveData, domain_id: int, runtime: timedelta, show_self: bool, topic: str
live: LiveData, domain_id: Optional[int], runtime: timedelta, show_self: bool, topic: str
) -> List[PApplication]:
try:
topic_re = re.compile(f"^{topic}$")
except re.error:
topic_re = re.compile(f"^{re.escape(topic)}$")

dp = domain.DomainParticipant(domain_id)
if domain_id is None:
dp = domain.DomainParticipant()
else:
dp = domain.DomainParticipant(domain_id)

rdp = builtin.BuiltinDataReader(dp, builtin.BuiltinTopicDcpsParticipant)
rcp = core.ReadCondition(
Expand Down Expand Up @@ -274,9 +280,12 @@ def ps_discovery(


def type_discovery(
live: LiveData, domain_id: int, runtime: timedelta, topic: str
live: LiveData, domain_id: Optional[int], runtime: timedelta, topic: str
) -> List[PApplication]:
dp = domain.DomainParticipant(domain_id)
if domain_id is None:
dp = domain.DomainParticipant()
else:
dp = domain.DomainParticipant(domain_id)

rdw = builtin.BuiltinDataReader(dp, builtin.BuiltinTopicDcpsPublication)
rcw = core.ReadCondition(
Expand Down
2 changes: 1 addition & 1 deletion cyclonedds/tools/cli/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@click.command(short_help="Scan and display DDS entities in your network")
@click.option(
"-i", "--id", "--domain-id", type=int, default=0, help="DDS Domain to inspect."
"-i", "--id", "--domain-id", type=int, help="DDS Domain to inspect."
)
@click.option(
"-r",
Expand Down
2 changes: 1 addition & 1 deletion cyclonedds/tools/cli/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@click.command(short_help="Scan and display DDS applications in your network")
@click.option(
"-i", "--id", "--domain-id", type=int, default=0, help="DDS Domain to inspect."
"-i", "--id", "--domain-id", type=int, help="DDS Domain to inspect."
)
@click.option(
"-r",
Expand Down
7 changes: 5 additions & 2 deletions cyclonedds/tools/cli/pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@click.command(short_help="Publish to an arbitrary topic")
@click.argument("topic")
@click.option(
"-i", "--id", "--domain-id", type=int, default=0, help="DDS Domain to inspect."
"-i", "--id", "--domain-id", type=int, help="DDS Domain to inspect."
)
@click.option(
"-r",
Expand Down Expand Up @@ -107,7 +107,10 @@ def publish(topic, id, runtime, suppress_progress_bar, color, qos, type):
)
console.print("[bold green] Publishing, run exit() to quit")

dp = domain.DomainParticipant(id)
if id is None:
dp = domain.DomainParticipant()
else:
dp = domain.DomainParticipant(id)
tp = cyclone_topic.Topic(dp, topic, discovered_type.dtype, qos=qos_topic)
dw = pub.DataWriter(dp, tp, qos=qos_endpoint)

Expand Down
2 changes: 1 addition & 1 deletion cyclonedds/tools/cli/sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@click.command(short_help="Subscribe to an arbitrary topic")
@click.argument("topic")
@click.option(
"-i", "--id", "--domain-id", type=int, default=0, help="DDS Domain to inspect."
"-i", "--id", "--domain-id", type=int, help="DDS Domain to inspect."
)
@click.option(
"-r",
Expand Down
2 changes: 1 addition & 1 deletion cyclonedds/tools/cli/typeof.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@click.command(short_help="Fetch and display reconstructed IDL of a type over XTypes")
@click.argument("topic")
@click.option(
"-i", "--id", "--domain-id", type=int, default=0, help="DDS Domain to inspect."
"-i", "--id", "--domain-id", type=int, help="DDS Domain to inspect."
)
@click.option(
"-r",
Expand Down
7 changes: 5 additions & 2 deletions cyclonedds/tools/ddsls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def parse_args(args):

def create_parser(args):
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--id", type=int, help="Define the domain participant id", default=0)
parser.add_argument("-i", "--id", type=int, help="Define the domain participant id")
parser.add_argument("-f", "--filename", type=str, help="Write results to file in JSON format")
parser.add_argument("-j", "--json", action="store_true", help="Print output in JSON format")
parser.add_argument("-w", "--watch", action="store_true", help="Watch for data reader & writer & qoses changes")
Expand Down Expand Up @@ -207,7 +207,10 @@ def main(sys_args):
JsonWriter.reset()
managers = []
args = create_parser(sys_args)
dp = DomainParticipant(args.id)
if args.id is None:
dp = DomainParticipant()
else:
dp = DomainParticipant(args.id)
topics = parse_args(args)
waitset = WaitSet(dp)

Expand Down
7 changes: 6 additions & 1 deletion cyclonedds/tools/pubsub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def create_parser(args):
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-T", "--topic", type=str, help="The name of the topic to publish/subscribe to")
group.add_argument("-D", "--dynamic", type=str, help="Dynamically publish/subscribe to a topic")
parser.add_argument("-i", "--id", type=int, help="Define the domain participant id")
parser.add_argument("-f", "--filename", type=str, help="Write results to file in JSON format")
parser.add_argument("-eqos", "--entityqos", choices=["all", "topic", "publisher", "subscriber",
"datawriter", "datareader"], default=None, help="""Select the entites to set the qos.
Expand Down Expand Up @@ -167,7 +168,11 @@ def main(sys_args):
qos = QosParser.parse(args.qos)
eqos.entity_qos(qos, args.entityqos)

dp = DomainParticipant(0)
if args.id is None:
dp = DomainParticipant()
else:
dp = DomainParticipant(args.id)

waitset = WaitSet(dp)
manager = TopicManager(args, dp, eqos, waitset)
if args.topic or args.dynamic:
Expand Down
2 changes: 1 addition & 1 deletion docs/manual/tools.ddsls.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Comprehend output
Domain participant id
^^^^^^^^^^^^^^^^^^^^^

By default, the **ddsls** subscribes to the default domain (domain 0) and displays information of entities in that domain. However, if you want to view the entity information in another domain, you can use the option ``-- id`` to change the domain to which the **ddsls** subscribes.
By default, the **ddsls** subscribes to the default domain (domain 0 unless otherwise configured) and displays information of entities in that domain. However, if you want to view the entity information in another domain, you can use the option ``-- id`` to change the domain to which the **ddsls** subscribes.

The ``--id`` option will set the id of the **ddsls** domain participant, allowing the **ddsls** to view entities in the domain you chooses.

Expand Down
4 changes: 2 additions & 2 deletions examples/async/async_subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ def on_liveliness_changed(self, reader, status):
qos = Qos(
Policy.Reliability.BestEffort,
Policy.Deadline(duration(microseconds=10)),
Policy.Durability.Transient,
Policy.Durability.TransientLocal,
Policy.History.KeepLast(10)
)

domain_participant = DomainParticipant(0)
domain_participant = DomainParticipant()
topic = Topic(domain_participant, 'Vehicle', Vehicle, qos=qos)
subscriber = Subscriber(domain_participant)
reader = DataReader(domain_participant, topic, listener=listener)
Expand Down
4 changes: 3 additions & 1 deletion examples/async/vehicles.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
"""

from dataclasses import dataclass

from cyclonedds.idl import IdlStruct
from cyclonedds.idl.types import int64


@dataclass
class Vehicle(IdlStruct):
name: str
x: int64
Expand Down
2 changes: 1 addition & 1 deletion examples/builtin_topics/participants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from cyclonedds.util import duration


# Create a DomainParticipant in domain 0
# Create a DomainParticipant in the default domain
dp = DomainParticipant()

# Create a datareader that can read from the builtin participant topic
Expand Down
2 changes: 1 addition & 1 deletion examples/helloworld/helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HelloWorld(IdlStruct, typename="HelloWorld.Msg"):


# Create a DomainParticipant, your entrypoint to DDS
# The default domain id is 0.
# Created in the default domain
dp = DomainParticipant()

# Create a Topic with topic name "Hello" and as datatype "HelloWorld" structs.
Expand Down
2 changes: 1 addition & 1 deletion examples/vehicle/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
Policy.History.KeepLast(10)
)

domain_participant = DomainParticipant(0)
domain_participant = DomainParticipant()
topic = Topic(domain_participant, 'Vehicle', Vehicle, qos=qos)
publisher = Publisher(domain_participant)
writer = DataWriter(publisher, topic)
Expand Down
2 changes: 1 addition & 1 deletion examples/vehicle/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def on_liveliness_changed(self, reader, status):
Policy.History.KeepLast(10)
)

domain_participant = DomainParticipant(0)
domain_participant = DomainParticipant()
topic = Topic(domain_participant, 'Vehicle', Vehicle, qos=qos)
subscriber = Subscriber(domain_participant)
reader = DataReader(domain_participant, topic, listener=listener)
Expand Down

0 comments on commit 3ca2d19

Please sign in to comment.