1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.objectweb.fractal.cecilia.primitive.thinkMC.types;
25
26 import static java.util.Arrays.asList;
27 import static org.objectweb.fractal.cecilia.adl.interfaces.InterfaceDecorationUtil.getBoundTo;
28 import static org.objectweb.fractal.cecilia.adl.interfaces.InterfaceDecorationUtil.getContainer;
29 import static org.objectweb.fractal.cecilia.adl.interfaces.InterfaceDecorationUtil.isNoStaticBinding;
30 import static org.objectweb.fractal.task.core.TaskFactory.EXPORT_ALL;
31
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.LinkedHashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38
39 import org.objectweb.fractal.adl.ADLException;
40 import org.objectweb.fractal.adl.components.ComponentContainer;
41 import org.objectweb.fractal.adl.interfaces.Interface;
42 import org.objectweb.fractal.adl.types.TypeInterface;
43 import org.objectweb.fractal.api.Component;
44 import org.objectweb.fractal.cecilia.adl.AbstractInstantiationTask;
45 import org.objectweb.fractal.cecilia.adl.InstanceNameProvider;
46 import org.objectweb.fractal.cecilia.adl.SourceCodeProvider;
47 import org.objectweb.fractal.cecilia.adl.TypeNameProvider;
48 import org.objectweb.fractal.cecilia.adl.file.CodeWriter;
49 import org.objectweb.fractal.cecilia.adl.interfaces.AbstractClientInterfaceVisitor;
50 import org.objectweb.fractal.task.core.TaskException;
51 import org.objectweb.fractal.task.core.primitive.annotations.ClientInterfaceForEach;
52 import org.objectweb.fractal.task.core.primitive.annotations.ServerInterface;
53 import org.objectweb.fractal.task.core.primitive.annotations.ServerInterfaces;
54 import org.objectweb.fractal.task.core.primitive.annotations.TaskParameters;
55
56
57
58
59
60 public class ClientInterfaceInstantiationVisitor
61 extends
62 AbstractClientInterfaceVisitor {
63
64
65
66
67
68
69
70
71
72 @Override
73 protected Component createTask(final ComponentContainer container,
74 final List<TypeInterface> clientInterfaces) throws ADLException,
75 TaskException {
76 final LinkedHashMap<TypeInterface, Interface> clientInterfaceBindings = new LinkedHashMap<TypeInterface, Interface>();
77 final Set<Object> serverComponents = new HashSet<Object>();
78 for (final TypeInterface clientInterface : clientInterfaces) {
79 final Interface serverItf = isNoStaticBinding(clientInterface)
80 ? null
81 : getBoundTo(clientInterface);
82 clientInterfaceBindings.put(clientInterface, serverItf);
83 if (serverItf != null) {
84 serverComponents.add(getContainer(serverItf));
85 }
86 }
87 final Component includeTask = createIncludeClientInstanceTask(container,
88 clientInterfaceBindings, serverComponents);
89 final Component initializationTask = createInitializationClientInstanceTask(
90 container, clientInterfaceBindings, serverComponents);
91
92 return taskFactoryItf.newCompositeTask(asList(includeTask,
93 initializationTask), EXPORT_ALL, null);
94 }
95
96
97
98
99
100 protected Component createIncludeClientInstanceTask(
101 final ComponentContainer container,
102 final LinkedHashMap<TypeInterface, Interface> clientInterfaceBindings,
103 final Set<Object> serverComponents) throws TaskException {
104 return taskFactoryItf.newPrimitiveTask(new IncludeClientInstanceTask(
105 clientInterfaceBindings), container, serverComponents);
106 }
107
108 protected Component createInitializationClientInstanceTask(
109 final ComponentContainer container,
110 final LinkedHashMap<TypeInterface, Interface> clientInterfaceBindings,
111 final Set<Object> serverComponents) throws TaskException {
112 return taskFactoryItf.newPrimitiveTask(
113 new InitializationClientInstanceTask(clientInterfaceBindings),
114 container, serverComponents);
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 @TaskParameters({"componentNode", "serverComponents"})
132 @ServerInterfaces(@ServerInterface(name = "source-code-provider", signature = SourceCodeProvider.class, record = "role:clientInterfaceInstantiation, id:%, codePiece:include", parameters = "componentNode"))
133 public static class IncludeClientInstanceTask
134 extends
135 AbstractInstantiationTask {
136
137 protected final LinkedHashMap<TypeInterface, Interface> staticBindings;
138
139
140
141
142
143
144
145
146
147 @ClientInterfaceForEach(iterable = "serverComponents", prefix = "server-type-name-provider", signature = TypeNameProvider.class, record = "role:staticBindingTypeNameProvider, id:%", parameters = "serverComponents.element")
148 public final Map<Object, TypeNameProvider> serverTypeNameProviderItf = new HashMap<Object, TypeNameProvider>();
149
150
151
152
153
154 @ClientInterfaceForEach(iterable = "serverComponents", prefix = "server-type-definition-provider", signature = SourceCodeProvider.class, record = "role:staticBindingTypeDefinitionProvider, id:%", parameters = "serverComponents.element")
155 public final Map<Object, SourceCodeProvider> serverTypeDefinitionProviderItf = new HashMap<Object, SourceCodeProvider>();
156
157
158
159
160
161 @ClientInterfaceForEach(iterable = "serverComponents", prefix = "server-instance-name-provider", signature = InstanceNameProvider.class, record = "role:staticBindingInstanceNameProvider, id:%", parameters = "serverComponents.element")
162 public final Map<Object, InstanceNameProvider> serverInstanceNameProviderItf = new HashMap<Object, InstanceNameProvider>();
163
164
165
166
167
168
169
170
171
172
173
174 public IncludeClientInstanceTask(
175 final LinkedHashMap<TypeInterface, Interface> staticBindings) {
176 this.staticBindings = staticBindings;
177 }
178
179 @Override
180 protected String processSourceCode() throws Exception {
181 final CodeWriter cw = new CodeWriter(
182 "Client Interfaces Instantiation Builder (extern references to server components.)");
183 for (final Map.Entry<TypeInterface, Interface> staticBinding : staticBindings
184 .entrySet()) {
185
186 if (staticBinding.getValue() == null) continue;
187
188 final Object serverComponent = getContainer(staticBinding.getValue());
189 final String serverTypeName = serverTypeNameProviderItf.get(
190 serverComponent).getCTypeName();
191 final String serverTypeDefinition = serverTypeDefinitionProviderItf
192 .get(serverComponent).getSourceCode();
193 final String serverInstanceName = serverInstanceNameProviderItf.get(
194 serverComponent).getCInstanceName();
195
196
197
198 cw.append("#ifndef ").append(serverInstanceName).append("_DECLARED")
199 .endl();
200 if (serverTypeName.equals(typeNameProviderItf.getCTypeName())) {
201 cw.append("extern struct ").append(
202 serverTypeName.toString().replace('.', '_')).append("_t ")
203 .append(serverInstanceName.replace('.', '_')).append(";").endl();
204 } else {
205 cw.append(serverTypeDefinition).endl();
206 cw.append("extern struct ").append(
207 serverTypeName.toString().replace('.', '_')).append("_exporteds ")
208 .append(serverInstanceName.replace('.', '_')).append(";").endl();
209 }
210 cw.append("#define ").append(serverInstanceName).append("_DECLARED")
211 .endl();
212 cw.appendln("#endif");
213
214 }
215 return cw.toString();
216 }
217 }
218
219
220
221
222
223
224
225
226
227
228
229 @ServerInterfaces(@ServerInterface(name = "source-code-provider", signature = SourceCodeProvider.class, record = "role:clientInterfaceInstantiation, id:%, codePiece:initialization", parameters = "componentNode"))
230 public class InitializationClientInstanceTask
231 extends
232 IncludeClientInstanceTask {
233
234
235
236
237
238
239
240
241 public InitializationClientInstanceTask(
242 final LinkedHashMap<TypeInterface, Interface> clientItfNodes) {
243 super(clientItfNodes);
244 }
245
246 @Override
247 protected String processSourceCode() throws Exception {
248 final CodeWriter cw = new CodeWriter(
249 "Client Interfaces Instantiation Builder (initialization)");
250 cw.append("{").endl();
251 for (final Map.Entry<TypeInterface, Interface> clientBinding : staticBindings
252 .entrySet()) {
253
254 final TypeInterface clientItf = clientBinding.getKey();
255 final Interface serverItf = clientBinding.getValue();
256 if (serverItf == null) {
257 cw.append("0");
258 } else {
259 final Object serverComponent = getContainer(serverItf);
260 final String serverTypeName = serverTypeNameProviderItf.get(
261 serverComponent).getCTypeName();
262 final String serverInstance = serverInstanceNameProviderItf.get(
263 serverComponent).getCInstanceName();
264
265 if (serverTypeName.equals(typeNameProviderItf.getCTypeName())) {
266
267
268
269 cw.append(
270 "(R" + clientItf.getSignature().replace('.', '_') + "*)&(")
271 .append(serverInstance.replace('.', '_')).append(
272 ".type.exported.").append(
273 serverItf.getName().replace('-', '_')).append(')');
274 } else {
275
276 cw.append(
277 "(R" + clientItf.getSignature().replace('.', '_') + "*)&(")
278 .append(serverInstance.replace('.', '_')).append(".")
279 .append(serverItf.getName().replace('-', '_')).append(')');
280 }
281 }
282 cw.append(", // ").append(clientItf.getName()).endl();
283
284 }
285 cw.append("}").endl();
286
287 return cw.toString();
288 }
289 }
290 }