00001
00002
00003
00004
00005
00006
00007
00008 #include "DCPS/DdsDcps_pch.h"
00009 #include "DomainParticipantFactoryImpl.h"
00010 #include "DomainParticipantImpl.h"
00011 #include "dds/DdsDcpsInfoUtilsC.h"
00012 #include "GuidConverter.h"
00013 #include "Service_Participant.h"
00014 #include "Qos_Helper.h"
00015 #include "Util.h"
00016 #include "tao/debug.h"
00017
00018 namespace OpenDDS {
00019 namespace DCPS {
00020
00021
00022 DomainParticipantFactoryImpl::DomainParticipantFactoryImpl()
00023 : qos_(TheServiceParticipant->initial_DomainParticipantFactoryQos()),
00024 default_participant_qos_(TheServiceParticipant->initial_DomainParticipantQos())
00025 {
00026 }
00027
00028
00029 DomainParticipantFactoryImpl::~DomainParticipantFactoryImpl()
00030 {
00031 if (DCPS_debug_level > 0) {
00032 ACE_DEBUG((LM_DEBUG,
00033 "%T (%P|%t) DomainParticipantFactoryImpl::"
00034 "~DomainParticipantFactoryImpl()\n"));
00035 }
00036 }
00037
00038 DDS::DomainParticipant_ptr
00039 DomainParticipantFactoryImpl::create_participant(
00040 DDS::DomainId_t domainId,
00041 const DDS::DomainParticipantQos & qos,
00042 DDS::DomainParticipantListener_ptr a_listener,
00043 DDS::StatusMask mask)
00044 {
00045 if (!Qos_Helper::valid(qos)) {
00046 ACE_ERROR((LM_ERROR,
00047 ACE_TEXT("(%P|%t) ERROR: ")
00048 ACE_TEXT("DomainParticipantFactoryImpl::create_participant, ")
00049 ACE_TEXT("invalid qos.\n")));
00050 return DDS::DomainParticipant::_nil();
00051 }
00052
00053 if (!Qos_Helper::consistent(qos)) {
00054 ACE_ERROR((LM_ERROR,
00055 ACE_TEXT("(%P|%t) ERROR: ")
00056 ACE_TEXT("DomainParticipantFactoryImpl::create_participant, ")
00057 ACE_TEXT("inconsistent qos.\n")));
00058 return DDS::DomainParticipant::_nil();
00059 }
00060
00061 Discovery_rch disco = TheServiceParticipant->get_discovery(domainId);
00062
00063 if (disco.is_nil()) {
00064 ACE_ERROR((LM_ERROR,
00065 ACE_TEXT("(%P|%t) ERROR: ")
00066 ACE_TEXT("DomainParticipantFactoryImpl::create_participant, ")
00067 ACE_TEXT("no repository found for domainId: %d.\n"), domainId));
00068 return DDS::DomainParticipant::_nil();
00069 }
00070
00071 const AddDomainStatus value =
00072 disco->add_domain_participant(domainId, qos);
00073
00074 if (value.id == GUID_UNKNOWN) {
00075 ACE_ERROR((LM_ERROR,
00076 ACE_TEXT("(%P|%t) ERROR: ")
00077 ACE_TEXT("DomainParticipantFactoryImpl::create_participant, ")
00078 ACE_TEXT("add_domain_participant returned invalid id.\n")));
00079 return DDS::DomainParticipant::_nil();
00080 }
00081
00082 DomainParticipantImpl* dp;
00083
00084 ACE_NEW_RETURN(dp,
00085 DomainParticipantImpl(this, domainId, value.id, qos, a_listener,
00086 mask, value.federated),
00087 DDS::DomainParticipant::_nil());
00088
00089 DDS::DomainParticipant_ptr dp_obj(dp);
00090
00091 if (CORBA::is_nil(dp_obj)) {
00092 ACE_ERROR((LM_ERROR,
00093 ACE_TEXT("(%P|%t) ERROR: ")
00094 ACE_TEXT("DomainParticipantFactoryImpl::create_participant, ")
00095 ACE_TEXT("nil DomainParticipant.\n")));
00096 return DDS::DomainParticipant::_nil();
00097 }
00098
00099
00100
00101 dp->set_object_reference(dp_obj);
00102
00103 if (qos_.entity_factory.autoenable_created_entities == 1) {
00104 dp->enable();
00105 }
00106
00107 ACE_GUARD_RETURN(ACE_Recursive_Thread_Mutex,
00108 tao_mon,
00109 this->participants_protector_,
00110 DDS::DomainParticipant::_nil());
00111
00112
00113
00114 Participant_Pair pair(dp, dp_obj, NO_DUP);
00115
00116 DPSet* entry;
00117
00118 if (find(participants_, domainId, entry) == -1) {
00119 DPSet set;
00120
00121 if (OpenDDS::DCPS::insert(set, pair) == -1) {
00122 ACE_ERROR((LM_ERROR,
00123 ACE_TEXT("(%P|%t) ERROR: ")
00124 ACE_TEXT("DomainParticipantFactoryImpl::create_participant, ")
00125 ACE_TEXT(" %p.\n"),
00126 ACE_TEXT("insert")));
00127 return DDS::DomainParticipant::_nil();
00128 }
00129
00130 if (OpenDDS::DCPS::bind(participants_, domainId, set) == -1) {
00131 ACE_ERROR((LM_ERROR,
00132 ACE_TEXT("(%P|%t) ERROR: ")
00133 ACE_TEXT("DomainParticipantFactoryImpl::create_participant, ")
00134 ACE_TEXT(" %p.\n"),
00135 ACE_TEXT("bind")));
00136 return DDS::DomainParticipant::_nil();
00137 }
00138
00139 } else {
00140 if (OpenDDS::DCPS::insert(*entry, pair) == -1) {
00141 ACE_ERROR((LM_ERROR,
00142 ACE_TEXT("(%P|%t) ERROR: ")
00143 ACE_TEXT("DomainParticipantFactoryImpl::create_participant, ")
00144 ACE_TEXT(" %p.\n"),
00145 ACE_TEXT("insert")));
00146 return DDS::DomainParticipant::_nil();
00147 }
00148 }
00149
00150
00151
00152 return DDS::DomainParticipant::_duplicate(dp_obj);
00153 }
00154
00155 DDS::ReturnCode_t
00156 DomainParticipantFactoryImpl::delete_participant(
00157 DDS::DomainParticipant_ptr a_participant)
00158 {
00159
00160
00161 if (CORBA::is_nil(a_participant)) {
00162 ACE_ERROR_RETURN((LM_ERROR,
00163 ACE_TEXT("(%P|%t) ERROR: ")
00164 ACE_TEXT("DomainParticipantFactoryImpl::delete_participant, ")
00165 ACE_TEXT("Nil participant.\n")),
00166 DDS::RETCODE_BAD_PARAMETER);
00167 }
00168
00169
00170
00171 DomainParticipantImpl* the_servant
00172 = dynamic_cast<DomainParticipantImpl*>(a_participant);
00173
00174
00175 if (the_servant->is_clean() == 0) {
00176 RepoId id = the_servant->get_id();
00177 GuidConverter converter(id);
00178 ACE_DEBUG((LM_DEBUG,
00179 ACE_TEXT("(%P|%t) WARNING: ")
00180 ACE_TEXT("DomainParticipantFactoryImpl::delete_participant: ")
00181 ACE_TEXT("the participant %C is not empty.\n"),
00182 OPENDDS_STRING(converter).c_str()));
00183
00184 return DDS::RETCODE_PRECONDITION_NOT_MET;
00185 }
00186
00187 DDS::DomainId_t domain_id = the_servant->get_domain_id();
00188 RepoId dp_id = the_servant->get_id();
00189
00190 DPSet* entry = 0;
00191
00192 if (find(participants_, domain_id, entry) == -1) {
00193 GuidConverter converter(dp_id);
00194 ACE_ERROR_RETURN((LM_ERROR,
00195 ACE_TEXT("(%P|%t) ERROR: ")
00196 ACE_TEXT("DomainParticipantFactoryImpl::delete_participant: ")
00197 ACE_TEXT("%p domain_id=%d dp_id=%s.\n"),
00198 ACE_TEXT("find"),
00199 domain_id,
00200 OPENDDS_STRING(converter).c_str()), DDS::RETCODE_ERROR);
00201
00202 } else {
00203 ACE_GUARD_RETURN(ACE_Recursive_Thread_Mutex,
00204 tao_mon,
00205 this->participants_protector_,
00206 DDS::RETCODE_ERROR);
00207
00208 DDS::ReturnCode_t result
00209 = the_servant->delete_contained_entities();
00210
00211
00212 if (result != DDS::RETCODE_OK) {
00213 return result;
00214 }
00215
00216 Participant_Pair pair(the_servant, a_participant, DUP);
00217
00218 if (OpenDDS::DCPS::remove(*entry, pair) == -1) {
00219 ACE_ERROR_RETURN((LM_ERROR,
00220 ACE_TEXT("(%P|%t) ERROR: ")
00221 ACE_TEXT("DomainParticipantFactoryImpl::delete_participant, ")
00222 ACE_TEXT(" %p.\n"),
00223 ACE_TEXT("remove")),
00224 DDS::RETCODE_ERROR);
00225 }
00226
00227
00228 if (entry->empty()) {
00229 if (unbind(participants_, domain_id) == -1) {
00230 ACE_ERROR_RETURN((LM_ERROR,
00231 ACE_TEXT("(%P|%t) ERROR: ")
00232 ACE_TEXT("DomainParticipantFactoryImpl::delete_participant, ")
00233 ACE_TEXT(" %p.\n"),
00234 ACE_TEXT("unbind")),
00235 DDS::RETCODE_ERROR);
00236 }
00237 }
00238 }
00239
00240 Discovery_rch disco = TheServiceParticipant->get_discovery(domain_id);
00241 if (!disco->remove_domain_participant(domain_id,
00242 dp_id)) {
00243 ACE_ERROR_RETURN((LM_ERROR,
00244 ACE_TEXT("(%P|%t) ERROR: ")
00245 ACE_TEXT("could not remove domain participant.\n")),
00246 DDS::RETCODE_ERROR);
00247 }
00248 return DDS::RETCODE_OK;
00249 }
00250
00251 DDS::DomainParticipant_ptr
00252 DomainParticipantFactoryImpl::lookup_participant(
00253 DDS::DomainId_t domainId)
00254 {
00255 ACE_GUARD_RETURN(ACE_Recursive_Thread_Mutex,
00256 tao_mon,
00257 this->participants_protector_,
00258 DDS::DomainParticipant::_nil());
00259
00260 DPSet* entry;
00261
00262 if (find(participants_, domainId, entry) == -1) {
00263 if (DCPS_debug_level >= 1) {
00264 ACE_DEBUG((LM_DEBUG,
00265 ACE_TEXT("(%P|%t) ")
00266 ACE_TEXT("DomainParticipantFactoryImpl::lookup_participant, ")
00267 ACE_TEXT(" not found for domain %d.\n"),
00268 domainId));
00269 }
00270
00271 return DDS::DomainParticipant::_nil();
00272
00273 } else {
00274
00275
00276
00277 return DDS::DomainParticipant::_duplicate((*(entry->begin())).obj_.in());
00278 }
00279 }
00280
00281 DDS::ReturnCode_t
00282 DomainParticipantFactoryImpl::set_default_participant_qos(
00283 const DDS::DomainParticipantQos & qos)
00284 {
00285 if (Qos_Helper::valid(qos)
00286 && Qos_Helper::consistent(qos)) {
00287 default_participant_qos_ = qos;
00288 return DDS::RETCODE_OK;
00289
00290 } else {
00291 return DDS::RETCODE_INCONSISTENT_POLICY;
00292 }
00293 }
00294
00295 DDS::ReturnCode_t
00296 DomainParticipantFactoryImpl::get_default_participant_qos(
00297 DDS::DomainParticipantQos & qos)
00298 {
00299 qos = default_participant_qos_;
00300 return DDS::RETCODE_OK;
00301 }
00302
00303 DDS::DomainParticipantFactory_ptr
00304 DomainParticipantFactoryImpl::get_instance()
00305 {
00306 return TheParticipantFactory;
00307 }
00308
00309 DDS::ReturnCode_t
00310 DomainParticipantFactoryImpl::set_qos(
00311 const DDS::DomainParticipantFactoryQos & qos)
00312 {
00313 if (Qos_Helper::valid(qos) && Qos_Helper::consistent(qos)) {
00314 if (!(qos_ == qos) && Qos_Helper::changeable(qos_, qos))
00315 qos_ = qos;
00316
00317 return DDS::RETCODE_OK;
00318
00319 } else {
00320 return DDS::RETCODE_INCONSISTENT_POLICY;
00321 }
00322 }
00323
00324 DDS::ReturnCode_t
00325 DomainParticipantFactoryImpl::get_qos(
00326 DDS::DomainParticipantFactoryQos & qos)
00327 {
00328 qos = this->qos_;
00329 return DDS::RETCODE_OK;
00330 }
00331
00332 const DomainParticipantFactoryImpl::DPMap&
00333 DomainParticipantFactoryImpl::participants() const
00334 {
00335 return this->participants_;
00336 }
00337
00338 }
00339 }