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