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