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