/* * Relationship Service for MICO * Copyright (C) 1998-99 Karel Gardas * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Send comments and/or bug reports to: * mico@informatik.uni-frankfurt.de * or to my private e-mail: * gardask@alpha.inf.upol.cz */ #include #include #include #include #ifdef HAVE_ANSI_CPLUSPLUS_HEADERS #include #include #else #include #include #endif #include //#define DEBUG 1 //#define DEBUG2 1 Role_impl::Role_impl () : POA_CosRelationships::Role () { #if DEBUG2 cout << " constructor\n"; #endif #if DEBUG cout << " Role_impl()\n" << flush; #endif iterator_server = CORBA::string_dup (""); rel_ship_repoid = CORBA::string_dup (""); rel_ships.length (0); rel_obj = CosRelationships::RelatedObject::_nil (); cardinality = 0; } Role_impl::Role_impl (CosRelationships::RelatedObject_ptr obj) : POA_CosRelationships::Role () { #if DEBUG2 cout << " constructor with params\n"; #endif rel_obj = obj; iterator_server = CORBA::string_dup (""); rel_ship_repoid = CORBA::string_dup (""); cardinality = 0; rel_ships.length (0); #if DEBUG if (CORBA::is_nil (rel_obj)) cout << " rel_obj is NIL!\n" << flush; else cout << " rel_obj is DEFINED!\n" << flush; cout << " rel_obj = " << rel_obj << "\n" << flush; cout << " constructor done.\n" << flush; #endif } Role_impl::~Role_impl () { #if DEBUG cout << " ~Role_impl ()\n" << flush; #endif delete rel_ship_repoid; delete iterator_server; //delete rel_ships; } CosRelationships::RelatedObject_ptr Role_impl::related_object () { #if DEBUG cout << " related_object();\n" << flush; if (CORBA::is_nil (rel_obj)) cout << " rel_obj is NIL!\n" << flush; #endif return CosRelationships::RelatedObject::_duplicate (rel_obj); } CosRelationships::RelatedObject_ptr Role_impl::get_other_related_object ( const CosRelationships::RelationshipHandle& rel, const char *target_name) { CosRelationships::Role_var tmp = get_other_role (rel, target_name); return tmp->related_object (); } CosRelationships::Role_ptr Role_impl::get_other_role (const CosRelationships::RelationshipHandle& rel, const char *target_name) { // CosRelationships::Relationship tmp = rel.the_relationship #if DEBUG cout << " get_other_role ();\n" << flush; cout << " target_name is " << target_name << "\n" << flush; cout << " random id: " << rel.constant_random_id << "\n" << flush; #endif CORBA::Boolean is_in = FALSE; for (CORBA::ULong i = 0; i < rel_ships.length (); i++) { if (rel.constant_random_id == rel_ships[i].constant_random_id) { is_in = TRUE; CosRelationships::NamedRoles_var nr = rel.the_relationship->named_roles(); CORBA::ULong x = nr->length(); for(CORBA::ULong j=0; j get_relationships (...)\n" << flush; cout << " rel_ships.length (): " << rel_ships.length () << "\n" << flush; cout << " first of all relationship_handles:\n" << flush; #endif CosRelationships::RelationshipHandles* tmp_handles = new CosRelationships::RelationshipHandles; int length; if (rel_ships.length () < how_many) length = rel_ships.length (); else length = how_many; tmp_handles->length (length); for (int i=0; i rand id: " << rel_ships[i].constant_random_id << "\n" << flush; #endif (*tmp_handles)[i].constant_random_id = rel_ships[i].constant_random_id; (*tmp_handles)[i].the_relationship = CosRelationships::Relationship::_duplicate (rel_ships[i].the_relationship); } rels = tmp_handles; #if DEBUG cout << " Create handles for iterator:\n" << flush; #endif if (how_many < rel_ships.length ()) { #if DEBUG cout << " rel_ships.length (): " << rel_ships.length () << "\n" << flush; cout << " how_many: " << how_many << "\n" << flush; #endif CosRelationships::RelationshipHandles tmp_relships; RelationshipIterator_impl* tmp_iter; int index = how_many; int max = rel_ships.length () - how_many; #if DEBUG cout << " max: " << max << "\n" << flush; cout << " index: " << index << "\n" << flush; #endif tmp_relships.length (max); for (CORBA::ULong i=0; i create iterator\n" << flush; #endif tmp_iter = new RelationshipIterator_impl (tmp_relships); /* CosRelationships::RelationshipIterator_ptr tmp_iterator = tmp_iter->_this (); iterator = CosRelationships::RelationshipIterator::_duplicate (tmp_iterator); */ iter = tmp_iter->_this (); assert (!CORBA::is_nil (iter.ptr())); #if DEBUG cout << " iter->_repoid (): " << iter->_repoid () << "\n" << flush; #endif } else { #if DEBUG cout << " create iterator _nil ()\n" << flush; #endif iter = CosRelationships::RelationshipIterator::_nil (); } #if DEBUG cout << " get_relationships DONE!\n" << flush; #endif } void Role_impl::destroy_relationships () { CORBA::Boolean error = FALSE; CosRelationships::Role::CannotDestroyRelationship ex; ex.offenders.length (0); int index = 0; for (CORBA::ULong i=0; idestroy (); #ifdef HAVE_EXCEPTIONS } catch (CosRelationships::Relationship::CannotUnlink_catch &exception) { error = TRUE; ex.offenders.length (ex.offenders.length () + 1); index = ex.offenders.length () - 1; ex.offenders[index].constant_random_id = rel_ships[i].constant_random_id; ex.offenders[index].the_relationship = CosRelationships::Relationship::_duplicate (rel_ships[i].the_relationship); } #endif } if (error) mico_throw (ex); } void Role_impl::destroy () { if (rel_ships.length () != 0) { CosRelationships::Role::ParticipatingInRelationship ex; ex.the_relationships = rel_ships; mico_throw (ex); } PortableServer::ObjectId_var oid = _default_POA ()->servant_to_id (this); _default_POA ()->deactivate_object (*oid); delete this; } CORBA::Boolean Role_impl::check_minimum_cardinality () { if (cardinality > min_cardinality) return TRUE; else return FALSE; } void Role_impl::link (const CosRelationships::RelationshipHandle& rel, const CosRelationships::NamedRoles& named_roles) { #if DEBUG cout << " link (...)\n" << flush; cout << " cardinality = " << cardinality << "\n" << flush; #endif //PortableServer::POA_var poa = _default_POA (); //CORBA::ORB_var o = orb; if (cardinality == max_cardinality) { #if DEBUG cout << " MaxCardinalityExceeded\n" << flush; #endif CosRelationships::RelationshipFactory::MaxCardinalityExceeded ex; ex.culprits.length (1); #if DEBUG cout << " ex.culprits.length (1);\n" << flush; #endif CosRelationships::Role_ptr tmp_this = _this (); ex.culprits[0].aRole = tmp_this; for (CORBA::ULong i=0; i " << i << "\n" << flush; #endif if (tmp_this->_is_equivalent (named_roles[i].aRole.in())) { #if DEBUG cout << " is equivalent!, name is " << named_roles[i].name << "\n" << flush; #endif ex.culprits[0].name = CORBA::string_dup (named_roles[i].name); #if DEBUG cout << " ex.culprits[0].name is " << ex.culprits[0].name << "\n" << flush; #endif } } #if DEBUG cout << " throw maxcard except :))\n" << flush; #endif mico_throw (ex); } #if DEBUG cout << " cardinality is OK!\n" << flush; #endif if (strcmp (rel_ship_repoid, "") != 0) { if (!rel.the_relationship->_get_interface ()->is_a (rel_ship_repoid)) { CosRelationships::Role::RelationshipTypeError ex; mico_throw (ex); } } rel_ships.length (rel_ships.length () + 1); rel_ships[rel_ships.length () - 1].constant_random_id = rel.constant_random_id; rel_ships[rel_ships.length () - 1].the_relationship = CosRelationships::Relationship::_duplicate (rel.the_relationship.in()); cardinality++; #if DEBUG cout << " last relship random_id: " << rel_ships[rel_ships.length () - 1].constant_random_id << "\n" << flush; cout << " link (...) done!\n" << flush; #endif } void Role_impl::unlink(const CosRelationships::RelationshipHandle& rel) { CosRelationships::RelationshipHandle tmp_rel_handle; tmp_rel_handle.constant_random_id = 0; tmp_rel_handle.the_relationship = CosRelationships::Relationship::_nil (); int pos = 0; CORBA::Boolean is_in = FALSE; #if DEBUG cout << " unlink (...)\n" << flush; cout << " random_id: " << rel.constant_random_id << "\n" << flush; cout << " rel_ships length: " << rel_ships.length () << "\n" << flush; #endif for (CORBA::ULong i=0; i " << i << ". r_handle random id: " << rel_ships[i].constant_random_id << "\n" << flush; #endif if (rel.constant_random_id == rel_ships[i].constant_random_id) { pos = i; is_in = TRUE; #if DEBUG cout << " position is " << pos << "\n" << flush; #endif break; } } if (is_in) { for (CORBA::ULong i=pos; i