|
1 | 1 | import time |
2 | 2 | from typing import Any, Callable |
| 3 | +import threading |
3 | 4 | from threading import Thread, Event |
4 | 5 |
|
5 | 6 | from cyclonedds.domain import Domain, DomainParticipant |
@@ -192,41 +193,51 @@ class ChannelFactory(Singleton): |
192 | 193 | __participant = None |
193 | 194 | __qos = None |
194 | 195 |
|
| 196 | + __initialized = False |
| 197 | + __init_lock = threading.Lock() |
| 198 | + |
195 | 199 | def __init__(self): |
196 | 200 | super().__init__() |
197 | 201 |
|
198 | 202 | def Init(self, id: int, networkInterface: str = None, qos: Qos = None): |
199 | | - config = None |
200 | | - # choose config |
201 | | - if networkInterface is None: |
202 | | - config = ChannelConfigAutoDetermine |
203 | | - else: |
204 | | - config = ChannelConfigHasInterface.replace('$__IF_NAME__$', networkInterface) |
205 | | - |
206 | | - try: |
207 | | - self.__domain = Domain(id, config) |
208 | | - except DDSException as e: |
209 | | - print("[ChannelFactory] create domain error. msg:", e.msg) |
210 | | - return False |
211 | | - except: |
212 | | - print("[ChannelFactory] create domain error.") |
213 | | - return False |
214 | | - |
215 | | - try: |
216 | | - self.__participant = DomainParticipant(id) |
217 | | - except DDSException as e: |
218 | | - print("[ChannelFactory] create domain participant error. msg:", e.msg) |
219 | | - return False |
220 | | - except: |
221 | | - print("[ChannelFactory] create domain participant error") |
222 | | - return False |
223 | | - |
224 | | - self.__qos = qos |
225 | | - |
226 | | - return True |
| 203 | + if self.__class__.__initialized: |
| 204 | + return True |
| 205 | + |
| 206 | + with self.__class__.__init_lock: |
| 207 | + if self.__class__.__initialized: |
| 208 | + return True |
| 209 | + |
| 210 | + config = None |
| 211 | + # choose config |
| 212 | + if networkInterface is None: |
| 213 | + config = ChannelConfigAutoDetermine |
| 214 | + else: |
| 215 | + config = ChannelConfigHasInterface.replace('$__IF_NAME__$', networkInterface) |
| 216 | + |
| 217 | + try: |
| 218 | + self.__class__.__domain = Domain(id, config) |
| 219 | + except DDSException as e: |
| 220 | + print("[ChannelFactory] create domain error. msg:", e.msg) |
| 221 | + return False |
| 222 | + except: |
| 223 | + print("[ChannelFactory] create domain error.") |
| 224 | + return False |
| 225 | + |
| 226 | + try: |
| 227 | + self.__class__.__participant = DomainParticipant(id) |
| 228 | + except DDSException as e: |
| 229 | + print("[ChannelFactory] create domain participant error. msg:", e.msg) |
| 230 | + return False |
| 231 | + except: |
| 232 | + print("[ChannelFactory] create domain participant error") |
| 233 | + return False |
| 234 | + |
| 235 | + self.__class__.__qos = qos |
| 236 | + self.__class__.__initialized = True |
| 237 | + return True |
227 | 238 |
|
228 | 239 | def CreateChannel(self, name: str, type: Any): |
229 | | - return Channel(self.__participant, name, type, self.__qos) |
| 240 | + return Channel(self.__class__.__participant, name, type, self.__class__.__qos) |
230 | 241 |
|
231 | 242 | def CreateSendChannel(self, name: str, type: Any): |
232 | 243 | channel = self.CreateChannel(name, type) |
|
0 commit comments