Salome HOME
00d4f83e8d6719ccbd2634e0818487af0dc88c00
[modules/kernel.git] / src / DSC / DSC_Basic / ConnectionManager_i.cxx
1 // Copyright (C) 2007-2014  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 //  File   : ConnectionManager_i.cxx
24 //  Author : AndrĂ© RIBES (EDF)
25 //  Module : KERNEL
26 //
27 #include "ConnectionManager_i.hxx"
28 #include "SALOME_NamingService.hxx"
29
30 #ifdef WIN32
31 # include <process.h>
32 #else
33 # include <unistd.h>
34 #endif
35
36 ConnectionManager_i::ConnectionManager_i(CORBA::ORB_ptr orb) {
37   _orb = CORBA::ORB::_duplicate(orb) ;
38   SALOME_NamingService * ns = new SALOME_NamingService(orb);
39   const char * ConnectionManagerNameInNS = "/ConnectionManager";
40   CORBA::Object_var obref = _this();
41   _remove_ref();
42   ns->Register(obref, ConnectionManagerNameInNS);
43   delete ns;
44
45   current_id = 0;
46   pthread_mutex_init(&mutex, NULL);
47 }
48
49 ConnectionManager_i::~ConnectionManager_i() {}
50
51 Engines::ConnectionManager::connectionId
52 ConnectionManager_i::connect(Engines::DSC_ptr uses_component, 
53                              const char* uses_port_name, 
54                              Engines::DSC_ptr provides_component, 
55                              const char* provides_port_name) 
56 {
57
58   Ports::Port_var p_port = provides_component->get_provides_port(provides_port_name, false);
59   uses_component->connect_uses_port(uses_port_name, p_port);
60   provides_component->connect_provides_port(provides_port_name);
61
62   // Creating a new connection id.
63   // We use a mutex for multithreaded applications.
64   pthread_mutex_lock(&mutex);
65   Engines::ConnectionManager::connectionId rtn_id = current_id;
66   current_id += 1;
67   pthread_mutex_unlock(&mutex);
68
69   // Creating a new structure containing connection's infos.
70   connection_infos * infos = new connection_infos();
71   infos->uses_component = Engines::DSC::_duplicate(uses_component);
72   infos->uses_port_name = uses_port_name;
73   infos->provides_component = Engines::DSC::_duplicate(provides_component);
74   infos->provides_port_name = provides_port_name;
75   infos->provides_port = Ports::Port::_duplicate(p_port);
76
77   // Adding the new connection into the map.
78   ids[rtn_id] = infos;
79
80   return rtn_id;
81 }
82
83 void
84 ConnectionManager_i::disconnect(Engines::ConnectionManager::connectionId id,
85                                 Engines::DSC::Message message)
86 {
87   int err=0;
88   // Connection id exist ?
89   ids_it = ids.find(id);
90   if (ids_it == ids.end())
91     throw Engines::ConnectionManager::BadId();
92
93   // TODO
94   // We need to catch exceptions if one of these disconnect operation fails.
95   connection_infos * infos = ids[id];
96   try
97     {
98       infos->provides_component->disconnect_provides_port(infos->provides_port_name.c_str(), message);
99     }
100   catch(CORBA::SystemException& ex)
101     {
102       std::cerr << "Problem in disconnect(CORBA::SystemException) provides port: " << infos->provides_port_name << std::endl;
103       err=1;
104     }
105   try
106     {
107       infos->uses_component->disconnect_uses_port(infos->uses_port_name.c_str(),
108                                                   infos->provides_port, message);
109     }
110   catch(CORBA::SystemException& ex)
111     {
112       std::cerr << "Problem in disconnect(CORBA::SystemException) uses port: " << infos->uses_port_name << std::endl;
113       err=1;
114     }
115   delete infos;
116   ids.erase(id);
117
118   if(err)
119     throw Engines::DSC::BadPortReference();
120 }
121
122 void
123 ConnectionManager_i::ShutdownWithExit()
124 {
125   ids_it = ids.begin();
126   while(ids_it != ids.end())
127     {
128       disconnect(ids_it->first, Engines::DSC::RemovingConnection);
129       ids_it = ids.begin();
130     }
131
132   if(!CORBA::is_nil(_orb))
133     _orb->shutdown(0);
134
135 }
136
137 CORBA::Long
138 ConnectionManager_i::getPID()
139 {
140     return
141 #ifndef WIN32
142     (CORBA::Long)getpid();
143 #else
144     (CORBA::Long)_getpid();
145 #endif
146 }