1 | package de.uka.ipd.sdq.pcm.transformations.builder.seff; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Collection; |
5 | |
6 | import de.uka.ipd.sdq.pcm.parameter.VariableCharacterisationType; |
7 | import de.uka.ipd.sdq.pcm.parameter.VariableUsage; |
8 | import de.uka.ipd.sdq.pcm.repository.OperationInterface; |
9 | import de.uka.ipd.sdq.pcm.repository.OperationProvidedRole; |
10 | import de.uka.ipd.sdq.pcm.repository.OperationRequiredRole; |
11 | import de.uka.ipd.sdq.pcm.repository.OperationSignature; |
12 | import de.uka.ipd.sdq.pcm.repository.PrimitiveTypeEnum; |
13 | import de.uka.ipd.sdq.pcm.transformations.BytesizeComputationForSignature; |
14 | import de.uka.ipd.sdq.pcm.transformations.BytesizeComputationForSignature.Modifier; |
15 | import de.uka.ipd.sdq.pcm.transformations.builder.infrastructure.MarshallingComponentBuilder.MarshallerSide; |
16 | import de.uka.ipd.sdq.pcm.transformations.builder.util.NumberOfElementsComputationForSignature; |
17 | |
18 | public class MarshallerComponentSeffBuilder extends |
19 | MiddlewareComponentSeffBuilder { |
20 | |
21 | PrimitiveTypeEnum[] primitiveTypes = new PrimitiveTypeEnum[]{PrimitiveTypeEnum.INT}; |
22 | |
23 | private OperationSignature marshallSignature; |
24 | private OperationSignature demarshallSignature; |
25 | |
26 | public MarshallerComponentSeffBuilder(OperationProvidedRole domainProvRole, |
27 | OperationRequiredRole domainReqRole, OperationRequiredRole middlewareReqRole, MarshallerSide side) { |
28 | super(domainProvRole, domainReqRole, middlewareReqRole); |
29 | this.marshallSignature = findService(middlewareRole.getRequiredInterface__OperationRequiredRole(), "marshall"); |
30 | this.demarshallSignature = findService(middlewareRole.getRequiredInterface__OperationRequiredRole(), "demarshall"); |
31 | |
32 | switch(side) { |
33 | case CLIENT: |
34 | prepareClient(); |
35 | break; |
36 | case SERVER: |
37 | prepareServer(); |
38 | break; |
39 | } |
40 | } |
41 | |
42 | private void prepareClient() { |
43 | |
44 | this.appendPreAction( |
45 | new SignatureDependentExternalCallActionDescriptor( |
46 | marshallSignature, |
47 | this.middlewareRole){ |
48 | |
49 | @Override |
50 | protected Collection<VariableUsage> getSignatureDependentInputParameter( |
51 | OperationSignature currentSignature) { |
52 | ArrayList<VariableUsage> result = new ArrayList<VariableUsage>(); |
53 | for (PrimitiveTypeEnum type : primitiveTypes){ |
54 | VariableUsage usage = createInputParameterFor(currentSignature, type, BytesizeComputationForSignature.Modifier.IN); |
55 | if (usage != null) |
56 | result.add(usage); |
57 | } |
58 | return result; |
59 | } |
60 | |
61 | @Override |
62 | protected Collection<VariableUsage> getSignatureDependentOutputParameter( |
63 | OperationSignature currentSignature) { |
64 | // add size of input parameters to stream |
65 | return createMarshallOutParameter(currentSignature, Modifier.IN); |
66 | } |
67 | |
68 | }); |
69 | |
70 | this.appendPreAction(new BytesizeAddingSetVariableActionDescriptor( |
71 | createVariableUsage("stream", VariableCharacterisationType.BYTESIZE, "stream.BYTESIZE"),Modifier.IN)); |
72 | |
73 | |
74 | this.appendPostAction( |
75 | new ExternalCallActionDescriptor( |
76 | demarshallSignature, |
77 | this.middlewareRole, |
78 | createMiddlewareParameter())); |
79 | } |
80 | |
81 | private void prepareServer() { |
82 | this.appendPreAction( |
83 | new ExternalCallActionDescriptor( |
84 | demarshallSignature, |
85 | this.middlewareRole, |
86 | createMiddlewareParameter())); |
87 | this.appendPostAction( |
88 | new SignatureDependentExternalCallActionDescriptor( |
89 | marshallSignature, |
90 | this.middlewareRole){ |
91 | |
92 | @Override |
93 | protected Collection<VariableUsage> getSignatureDependentInputParameter( |
94 | OperationSignature currentSignature) { |
95 | ArrayList<VariableUsage> result = new ArrayList<VariableUsage>(); |
96 | for (PrimitiveTypeEnum type : primitiveTypes) { |
97 | VariableUsage usage = createInputParameterFor(currentSignature, type, BytesizeComputationForSignature.Modifier.OUT); |
98 | if (usage != null) |
99 | result.add(usage); |
100 | } |
101 | return result; |
102 | } |
103 | |
104 | @Override |
105 | protected Collection<VariableUsage> getSignatureDependentOutputParameter( |
106 | OperationSignature currentSignature) { |
107 | // add size of return parameters to stream |
108 | return createMarshallOutParameter(currentSignature, Modifier.OUT); |
109 | } |
110 | }); |
111 | this.appendPostAction(new BytesizeAddingSetVariableActionDescriptor( |
112 | createVariableUsage("stream", VariableCharacterisationType.BYTESIZE, "stream.BYTESIZE"),Modifier.OUT)); |
113 | } |
114 | |
115 | |
116 | |
117 | private OperationSignature findService(OperationInterface middlewareInterface, |
118 | String preServiceName) { |
119 | for (OperationSignature sig : middlewareInterface.getSignatures__OperationInterface()){ |
120 | if (sig.getEntityName().equals(preServiceName)) |
121 | return sig; |
122 | } |
123 | throw new RuntimeException("Required middleware service not found in middleware interface"); |
124 | } |
125 | |
126 | /** |
127 | * This method is responsible to determine the size of the stream that has to be sent over the network |
128 | * after marshalling (i.e. the marshall out data). |
129 | * @param currentSignature |
130 | * @return |
131 | */ |
132 | private Collection<VariableUsage> createMarshallOutParameter(OperationSignature currentSignature, Modifier modifier) { |
133 | Collection<VariableUsage> result = new ArrayList<VariableUsage>(); |
134 | |
135 | result.add(createVariableUsage("stream", VariableCharacterisationType.BYTESIZE, "RETURN.BYTESIZE")); |
136 | |
137 | |
138 | return result; |
139 | } |
140 | |
141 | /** |
142 | * This method is responsible for creating the variable characterizations ints.NUMBER_OF_ELEMENTS that appear in the simulation code of connector completions. |
143 | * This data is input to the middleware component configured in the models, and can be used by |
144 | * the middleware component to calculate the size of the passed stream. |
145 | * For example, in the default Glassfish component, ints.NUMBER_OF_ELEMENTS is then transformed to a BYTESIZE characterisation |
146 | * with 2 bytes for each int (see default glassfish model). |
147 | * |
148 | * |
149 | * @param currentSignature |
150 | * @param type |
151 | * @param in |
152 | * @return |
153 | */ |
154 | protected VariableUsage createInputParameterFor(OperationSignature currentSignature, |
155 | PrimitiveTypeEnum type, Modifier in) { |
156 | String specification = NumberOfElementsComputationForSignature.countAmount(currentSignature,type,in); |
157 | |
158 | if (specification != null) |
159 | return createVariableUsage(type.getLiteral().toLowerCase()+"s", VariableCharacterisationType.NUMBER_OF_ELEMENTS, |
160 | specification); |
161 | else |
162 | return createVariableUsage(type.getLiteral().toLowerCase()+"s", VariableCharacterisationType.NUMBER_OF_ELEMENTS, |
163 | "0"); |
164 | } |
165 | |
166 | |
167 | } |