1 // Copyright (C) 2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // 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"
29 Engines_DSC_interface::Engines_DSC_interface() {}
31 Engines_DSC_interface::~Engines_DSC_interface() {}
34 Engines_DSC_interface::add_provides_port(Ports::Port_ptr ref,
35 const char* provides_port_name,
36 Ports::PortProperties_ptr port_prop)
37 throw (Engines::DSC::PortAlreadyDefined,
38 Engines::DSC::NilPort,
39 Engines::DSC::BadProperty)
42 assert(provides_port_name);
43 if (CORBA::is_nil(ref))
44 throw Engines::DSC::NilPort();
45 if (CORBA::is_nil(port_prop))
46 throw Engines::DSC::BadProperty();
48 my_ports_it = my_ports.find(provides_port_name);
49 if (my_ports_it == my_ports.end()) {
50 // Creating a new port provides
51 port_t * new_port = new port_t();
52 new_port->type = provides;
53 new_port->connection_nbr = 0;
54 new_port->provides_port_ref = Ports::Port::_duplicate(ref);
55 new_port->port_prop = Ports::PortProperties::_duplicate(port_prop);
57 // Port into the port's map
58 my_ports[provides_port_name] = new_port;
61 throw Engines::DSC::PortAlreadyDefined();
65 Engines_DSC_interface::add_uses_port(const char* repository_id,
66 const char* uses_port_name,
67 Ports::PortProperties_ptr port_prop)
68 throw (Engines::DSC::PortAlreadyDefined,
69 Engines::DSC::BadProperty)
72 // Note : We can't be shure that repository id
73 // is a correct CORBA id.
74 assert(repository_id);
75 assert(uses_port_name);
76 if (CORBA::is_nil(port_prop))
77 throw Engines::DSC::BadProperty();
79 my_ports_it = my_ports.find(uses_port_name);
80 if (my_ports_it == my_ports.end()) {
81 // Creating a new uses port
82 port_t * new_port = new port_t();
83 new_port->type = uses;
84 new_port->connection_nbr = 0;
85 new_port->uses_port_refs.length(0);
86 new_port->repository_id = repository_id;
87 new_port->port_prop = Ports::PortProperties::_duplicate(port_prop);
89 // Port into port's map
90 my_ports[uses_port_name] = new_port;
93 throw Engines::DSC::PortAlreadyDefined();
97 Engines_DSC_interface::get_provides_port(const char* provides_port_name,
98 const CORBA::Boolean connection_error)
99 throw (Engines::DSC::PortNotDefined,
100 Engines::DSC::PortNotConnected,
101 Engines::DSC::BadPortType)
104 assert(provides_port_name);
106 Ports::Port_ptr rtn_port = Ports::Port::_nil();
107 // std::cout << "---- DSC_Interface : MARK 1 ---- Recherche de : " << provides_port_name << "----" << std::endl;
108 // ports::iterator it;
109 // std::cout << "----> ";
110 // for(it=my_ports.begin();it!=my_ports.end();++it)
111 // std::cout << "|"<<(*it).first<<"|, ";
112 // std::cout << std::endl;
114 // Searching the port
115 my_ports_it = my_ports.find(provides_port_name);
116 if (my_ports_it == my_ports.end())
117 throw Engines::DSC::PortNotDefined();
118 if (my_ports[provides_port_name]->type != provides) {
119 Engines::DSC::BadPortType BPT;
120 BPT.expected = CORBA::string_dup("Expected a provides port");
121 BPT.received = CORBA::string_dup((std::string("Received a uses/none port : ")+provides_port_name).c_str());
125 if (my_ports[provides_port_name]->connection_nbr == 0 && connection_error)
126 throw Engines::DSC::PortNotConnected();
128 rtn_port = Ports::Port::_duplicate(my_ports[provides_port_name]->provides_port_ref);
132 Engines::DSC::uses_port *
133 Engines_DSC_interface::get_uses_port(const char* uses_port_name)
134 throw (Engines::DSC::PortNotDefined,
135 Engines::DSC::PortNotConnected,
136 Engines::DSC::BadPortType)
139 assert(uses_port_name);
141 Engines::DSC::uses_port * rtn_port = NULL;
143 // Searching the uses port
144 my_ports_it = my_ports.find(uses_port_name);
145 if (my_ports_it == my_ports.end())
146 throw Engines::DSC::PortNotDefined();
147 if (my_ports[uses_port_name]->type != uses){
148 Engines::DSC::BadPortType BPT;
149 BPT.expected = CORBA::string_dup("Expected a uses port");
150 BPT.received = CORBA::string_dup((std::string("Received a provides/none port : ")+uses_port_name).c_str());
151 std::cout << "---- DSC_Interface : MARK 1 ---- exception : " << uses_port_name << "----" << std::endl;
155 // Is the port connected ?
156 if (my_ports[uses_port_name]->connection_nbr > 0) {
157 rtn_port = new Engines::DSC::uses_port(my_ports[uses_port_name]->uses_port_refs);
161 std::cout << "---- DSC_Interface : MARK 2 ---- exception : " << uses_port_name << "----" << std::endl;
162 throw Engines::DSC::PortNotConnected();
169 Engines_DSC_interface::connect_provides_port(const char* provides_port_name)
170 throw (Engines::DSC::PortNotDefined)
173 assert(provides_port_name);
175 // Searching the provides port
176 my_ports_it = my_ports.find(provides_port_name);
177 if (my_ports_it == my_ports.end())
178 throw Engines::DSC::PortNotDefined();
179 if (my_ports[provides_port_name]->type != provides)
180 throw Engines::DSC::PortNotDefined();
183 // Adding a new connection
184 my_ports[provides_port_name]->connection_nbr += 1;
185 // User code is informed
186 provides_port_changed(provides_port_name,
187 my_ports[provides_port_name]->connection_nbr,
188 Engines::DSC::AddingConnection
193 Engines_DSC_interface::connect_uses_port(const char* uses_port_name,
194 Ports::Port_ptr provides_port_ref)
195 throw (Engines::DSC::PortNotDefined,
196 Engines::DSC::BadPortType,
197 Engines::DSC::NilPort)
200 assert(uses_port_name);
202 if (CORBA::is_nil(provides_port_ref))
203 throw Engines::DSC::NilPort();
205 // Searching the uses port
206 my_ports_it = my_ports.find(uses_port_name);
207 if (my_ports_it == my_ports.end())
208 throw Engines::DSC::PortNotDefined();
209 if (my_ports[uses_port_name]->type != uses) {
210 Engines::DSC::BadPortType BPT;
211 BPT.expected = CORBA::string_dup("Expected a uses port");
212 BPT.received = CORBA::string_dup((std::string("Received a provides/none port : ")+uses_port_name).c_str());
216 // repository_id test
217 const char * repository_id = my_ports[uses_port_name]->repository_id.c_str();
218 if (provides_port_ref->_is_a(repository_id))
220 // Adding provides port into the uses port sequence
221 CORBA::ULong lgth = my_ports[uses_port_name]->uses_port_refs.length();
222 my_ports[uses_port_name]->
223 uses_port_refs.length(lgth + 1);
224 my_ports[uses_port_name]->uses_port_refs[lgth] =
225 Ports::Port::_duplicate(provides_port_ref);
227 // Adding a new connection
228 my_ports[uses_port_name]->connection_nbr += 1;
229 // User code is informed
230 uses_port_changed(uses_port_name,
231 new Engines::DSC::uses_port(my_ports[uses_port_name]->uses_port_refs),
232 Engines::DSC::AddingConnection);
235 Engines::DSC::BadPortType BPT;
236 BPT.expected = CORBA::string_dup("Expected ...");
237 BPT.received = CORBA::string_dup((std::string("Received an incorrect repository id type ")+
238 repository_id).c_str());
245 Engines_DSC_interface::is_connected(const char* port_name)
246 throw (Engines::DSC::PortNotDefined)
248 CORBA::Boolean rtn = false;
253 my_ports_it = my_ports.find(port_name);
254 if (my_ports_it == my_ports.end())
255 throw Engines::DSC::PortNotDefined();
258 if (my_ports[port_name]->connection_nbr > 0)
265 Engines_DSC_interface::disconnect_provides_port(const char* provides_port_name,
266 const Engines::DSC::Message message)
267 throw (Engines::DSC::PortNotDefined,
268 Engines::DSC::PortNotConnected)
271 assert(provides_port_name);
273 my_ports_it = my_ports.find(provides_port_name);
274 if (my_ports_it == my_ports.end())
275 throw Engines::DSC::PortNotDefined();
276 if (my_ports[provides_port_name]->type != provides)
277 throw Engines::DSC::PortNotDefined();
280 if (my_ports[provides_port_name]->connection_nbr > 0)
282 my_ports[provides_port_name]->connection_nbr -= 1;
283 provides_port_changed(provides_port_name,
284 my_ports[provides_port_name]->connection_nbr,
288 throw Engines::DSC::PortNotConnected();
292 Engines_DSC_interface::disconnect_uses_port(const char* uses_port_name,
293 Ports::Port_ptr provides_port_ref,
294 const Engines::DSC::Message message)
295 throw (Engines::DSC::PortNotDefined,
296 Engines::DSC::PortNotConnected,
297 Engines::DSC::BadPortReference)
300 assert(uses_port_name);
302 my_ports_it = my_ports.find(uses_port_name);
303 if (my_ports_it == my_ports.end())
304 throw Engines::DSC::PortNotDefined();
305 if (my_ports[uses_port_name]->type != uses)
306 throw Engines::DSC::PortNotDefined();
308 if (CORBA::is_nil(provides_port_ref))
309 throw Engines::DSC::BadPortReference();
312 if (my_ports[uses_port_name]->connection_nbr > 0) {
313 CORBA::Long port_index = -1;
314 CORBA::ULong seq_length = my_ports[uses_port_name]->uses_port_refs.length();
315 for(int i = 0; i < seq_length; i++)
317 if (my_ports[uses_port_name]->uses_port_refs[i]->_is_equivalent(provides_port_ref))
323 if (port_index == -1)
324 throw Engines::DSC::BadPortReference();
326 my_ports[uses_port_name]->connection_nbr -= 1;
327 Engines::DSC::uses_port * new_uses_port =
328 new Engines::DSC::uses_port();
329 new_uses_port->length(seq_length - 1);
331 int index_ancien = 0;
332 int index_nouveau = 0;
333 for(;index_ancien < seq_length;) {
334 if (index_ancien == port_index)
337 // On ne change pas le index du nouveau tableau
342 (*new_uses_port)[index_nouveau] = my_ports[uses_port_name]->uses_port_refs[index_ancien];
348 // New uses port's sequence
349 my_ports[uses_port_name]->uses_port_refs = *new_uses_port;
351 // The user code is informed
352 uses_port_changed(uses_port_name,
357 throw Engines::DSC::PortNotConnected();
360 Ports::PortProperties_ptr
361 Engines_DSC_interface::get_port_properties(const char* port_name)
362 throw (Engines::DSC::PortNotDefined)
364 Ports::PortProperties_ptr rtn_properties = Ports::PortProperties::_nil();
369 my_ports_it = my_ports.find(port_name);
370 if (my_ports_it == my_ports.end())
371 throw Engines::DSC::PortNotDefined();
373 rtn_properties = Ports::PortProperties::_duplicate(my_ports[port_name]->port_prop);
374 return rtn_properties;