OpenSplice C# API  v6.x
OpenSplice C# Data Distribution Service Data-Centric Publish-Subscribe API
DomainParticipantFactory.cs
Go to the documentation of this file.
1 /*
2  * Vortex OpenSplice
3  *
4  * This software and documentation are Copyright 2006 to 2021 ADLINK
5  * Technology Limited, its affiliated companies and licensors. All rights
6  * reserved.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 using System;
22 using System.Runtime.InteropServices;
23 using System.Collections.Generic;
24 using DDS.OpenSplice;
25 using DDS.OpenSplice.OS;
26 using DDS.OpenSplice.CustomMarshalers;
27 using DDS.OpenSplice.Kernel;
28 
29 namespace DDS
30 {
37  public class DomainParticipantFactory : IDomainParticipantFactory
38  {
39  //
40  // Attribute containing the singleton 'self' reference.
41  //
42  private static DomainParticipantFactory singletonSelf = null;
43  private static readonly object singleton_lock = new object();
45  private DDS.DomainParticipantQos defaultParticipantQos = new DDS.DomainParticipantQos();
46  private List<DomainParticipant> participantList = new List<DomainParticipant>();
47 
48  // Attribute containing the delegates to the individual Listener functions.
49 
64  private static DomainParticipantFactory GetInstance()
65  {
66  // GetInstance() is called very infrequently so a simple locked singleton
67  // approach is used.
68  lock(singleton_lock)
69  {
70  if (singletonSelf == null) // If singleton doesn't exist, create it.
71  {
72  if (DDS.OpenSplice.User.u.userInitialise() == V_RESULT.OK)
73  {
74  ReportStack.Start();
75  singletonSelf = new DomainParticipantFactory();
76  ReportStack.Flush(null, singletonSelf == null);
77  if (singletonSelf != null) {
78  AppDomain.CurrentDomain.ProcessExit += new EventHandler(ProcessExit);
79  }
80  }
81  }
82  return singletonSelf;
83  }
84  }
85 
100  public static DomainParticipantFactory Instance
101  {
102  get { return GetInstance(); }
103  }
104 
108  protected DomainParticipantFactory()
109  {
110  DDS.ReturnCode result;
111 
112  // Init default ParticipantFactoryQos and default ParticipantQos.
113  using (OpenSplice.CustomMarshalers.DomainParticipantQosMarshaler marshaler =
114  new OpenSplice.CustomMarshalers.DomainParticipantQosMarshaler())
115  {
116  result = marshaler.CopyIn(QosManager.defaultDomainParticipantQos);
117  if (result == ReturnCode.Ok)
118  {
119  marshaler.CopyOut(ref defaultParticipantQos);
120  }
121  }
123  myQos.EntityFactory.AutoenableCreatedEntities = true;
124  }
126 
145  DomainId domainId)
146  {
147  return CreateParticipant(domainId, defaultParticipantQos, null, 0);
148  }
149 
175  DomainId domainId,
177  StatusKind mask)
178  {
179  return CreateParticipant(domainId, defaultParticipantQos, listener, mask);
180  }
181 
204  DomainId domainId,
206  {
207  return CreateParticipant(domainId, qos, null, 0);
208  }
209 
305  DomainId domainId,
308  StatusKind mask)
309  {
310  DomainParticipant participant = null;
311  ReturnCode result;
312 
313  ReportStack.Start();
314  result = QosManager.checkQos(qos);
315  if (result == DDS.ReturnCode.Ok)
316  {
317  if (domainId != DDS.DomainId.Invalid)
318  {
319  lock(singleton_lock)
320  {
321  participant = new OpenSplice.DomainParticipant();
322  result = participant.init(domainId, qos);
323  if (result == ReturnCode.Ok)
324  {
325  result = participant.SetListener(listener, mask);
326  }
327  else
328  {
329  participant = null;
330  }
331 
332  if (result == ReturnCode.Ok)
333  {
334  participantList.Add(participant);
335  if (myQos.EntityFactory.AutoenableCreatedEntities)
336  {
337  result = participant.Enable();
338  }
339  }
340  }
341  }
342  else
343  {
344  ReportStack.Report(DDS.ReturnCode.BadParameter,
345  "DomainParticipant is using an invalid domain identifier (" + domainId + ").");
346  }
347  }
348 
349  if (result != ReturnCode.Ok && participant != null)
350  {
351  // Ignore result because we prefer the original error.
352  DeleteParticipant(participant);
353  participant = null;
354  }
355 
356  ReportStack.Flush(null, result != ReturnCode.Ok);
357  return participant;
358  }
359 
378  {
379  ReturnCode result = DDS.ReturnCode.Ok;
380 
381  ReportStack.Start();
382  DomainParticipant participant = a_participant as DomainParticipant;
383  if (participant != null)
384  {
385  lock(singleton_lock)
386  {
387  if (participantList.Remove(participant))
388  {
389  result = participant.wlReq_deinit();
390  if (result != DDS.ReturnCode.Ok)
391  {
392  participantList.Add(participant);
393  }
394  }
395  else
396  {
397  /* Since there is only one DomainParticipantFactory, the participant
398  * has to be created by this one. If we can't find it anymore, it
399  * must have beel deleted already.
400  */
401  result = DDS.ReturnCode.BadParameter;
402  ReportStack.Report(result, "DomainParticipant was already deleted.");
403  }
404  }
405  }
406  else
407  {
408  result = ReturnCode.BadParameter;
409  ReportStack.Report(result, "Participant parameter 'a_participant' is null.");
410  }
411 
412  ReportStack.Flush(null, result != DDS.ReturnCode.Ok);
413  return result;
414  }
415 
435  {
436  DomainParticipant participant;
437 
438  if (domainId == DDS.DomainId.Default) {
439  domainId = DDS.OpenSplice.User.u.GetDomainIdFromEnvUri();
440  }
441  lock(singleton_lock)
442  {
443  participant = participantList.Find(
444  delegate (DomainParticipant a_participant)
445  {
446  return a_participant.DomainId == domainId;
447  }
448  );
449  }
450  return participant;
451  }
452 
473  {
474  ReturnCode result;
475 
476  lock(singleton_lock)
477  {
478  ReportStack.Start();
479  result = QosManager.checkQos(qos);
480  if (result == DDS.ReturnCode.Ok) {
481  using (OpenSplice.CustomMarshalers.DomainParticipantQosMarshaler marshaler =
482  new OpenSplice.CustomMarshalers.DomainParticipantQosMarshaler())
483  {
484  result = marshaler.CopyIn(qos);
485  if (result == ReturnCode.Ok)
486  {
487  marshaler.CopyOut(ref defaultParticipantQos);
488  // Listener scheduling qospolicy is not part of User Layer, so obtain it separately from participant.
489  if (qos.ListenerScheduling.SchedulingClass == null)
490  {
491  return ReturnCode.BadParameter;
492  }
493  this.defaultParticipantQos.ListenerScheduling.SchedulingClass.Kind = qos.ListenerScheduling.SchedulingClass.Kind;
495  {
496  return ReturnCode.BadParameter;
497  }
498  this.defaultParticipantQos.ListenerScheduling.SchedulingPriorityKind.Kind = qos.ListenerScheduling.SchedulingPriorityKind.Kind;
499  this.defaultParticipantQos.ListenerScheduling.SchedulingPriority = qos.ListenerScheduling.SchedulingPriority;
500  }
501  }
502  }
503  ReportStack.Flush(null, result != ReturnCode.Ok);
504  }
505  return result;
506  }
507 
531  {
532  ReturnCode result;
533 
534  using (OpenSplice.CustomMarshalers.DomainParticipantQosMarshaler marshaler =
535  new OpenSplice.CustomMarshalers.DomainParticipantQosMarshaler())
536  {
537  lock(singleton_lock)
538  {
539  ReportStack.Start();
540  result = marshaler.CopyIn(defaultParticipantQos);
541  if (result == ReturnCode.Ok)
542  {
543  marshaler.CopyOut(ref qos);
544  // Listener scheduling qospolicy is not part of User Layer, so obtain it separately from participant.
545  if (qos.ListenerScheduling.SchedulingClass == null)
546  {
547  qos.ListenerScheduling.SchedulingClass = new SchedulingClassQosPolicy();
548  }
549  qos.ListenerScheduling.SchedulingClass.Kind = this.defaultParticipantQos.ListenerScheduling.SchedulingClass.Kind;
550  if (qos.ListenerScheduling.SchedulingPriorityKind == null)
551  {
552  qos.ListenerScheduling.SchedulingPriorityKind = new SchedulingPriorityQosPolicy();
553  }
554  qos.ListenerScheduling.SchedulingPriorityKind.Kind = this.defaultParticipantQos.ListenerScheduling.SchedulingPriorityKind.Kind;
555  qos.ListenerScheduling.SchedulingPriority = this.defaultParticipantQos.ListenerScheduling.SchedulingPriority;
556  }
557  ReportStack.Flush(null, result != ReturnCode.Ok);
558  }
559  }
560 
561  return result;
562  }
563 
581  {
582  lock(singleton_lock)
583  {
584  myQos.EntityFactory.AutoenableCreatedEntities = qos.EntityFactory.AutoenableCreatedEntities;
585  return ReturnCode.Ok;
586  }
587  }
588 
604  {
605  lock(singleton_lock)
606  {
607  if (qos == null) qos = new DomainParticipantFactoryQos();
608  if (qos.EntityFactory == null) qos.EntityFactory = new EntityFactoryQosPolicy();
609  qos.EntityFactory.AutoenableCreatedEntities = myQos.EntityFactory.AutoenableCreatedEntities;
610  return ReturnCode.Ok;
611  }
612  }
613 
641  {
642  ReturnCode result = DDS.ReturnCode.Ok;
643 
644  lock(singleton_lock)
645  {
646  foreach (DomainParticipant p in participantList) {
647  result = p.DeleteContainedEntities();
648  if (result == DDS.ReturnCode.Ok)
649  {
650  result = p.deinit();
651  }
652  if (result != DDS.ReturnCode.Ok)
653  {
654  break;
655  }
656  }
657  }
658 
659  if (result == DDS.ReturnCode.Ok)
660  {
661  participantList.Clear();
662  }
663 
664  return ReturnCode.Ok;
665  }
666 
722  public ReturnCode DetachAllDomains(bool blockOperations, bool deleteEntities)
723  {
724  uint flags = 0;
725 
726  if (blockOperations) {
727  flags |= DDS.OpenSplice.User.u.BLOCK_OPERATIONS;
728  }
729  if (deleteEntities) {
730  flags |= DDS.OpenSplice.User.u.DELETE_ENTITIES;
731  }
732 
733  return SacsSuperClass.uResultToReturnCode(DDS.OpenSplice.User.u.userDetach(flags));
734  }
735 
737  static void ProcessExit(object sender, EventArgs args)
738  {
739  try {
740  DDS.OpenSplice.User.u.userDetach(DDS.OpenSplice.User.u.DELETE_ENTITIES);
741  } catch (Exception e) {
742  Console.WriteLine(e.ToString());
743  }
744  }
746  }
747 }
ReturnCode DeleteContainedEntities()
This operation deletes all of the Entity objects that were created on the DomainParticipantFactory.
ReturnCode SetQos(DomainParticipantFactoryQos qos)
This operation replaces the existing set of QoS settings for the DomainParticipantFactory.
All the DCPS IEntity objects are attached to a IDomainParticipant. A IDomainParticipant represents th...
PlaceHolder for a domain ID.
SchedulingQosPolicy ListenerScheduling
Specifies the scheduling parameters used to create the listener thread.
static DomainParticipantFactory Instance
This property is used to get the DomainParticipantFactory singleton object.
The purpose of this class is to allow the creation and destruction of IDomainParticipant objects...
ReturnCode DetachAllDomains(bool blockOperations, bool deleteEntities)
This operation safely detaches the application from all domains it is currently participating in...
The scheduling policy which indicates if the scheduling priority is relative or absolute.
ReturnCode SetDefaultParticipantQos(DomainParticipantQos qos)
This operation sets the default DomainParticipantQos of the DomainParticipantFactory.
SchedulingClassQosPolicy SchedulingClass
ReturnCode GetDefaultParticipantQos(ref DomainParticipantQos qos)
This operation gets the default DomainParticipantQos of the DomainParticipantFactory ...
This class provides the basic mechanism for an application to specify Quality of Service attributes f...
static readonly DomainId Invalid
Representation of an invalid domain ID.
IDomainParticipant CreateParticipant(DomainId domainId, DomainParticipantQos qos, IDomainParticipantListener listener, StatusKind mask)
This operation creates a new IDomainParticipant which will join the domain identified by domainId...
ReturnCode
This is the enum that represents the various ReturnCode values that DDS operations return...
Definition: DdsDcpsEnums.cs:32
Since a DomainParticipant is an Entity, it has the ability to have a Listener associated with it...
The scheduling priority.
EntityFactoryQosPolicy EntityFactory
Specifies whether a just created DomainParticipant should be enabled.
ReturnCode GetQos(ref DomainParticipantFactoryQos qos)
This operation obtains the QoS settings for the DomainParticipantFactory.
static readonly DomainId Default
Representation of the default domain ID.
SchedulingClassQosPolicyKind Kind
IDomainParticipant LookupParticipant(DomainId domainId)
This operation retrieves a previously created IDomainParticipant belonging to the specified domainId...
IDomainParticipant CreateParticipant(DomainId domainId)
This operation creates a new IDomainParticipant which will join the domain identified by domainId (or...
This class provides the basic mechanism for an application to specify Quality of Service attributes f...
SchedulingPriorityQosPolicy SchedulingPriorityKind
IDomainParticipant CreateParticipant(DomainId domainId, DomainParticipantQos qos)
This operation creates a new IDomainParticipant which will join the domain identified by domainId (or...
StatusKind
Each concrete Entity class has a set of Status attributes and for each attribute the Entity class pro...
IDomainParticipant CreateParticipant(DomainId domainId, IDomainParticipantListener listener, StatusKind mask)
This operation creates a new IDomainParticipant which will join the domain identified by domainId (or...
ReturnCode SetListener(IDomainParticipantListener listener, StatusKind mask)
This operation attaches a IDomainParticipantListener to the IDomainParticipant.
ReturnCode DeleteParticipant(IDomainParticipant a_participant)
This operation deletes an IDomainParticipant.
SchedulingPriorityQosPolicyKind Kind