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