1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : DSC_interface.cxx
23 // Author : André RIBES (EDF)
27 #include "DSC_interface.hxx"
31 Engines_DSC_interface::Engines_DSC_interface() {}
33 Engines_DSC_interface::~Engines_DSC_interface()
35 my_ports_it = my_ports.begin();
36 for(;my_ports_it != my_ports.end();my_ports_it++)
37 delete my_ports_it->second;
42 Engines_DSC_interface::add_provides_port(Ports::Port_ptr ref,
43 const char* provides_port_name,
44 Ports::PortProperties_ptr port_prop)
45 throw (Engines::DSC::PortAlreadyDefined,
46 Engines::DSC::NilPort,
47 Engines::DSC::BadProperty)
50 assert(provides_port_name);
51 if (CORBA::is_nil(ref))
52 throw Engines::DSC::NilPort();
53 if (CORBA::is_nil(port_prop))
54 throw Engines::DSC::BadProperty();
56 my_ports_it = my_ports.find(provides_port_name);
57 if (my_ports_it == my_ports.end()) {
58 // Creating a new port provides
59 port_t * new_port = new port_t();
60 new_port->type = provides;
61 new_port->connection_nbr = 0;
62 new_port->provides_port_ref = Ports::Port::_duplicate(ref);
63 new_port->port_prop = Ports::PortProperties::_duplicate(port_prop);
65 // Port into the port's map
66 my_ports[provides_port_name] = new_port;
69 throw Engines::DSC::PortAlreadyDefined();
73 Engines_DSC_interface::add_uses_port(const char* repository_id,
74 const char* uses_port_name,
75 Ports::PortProperties_ptr port_prop)
76 throw (Engines::DSC::PortAlreadyDefined,
77 Engines::DSC::BadProperty)
80 // Note : We can't be shure that repository id
81 // is a correct CORBA id.
82 assert(repository_id);
83 assert(uses_port_name);
84 if (CORBA::is_nil(port_prop))
85 throw Engines::DSC::BadProperty();
87 my_ports_it = my_ports.find(uses_port_name);
88 if (my_ports_it == my_ports.end()) {
89 // Creating a new uses port
90 port_t * new_port = new port_t();
91 new_port->type = uses;
92 new_port->connection_nbr = 0;
93 new_port->uses_port_refs.length(0);
94 new_port->repository_id = repository_id;
95 new_port->port_prop = Ports::PortProperties::_duplicate(port_prop);
97 // Port into port's map
98 my_ports[uses_port_name] = new_port;
101 throw Engines::DSC::PortAlreadyDefined();
105 Engines_DSC_interface::get_provides_port(const char* provides_port_name,
106 const CORBA::Boolean connection_error)
107 throw (Engines::DSC::PortNotDefined,
108 Engines::DSC::PortNotConnected,
109 Engines::DSC::BadPortType)
112 assert(provides_port_name);
114 Ports::Port_ptr rtn_port = Ports::Port::_nil();
115 // std::cout << "---- DSC_Interface : MARK 1 ---- Recherche de : " << provides_port_name << "----" << std::endl;
116 // ports::iterator it;
117 // std::cout << "----> ";
118 // for(it=my_ports.begin();it!=my_ports.end();++it)
119 // std::cout << "|"<<(*it).first<<"|, ";
120 // std::cout << std::endl;
122 // Searching the port
123 my_ports_it = my_ports.find(provides_port_name);
124 if (my_ports_it == my_ports.end())
125 throw Engines::DSC::PortNotDefined();
126 if (my_ports[provides_port_name]->type != provides) {
127 Engines::DSC::BadPortType BPT;
128 BPT.expected = CORBA::string_dup("Expected a provides port");
129 BPT.received = CORBA::string_dup((std::string("Received a uses/none port : ")+provides_port_name).c_str());
133 if (my_ports[provides_port_name]->connection_nbr == 0 && connection_error)
134 throw Engines::DSC::PortNotConnected();
136 rtn_port = Ports::Port::_duplicate(my_ports[provides_port_name]->provides_port_ref);
140 Engines::DSC::uses_port *
141 Engines_DSC_interface::get_uses_port(const char* uses_port_name)
142 throw (Engines::DSC::PortNotDefined,
143 Engines::DSC::PortNotConnected,
144 Engines::DSC::BadPortType)
147 assert(uses_port_name);
149 Engines::DSC::uses_port * rtn_port = NULL;
151 // Searching the uses port
152 my_ports_it = my_ports.find(uses_port_name);
153 if (my_ports_it == my_ports.end())
154 throw Engines::DSC::PortNotDefined();
155 if (my_ports[uses_port_name]->type != uses){
156 Engines::DSC::BadPortType BPT;
157 BPT.expected = CORBA::string_dup("Expected a uses port");
158 BPT.received = CORBA::string_dup((std::string("Received a provides/none port : ")+uses_port_name).c_str());
160 std::cout << "---- DSC_Interface : MARK 1 ---- exception : " << uses_port_name << "----" << std::endl;
165 // Is the port connected ?
166 if (my_ports[uses_port_name]->connection_nbr > 0) {
167 rtn_port = new Engines::DSC::uses_port(my_ports[uses_port_name]->uses_port_refs);
172 std::cout << "---- DSC_Interface : MARK 2 ---- exception : " << uses_port_name << "----" << std::endl;
174 throw Engines::DSC::PortNotConnected();
181 Engines_DSC_interface::connect_provides_port(const char* provides_port_name)
182 throw (Engines::DSC::PortNotDefined)
185 assert(provides_port_name);
187 // Searching the provides port
188 my_ports_it = my_ports.find(provides_port_name);
189 if (my_ports_it == my_ports.end())
190 throw Engines::DSC::PortNotDefined();
191 if (my_ports[provides_port_name]->type != provides)
192 throw Engines::DSC::PortNotDefined();
195 // Adding a new connection
196 my_ports[provides_port_name]->connection_nbr += 1;
197 // User code is informed
198 provides_port_changed(provides_port_name,
199 my_ports[provides_port_name]->connection_nbr,
200 Engines::DSC::AddingConnection
205 Engines_DSC_interface::connect_uses_port(const char* uses_port_name,
206 Ports::Port_ptr provides_port_ref)
207 throw (Engines::DSC::PortNotDefined,
208 Engines::DSC::BadPortType,
209 Engines::DSC::NilPort)
212 assert(uses_port_name);
214 if (CORBA::is_nil(provides_port_ref))
215 throw Engines::DSC::NilPort();
217 // Searching the uses port
218 my_ports_it = my_ports.find(uses_port_name);
219 if (my_ports_it == my_ports.end())
220 throw Engines::DSC::PortNotDefined();
221 if (my_ports[uses_port_name]->type != uses) {
222 Engines::DSC::BadPortType BPT;
223 BPT.expected = CORBA::string_dup("Expected a uses port");
224 BPT.received = CORBA::string_dup((std::string("Received a provides/none port : ")+uses_port_name).c_str());
228 // repository_id test
229 const char * repository_id = my_ports[uses_port_name]->repository_id.c_str();
230 if (provides_port_ref->_is_a(repository_id))
232 // Adding provides port into the uses port sequence
233 CORBA::ULong lgth = my_ports[uses_port_name]->uses_port_refs.length();
234 my_ports[uses_port_name]->
235 uses_port_refs.length(lgth + 1);
236 my_ports[uses_port_name]->uses_port_refs[lgth] =
237 Ports::Port::_duplicate(provides_port_ref);
239 // Adding a new connection
240 my_ports[uses_port_name]->connection_nbr += 1;
241 // User code is informed
242 uses_port_changed(uses_port_name,
243 new Engines::DSC::uses_port(my_ports[uses_port_name]->uses_port_refs),
244 Engines::DSC::AddingConnection);
247 Engines::DSC::BadPortType BPT;
248 BPT.expected = CORBA::string_dup("Expected ...");
249 BPT.received = CORBA::string_dup((std::string("Received an incorrect repository id type ")+
250 repository_id).c_str());
257 Engines_DSC_interface::is_connected(const char* port_name)
258 throw (Engines::DSC::PortNotDefined)
260 CORBA::Boolean rtn = false;
265 my_ports_it = my_ports.find(port_name);
266 if (my_ports_it == my_ports.end())
267 throw Engines::DSC::PortNotDefined();
270 if (my_ports[port_name]->connection_nbr > 0)
277 Engines_DSC_interface::disconnect_provides_port(const char* provides_port_name,
278 const Engines::DSC::Message message)
279 throw (Engines::DSC::PortNotDefined,
280 Engines::DSC::PortNotConnected)
283 assert(provides_port_name);
285 my_ports_it = my_ports.find(provides_port_name);
286 if (my_ports_it == my_ports.end())
287 throw Engines::DSC::PortNotDefined();
288 if (my_ports[provides_port_name]->type != provides)
289 throw Engines::DSC::PortNotDefined();
292 if (my_ports[provides_port_name]->connection_nbr > 0)
294 my_ports[provides_port_name]->connection_nbr -= 1;
295 provides_port_changed(provides_port_name,
296 my_ports[provides_port_name]->connection_nbr,
300 throw Engines::DSC::PortNotConnected();
304 Engines_DSC_interface::disconnect_uses_port(const char* uses_port_name,
305 Ports::Port_ptr provides_port_ref,
306 const Engines::DSC::Message message)
307 throw (Engines::DSC::PortNotDefined,
308 Engines::DSC::PortNotConnected,
309 Engines::DSC::BadPortReference)
312 assert(uses_port_name);
314 my_ports_it = my_ports.find(uses_port_name);
315 if (my_ports_it == my_ports.end())
316 throw Engines::DSC::PortNotDefined();
317 if (my_ports[uses_port_name]->type != uses)
318 throw Engines::DSC::PortNotDefined();
320 if (CORBA::is_nil(provides_port_ref))
321 throw Engines::DSC::BadPortReference();
324 if (my_ports[uses_port_name]->connection_nbr > 0) {
325 CORBA::Long port_index = -1;
326 CORBA::ULong seq_length = my_ports[uses_port_name]->uses_port_refs.length();
327 for(int i = 0; i < seq_length; i++)
329 if (my_ports[uses_port_name]->uses_port_refs[i]->_is_equivalent(provides_port_ref))
335 if (port_index == -1)
336 throw Engines::DSC::BadPortReference();
338 my_ports[uses_port_name]->connection_nbr -= 1;
339 Engines::DSC::uses_port * new_uses_port =
340 new Engines::DSC::uses_port();
341 new_uses_port->length(seq_length - 1);
343 int index_ancien = 0;
344 int index_nouveau = 0;
345 for(;index_ancien < seq_length;) {
346 if (index_ancien == port_index)
349 // On ne change pas le index du nouveau tableau
354 (*new_uses_port)[index_nouveau] = my_ports[uses_port_name]->uses_port_refs[index_ancien];
360 // New uses port's sequence
361 my_ports[uses_port_name]->uses_port_refs = *new_uses_port;
363 // The user code is informed
364 uses_port_changed(uses_port_name,
369 throw Engines::DSC::PortNotConnected();
372 Ports::PortProperties_ptr
373 Engines_DSC_interface::get_port_properties(const char* port_name)
374 throw (Engines::DSC::PortNotDefined)
376 Ports::PortProperties_ptr rtn_properties = Ports::PortProperties::_nil();
381 my_ports_it = my_ports.find(port_name);
382 if (my_ports_it == my_ports.end())
383 throw Engines::DSC::PortNotDefined();
385 rtn_properties = Ports::PortProperties::_duplicate(my_ports[port_name]->port_prop);
386 return rtn_properties;