Back to index

salome-kernel  6.5.0
ConnectionManager_i.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : ConnectionManager_i.cxx
00024 //  Author : André RIBES (EDF)
00025 //  Module : KERNEL
00026 //
00027 #include "ConnectionManager_i.hxx"
00028 #include "SALOME_NamingService.hxx"
00029 
00030 #ifdef WIN32
00031 # include <process.h>
00032 #endif
00033 
00034 ConnectionManager_i::ConnectionManager_i(CORBA::ORB_ptr orb) {
00035   _orb = CORBA::ORB::_duplicate(orb) ;
00036   SALOME_NamingService * ns = new SALOME_NamingService(orb);
00037   const char * ConnectionManagerNameInNS = "/ConnectionManager";
00038   CORBA::Object_var obref = _this();
00039   _remove_ref();
00040   ns->Register(obref, ConnectionManagerNameInNS);
00041   delete ns;
00042 
00043   current_id = 0;
00044   pthread_mutex_init(&mutex, NULL);
00045 }
00046 
00047 ConnectionManager_i::~ConnectionManager_i() {}
00048 
00049 Engines::ConnectionManager::connectionId
00050 ConnectionManager_i::connect(Engines::DSC_ptr uses_component, 
00051                              const char* uses_port_name, 
00052                              Engines::DSC_ptr provides_component, 
00053                              const char* provides_port_name) 
00054 {
00055 
00056   Ports::Port_var p_port = provides_component->get_provides_port(provides_port_name, false);
00057   uses_component->connect_uses_port(uses_port_name, p_port);
00058   provides_component->connect_provides_port(provides_port_name);
00059 
00060   // Creating a new connection id.
00061   // We use a mutex for multithreaded applications.
00062   pthread_mutex_lock(&mutex);
00063   Engines::ConnectionManager::connectionId rtn_id = current_id;
00064   current_id += 1;
00065   pthread_mutex_unlock(&mutex);
00066 
00067   // Creating a new structure containing connection's infos.
00068   connection_infos * infos = new connection_infos();
00069   infos->uses_component = Engines::DSC::_duplicate(uses_component);
00070   infos->uses_port_name = uses_port_name;
00071   infos->provides_component = Engines::DSC::_duplicate(provides_component);
00072   infos->provides_port_name = provides_port_name;
00073   infos->provides_port = Ports::Port::_duplicate(p_port);
00074 
00075   // Adding the new connection into the map.
00076   ids[rtn_id] = infos;
00077 
00078   return rtn_id;
00079 }
00080 
00081 void
00082 ConnectionManager_i::disconnect(Engines::ConnectionManager::connectionId id,
00083                                 Engines::DSC::Message message)
00084 {
00085   int err=0;
00086   // Connection id exist ?
00087   ids_it = ids.find(id);
00088   if (ids_it == ids.end())
00089     throw Engines::ConnectionManager::BadId();
00090 
00091   // TODO
00092   // We need to catch exceptions if one of these disconnect operation fails.
00093   connection_infos * infos = ids[id];
00094   try
00095     {
00096       infos->provides_component->disconnect_provides_port(infos->provides_port_name.c_str(), message);
00097     }
00098   catch(CORBA::SystemException& ex)
00099     {
00100       std::cerr << "Problem in disconnect(CORBA::SystemException) provides port: " << infos->provides_port_name << std::endl;
00101       err=1;
00102     }
00103   try
00104     {
00105       infos->uses_component->disconnect_uses_port(infos->uses_port_name.c_str(),
00106                                                   infos->provides_port, message);
00107     }
00108   catch(CORBA::SystemException& ex)
00109     {
00110       std::cerr << "Problem in disconnect(CORBA::SystemException) uses port: " << infos->uses_port_name << std::endl;
00111       err=1;
00112     }
00113   delete infos;
00114   ids.erase(id);
00115 
00116   if(err)
00117     throw Engines::DSC::BadPortReference();
00118 }
00119 
00120 void
00121 ConnectionManager_i::ShutdownWithExit()
00122 {
00123   ids_it = ids.begin();
00124   while(ids_it != ids.end())
00125     {
00126       disconnect(ids_it->first, Engines::DSC::RemovingConnection);
00127       ids_it = ids.begin();
00128     }
00129 
00130   if(!CORBA::is_nil(_orb))
00131     _orb->shutdown(0);
00132 
00133 }
00134 
00135 CORBA::Long
00136 ConnectionManager_i::getPID()
00137 {
00138     return
00139 #ifndef WIN32
00140     (CORBA::Long)getpid();
00141 #else
00142     (CORBA::Long)_getpid();
00143 #endif
00144 }