-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdds_capture.py
More file actions
197 lines (145 loc) · 7.06 KB
/
dds_capture.py
File metadata and controls
197 lines (145 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import rti.connextdds as dds
from rti.connextdds import PublicationBuiltinTopicData, SubscriptionBuiltinTopicData
import threading
import time
import csv
# Global map to store types
entities = {}
participants = {}
edges = {}
class Entity:
def __init__(self, id=None, topic_name=None, type_name=None, kind=None, p_ip=None, p_name=None, p_guid=None):
self.id = id,
self.topic_name = topic_name
self.type_name = type_name
self.kind = kind
self.p_ip = p_ip
self.p_name = p_name
self.p_guid = p_guid
class Participant:
def __init__(self, name=None, ip=None):
self.name = name
self.ip = ip
class Edge:
def __init__(self, source=None, target=None, topic_name=None):
self.source = source
self.target = target
self.topic_name = topic_name
# Listener for publication discovery
class PublicationListener(dds.PublicationBuiltinTopicData.DataReaderListener):
def __init__(self):
super(PublicationListener, self).__init__()
def on_data_available(self, reader):
for data, info in reader.take():
if info.valid:
# print(f"Discovered Writer {data.topic_name}")
key_list = data.key.value
key_int = int(''.join(map(str, key_list)))
# print(f"Writer Key: {key_int}")
type_name = data.type_name
topic_name = data.topic_name
p_guid_list = data.participant_key.value
p_guid_int = int(''.join(map(str, p_guid_list)))
writer = Entity(topic_name=topic_name,type_name=type_name, kind="Writer", p_guid=p_guid_int)
if key_int not in entities:
print(f"Adding Writer to list: {writer.topic_name}")
entities[key_int] = writer
# Listener for subscription discovery
class SubscriptionListener(dds.SubscriptionBuiltinTopicData.DataReaderListener):
def on_data_available(self, reader):
for data, info in reader.take():
if info.valid:
# print(f"Discovered Reader {data.topic_name}")
key_list = data.key.value
key_int = int(''.join(map(str, key_list)))
# print(f"Reader Key: {key_int}")
type_name = data.type_name
topic_name = data.topic_name
p_guid_list = data.participant_key.value
p_guid_int = int(''.join(map(str, p_guid_list)))
reader = Entity(topic_name=topic_name,
type_name=type_name, kind="Reader", p_guid=p_guid_int)
if key_int not in entities:
print(f"Adding Reader to list: {reader.topic_name}")
entities[key_int] = reader
def main():
# Create participant in disabled state
participant_factory_qos = dds.DomainParticipantFactoryQos()
participant_factory_qos.entity_factory.autoenable_created_entities = False
dds.DomainParticipant.participant_factory_qos = participant_factory_qos
participant = dds.DomainParticipant(domain_id=1)
# Set listeners for the built-in DataReaders
participant.publication_reader.set_listener(PublicationListener(), dds.StatusMask.DATA_AVAILABLE)
participant.subscription_reader.set_listener(SubscriptionListener(), dds.StatusMask.DATA_AVAILABLE)
# Enable participant
participant.enable()
print("Participant Enabled, listening for entities")
# Keep the application running
try:
while True:
# Get current participants
p_list = participant.discovered_participants()
for p in p_list:
data = participant.discovered_participant_data(p)
name = data.participant_name.name
ip_list = data.default_unicast_locators[0].address[-4:]
ip = '.'.join(str(byte) for byte in ip_list)
participant_info = Participant(name, ip)
key_list = data.key.value
key_int = int(''.join(map(str, key_list)))
# print(f"Participant Key: {key_int}")
participants[key_int] = participant_info
# print(f'Participant Name: {data.participant_name.name}')
# Downselect Dict for Edge matching
readers = {k: v for k, v in entities.items() if v.kind == "Reader"}
writers = {k: v for k, v in entities.items() if v.kind == "Writer"}
for w in writers:
# Update writers with Participant info
if writers[w].p_guid in participants:
entities[w].p_ip = participants[writers[w].p_guid].ip
entities[w].p_name = participants[writers[w].p_guid].name
# Get Edges
for r in readers:
if readers[r].topic_name == writers[w].topic_name and readers[r].type_name == writers[w].type_name:
edge_key = (w, r)
# print(edge_key)
new_edge = Edge(w, r, readers[r].topic_name)
edges[edge_key] = new_edge
for r in readers:
# Update readers with Participant info
if readers[r].p_guid in participants:
entities[r].p_ip = participants[readers[r].p_guid].ip
entities[r].p_name = participants[readers[r].p_guid].name
print(f'Discovered Entities Count: {len(entities)}')
print(f'Discovered Participants Count: {len(p_list)}')
for p in participants:
print(f'Name: {participants[p].name}')
print(f'IP: {participants[p].ip}')
time.sleep(2)
except KeyboardInterrupt:
# Entities
print("\nEXPORTING Entities")
en_header = ["id", "topic name", "type name", "kind", "participant ip", "participant name", "participant id"]
with open('entities.csv', 'w', newline='') as en_csvfile:
en_writer = csv.writer(en_csvfile)
en_writer.writerow(en_header)
for e in entities:
en_writer.writerow([e, entities[e].topic_name, entities[e].type_name, entities[e].kind, entities[e].p_ip, entities[e].p_name, entities[e].p_guid])
# Participants
print("\nEXPORTING Participants")
dp_header = ["id", "name", "ip"]
with open('participants.csv', 'w', newline='') as dp_csvfile:
dp_writer = csv.writer(dp_csvfile)
dp_writer.writerow(dp_header)
for p in participants:
dp_writer.writerow([p, participants[p].name, participants[p].ip])
# Edges
print("\nEXPORTING Edges")
ed_header = ["id", "from", "to", "topic"]
with open('edges.csv', 'w', newline='') as ed_csvfile:
ed_writer = csv.writer(ed_csvfile)
ed_writer.writerow(ed_header)
for x in edges:
ed_writer.writerow([x, edges[x].source, edges[x].target, edges[x].topic_name])
if __name__ == "__main__":
main()