]> SALOME platform Git repositories - modules/kernel.git/blob - src/DSC/DSC_Basic/DSC_interface.cxx
Salome HOME
CCAR: remove debug prints from the standard trace and put these prints
[modules/kernel.git] / src / DSC / DSC_Basic / DSC_interface.cxx
1 //  Copyright (C) 2007-2008  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 //  File   : DSC_interface.cxx
23 //  Author : André RIBES (EDF)
24 //  Module : KERNEL
25 //
26 #include <string>
27 #include "DSC_interface.hxx"
28
29 //#define MYDEBUG
30
31 Engines_DSC_interface::Engines_DSC_interface() {}
32
33 Engines_DSC_interface::~Engines_DSC_interface() 
34 {
35   my_ports_it = my_ports.begin();
36   for(;my_ports_it != my_ports.end();my_ports_it++)
37     delete my_ports_it->second;
38
39 }
40
41 void
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) 
48 {
49   // Method args test
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();
55
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);
64
65     // Port into the port's map
66     my_ports[provides_port_name] = new_port;
67   }
68   else
69     throw Engines::DSC::PortAlreadyDefined();
70 }
71
72 void
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) 
78 {
79   // Method args test
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();
86
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);
96
97     // Port into port's map
98     my_ports[uses_port_name] = new_port;
99   }
100   else
101     throw Engines::DSC::PortAlreadyDefined();
102 }
103
104 Ports::Port_ptr
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) 
110 {
111   // Method arg test
112   assert(provides_port_name);
113
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;
121  
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());
130     throw BPT;
131   }
132
133   if (my_ports[provides_port_name]->connection_nbr == 0 && connection_error)
134     throw Engines::DSC::PortNotConnected();
135
136   rtn_port = Ports::Port::_duplicate(my_ports[provides_port_name]->provides_port_ref);
137   return rtn_port;
138 }
139
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) 
145 {
146   // Method arg test
147   assert(uses_port_name);
148
149   Engines::DSC::uses_port * rtn_port = NULL;  
150
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());
159 #ifdef MYDEBUG
160    std::cout << "---- DSC_Interface : MARK 1 ---- exception : " << uses_port_name << "----" << std::endl;
161 #endif
162     throw BPT;
163   }
164
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);
168   }
169   else
170     {
171 #ifdef MYDEBUG
172    std::cout << "---- DSC_Interface : MARK 2 ---- exception : " << uses_port_name << "----" << std::endl;
173 #endif
174     throw Engines::DSC::PortNotConnected();
175     }
176   
177   return rtn_port;
178 }
179
180 void
181 Engines_DSC_interface::connect_provides_port(const char* provides_port_name)
182     throw (Engines::DSC::PortNotDefined)
183 {
184   // Method arg test
185   assert(provides_port_name);
186
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();
193
194
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
201                        );
202 }
203
204 void
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)
210 {
211   // Method arg test
212   assert(uses_port_name);
213
214   if (CORBA::is_nil(provides_port_ref))
215     throw Engines::DSC::NilPort();
216
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());
225     throw BPT;
226   }
227
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)) 
231   {
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);
238
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);
245   }
246   else {
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());
251     throw BPT;
252   }
253
254 }
255
256 CORBA::Boolean
257 Engines_DSC_interface::is_connected(const char* port_name) 
258   throw (Engines::DSC::PortNotDefined) 
259 {
260   CORBA::Boolean rtn = false;
261
262   // Method arg test
263   assert(port_name);
264
265   my_ports_it = my_ports.find(port_name);
266   if (my_ports_it ==  my_ports.end())
267     throw Engines::DSC::PortNotDefined();
268
269   // Is it connected ?
270   if (my_ports[port_name]->connection_nbr > 0)
271     rtn = true;
272
273   return rtn;
274 }
275
276 void
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)
281 {
282   // Method args test
283   assert(provides_port_name);
284
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();
290
291   // Is it connected ?
292   if (my_ports[provides_port_name]->connection_nbr > 0) 
293   {
294     my_ports[provides_port_name]->connection_nbr -= 1;
295     provides_port_changed(provides_port_name,
296                           my_ports[provides_port_name]->connection_nbr,
297                           message);
298   }
299   else
300     throw Engines::DSC::PortNotConnected();
301 }
302
303 void
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) 
310 {
311   // Method args test
312   assert(uses_port_name);
313
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();
319
320   if (CORBA::is_nil(provides_port_ref))
321     throw Engines::DSC::BadPortReference();
322
323   // Is it connected ?
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++)
328     {
329       if (my_ports[uses_port_name]->uses_port_refs[i]->_is_equivalent(provides_port_ref))
330       {
331         port_index = i;
332         break;
333       }
334     }
335     if (port_index == -1)
336       throw Engines::DSC::BadPortReference();
337
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);
342
343     int index_ancien = 0;
344     int index_nouveau = 0;
345     for(;index_ancien < seq_length;) {
346       if (index_ancien == port_index) 
347       {
348         // Rien a faire !
349         // On ne change pas le index du nouveau tableau
350         index_ancien += 1;
351       }
352       else 
353       {
354         (*new_uses_port)[index_nouveau] = my_ports[uses_port_name]->uses_port_refs[index_ancien];
355         index_ancien += 1;
356         index_nouveau += 1;
357       }
358     }
359
360     // New uses port's sequence
361     my_ports[uses_port_name]->uses_port_refs = *new_uses_port;
362
363     // The user code is informed
364     uses_port_changed(uses_port_name,
365                       new_uses_port,
366                       message);
367   }
368   else
369     throw Engines::DSC::PortNotConnected();
370 }
371
372 Ports::PortProperties_ptr
373 Engines_DSC_interface::get_port_properties(const char* port_name) 
374   throw (Engines::DSC::PortNotDefined) 
375 {
376   Ports::PortProperties_ptr rtn_properties = Ports::PortProperties::_nil();
377
378   // Method arg test
379   assert(port_name);
380
381   my_ports_it = my_ports.find(port_name);
382   if (my_ports_it ==  my_ports.end())
383     throw Engines::DSC::PortNotDefined();
384
385   rtn_properties = Ports::PortProperties::_duplicate(my_ports[port_name]->port_prop);
386   return rtn_properties;
387 }