Salome HOME
[EDF30062] and [EDF30014] : addition of --activate-custom-overrides parameter in...
[modules/yacs.git] / src / runtime / ConnectionManager.cxx
1 // Copyright (C) 2007-2024  CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ConnectionManager.hxx"
21 #include <iostream>
22
23 ConnectionManager::ConnectionManager()
24 : _ids()
25 , _current_id(0)
26 , _mutex()
27 {
28 }
29
30 ConnectionManager::~ConnectionManager()
31 {
32 }
33
34 ConnectionManager::connectionId
35 ConnectionManager::connect(Engines::DSC_ptr uses_component, 
36                              const char* uses_port_name, 
37                              Engines::DSC_ptr provides_component, 
38                              const char* provides_port_name) 
39 {
40   // We use a mutex for multithreaded applications.
41   std::unique_lock<std::mutex> lock(_mutex);
42   Ports::Port_var p_port = provides_component->get_provides_port(provides_port_name, false);
43   uses_component->connect_uses_port(uses_port_name, p_port);
44   provides_component->connect_provides_port(provides_port_name);
45
46   // Creating a new structure containing connection's infos.
47   connection_infos * infos = new connection_infos();
48   infos->uses_component = Engines::DSC::_duplicate(uses_component);
49   infos->uses_port_name = uses_port_name;
50   infos->provides_component = Engines::DSC::_duplicate(provides_component);
51   infos->provides_port_name = provides_port_name;
52   infos->provides_port = Ports::Port::_duplicate(p_port);
53
54   // Creating a new connection id.
55   ConnectionManager::connectionId rtn_id = _current_id;
56   _current_id += 1;
57   // Adding the new connection into the map.
58   _ids[rtn_id] = infos;
59
60   return rtn_id;
61 }
62
63 void
64 ConnectionManager::disconnect(ConnectionManager::connectionId id,
65                               Engines::DSC::Message message)
66 {
67   std::unique_lock<std::mutex> lock(_mutex);
68   int err=0;
69   // Connection id exist ?
70   ids_it_type ids_it = _ids.find(id);
71   if (ids_it == _ids.end())
72     return;
73
74   // TODO
75   // We need to catch exceptions if one of these disconnect operation fails.
76   // connection_infos * infos = ids[id];
77   connection_infos * infos = ids_it->second;
78   try
79     {
80       infos->provides_component->disconnect_provides_port(infos->provides_port_name.c_str(), message);
81     }
82   catch(CORBA::SystemException& ex)
83     {
84       std::cerr << "Problem in disconnect(CORBA::SystemException) provides port: " << infos->provides_port_name.c_str() << std::endl;
85       err=1;
86     }
87   try
88     {
89       infos->uses_component->disconnect_uses_port(infos->uses_port_name.c_str(),
90                                                   infos->provides_port, message);
91     }
92   catch(CORBA::SystemException& ex)
93     {
94       std::cerr << "Problem in disconnect(CORBA::SystemException) uses port: " << infos->uses_port_name.c_str() << std::endl;
95       err=1;
96     }
97   delete infos;
98   _ids.erase(id);
99
100   if(err)
101     throw Engines::DSC::BadPortReference();
102 }
103
104 void
105 ConnectionManager::ShutdownWithExit()
106 {
107   ids_it_type ids_it = _ids.begin();
108   while(ids_it != _ids.end())
109     {
110       disconnect(ids_it->first, Engines::DSC::RemovingConnection);
111       ids_it = _ids.begin();
112     }
113 }