Salome HOME
updated copyright message
[modules/kernel.git] / src / DSC / DSC_User / Datastream / Calcium / Calcium.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Calcium.hxx"
21 #include "CalciumInterface.hxx"
22 #include "calcium.h"
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 #include <exception>
27 #include <cstring>
28
29 PySupervCompo::PySupervCompo( CORBA::ORB_ptr orb,
30                               PortableServer::POA_ptr poa,
31                               Engines::Container_ptr contain,
32                               const char *instanceName,
33                               const char *interfaceName,
34                               bool /*notif*/) :
35   Superv_Component_i(orb, poa,contain, instanceName, interfaceName,false,false)
36 {
37 }
38
39 PySupervCompo::~PySupervCompo()
40 {
41 }
42
43
44 extern "C"
45 {
46   void cp_exit(int);
47   void setDependency(provides_port*, char*, CalciumTypes::DependencyType);
48
49   void cp_exit(int err)
50     {
51       throw CalciumException(err,LOC("Abort coupling"));
52     }
53
54   void setDependency(provides_port * port,char* type,CalciumTypes::DependencyType depend)
55   {
56     if(std::string(type)=="CALCIUM_real")
57       {
58         dynamic_cast<calcium_real_port_provides *>(port)->setDependencyType(depend);
59       }
60     else if(std::string(type)=="CALCIUM_double")
61       {
62         dynamic_cast<calcium_double_port_provides *>(port)->setDependencyType(depend);
63       }
64     else if(std::string(type)=="CALCIUM_integer")
65       {
66         dynamic_cast<calcium_integer_port_provides *>(port)->setDependencyType(depend);
67       }
68     else if(std::string(type)=="CALCIUM_long")
69       {
70         dynamic_cast<calcium_long_port_provides *>(port)->setDependencyType(depend);
71       }
72     else if(std::string(type)=="CALCIUM_string")
73       {
74         dynamic_cast<calcium_string_port_provides *>(port)->setDependencyType(depend);
75       }
76     else if(std::string(type)=="CALCIUM_logical")
77       {
78         dynamic_cast<calcium_logical_port_provides *>(port)->setDependencyType(depend);
79       }
80     else if(std::string(type)=="CALCIUM_complex")
81       {
82         dynamic_cast<calcium_complex_port_provides *>(port)->setDependencyType(depend);
83       }
84     else
85       {
86         std::cerr << "unknown type:" << std::endl;
87       }
88   }
89
90   void create_calcium_port(Superv_Component_i* compo,char* name,char* type,char *mode,char* depend)
91   {
92     std::stringstream msg;
93     msg << type << " " << mode << " " << depend;
94     CORBA::String_var componentName=compo->instanceName();
95     std::string containerName=compo->getContainerName();
96     Engines_DSC_interface::writeEvent("create_calcium_port",containerName,componentName,name,"",msg.str().c_str());
97
98     if(std::string(mode) == "IN")
99       {
100         provides_port * port = 0;
101         //provides port
102         try
103           {
104             port = compo->create_provides_data_port(type);
105             compo->add_port(port, name);
106             if(std::string(depend) == "I")
107               setDependency(port,type,CalciumTypes::ITERATION_DEPENDENCY);
108             else if(std::string(depend) == "T")
109               setDependency(port,type,CalciumTypes::TIME_DEPENDENCY);
110             else
111               {
112                 std::cerr << "create_calcium_port:unknown dependency: " << depend << std::endl;
113               }
114           }
115         catch(const Superv_Component_i::PortAlreadyDefined& ex)
116           {
117             //Port already defined : we use the old one
118             delete port;
119             std::cerr << "create_calcium_port: " << ex.what() << std::endl;
120           }
121         catch ( ... )
122           {
123             std::cerr << "create_calcium_port: unknown exception" << std::endl;
124           }
125       }
126     else if(std::string(mode) == "OUT")
127       {
128         uses_port * uport = 0;
129         try
130           {
131             uport = compo->create_uses_data_port(type);
132             compo->add_port(uport, name);
133           }
134         catch(const Superv_Component_i::PortAlreadyDefined& ex)
135           {
136             //Port already defined : we use the old one
137             delete uport;
138             std::cerr << "create_calcium_port: " << ex.what() << std::endl;
139           }
140         catch ( ... )
141           {
142             std::cerr << "create_calcium_port: unknown exception" << std::endl;
143           }
144       }
145     else
146       {
147         //Unknown mode
148         std::cerr << "create_calcium_port:Unknown mode: " << mode << std::endl;
149       }
150   }
151
152   char** create_multiple_calcium_port(Superv_Component_i* compo,char* name,char* type,char *mode,char* depend, int number)
153   {
154     if (number <= 0)
155     {
156       std::cerr << "Cannot create a multiple calcium port with number <= 0, value is " << number << std::endl;
157       return NULL;
158     }
159     char** names = new char*[number];
160     for (int i = 0; i < number; i++)
161     {
162       std::ostringstream new_name;
163       new_name << name << "_" << i;
164       std::string cpp_name = new_name.str();
165       char * c_name = new char [cpp_name.size()+1];
166       strcpy(c_name, cpp_name.c_str());
167       names[i] = c_name;
168     }
169
170     // Create ports
171     for (int i = 0; i < number; i++)
172     {
173       create_calcium_port(compo, (char*)std::string(names[i]).c_str(), type, mode, depend);
174     }
175
176     return names;
177   }
178
179 }
180
181