OLD | NEW |
| (Empty) |
1 {%- for method in interface.methods %} | |
2 var k{{interface.name}}_{{method.name}}_Name = {{method.ordinal}}; | |
3 {%- endfor %} | |
4 | |
5 function {{interface.name}}Proxy(receiver) { | |
6 bindings.ProxyBase.call(this, receiver); | |
7 } | |
8 {{interface.name}}Proxy.prototype = Object.create(bindings.ProxyBase.prototype
); | |
9 | |
10 {%- for method in interface.methods %} | |
11 {{interface.name}}Proxy.prototype.{{method.name|stylize_method}} = function( | |
12 {%- for parameter in method.parameters -%} | |
13 {{parameter.name}}{% if not loop.last %}, {% endif %} | |
14 {%- endfor -%} | |
15 ) { | |
16 var params = new {{interface.name}}_{{method.name}}_Params(); | |
17 {%- for parameter in method.parameters %} | |
18 params.{{parameter.name}} = {{parameter|js_proxy_method_parameter_value}}; | |
19 {%- endfor %} | |
20 | |
21 {%- if method.response_parameters == None %} | |
22 var builder = new codec.MessageBuilder( | |
23 k{{interface.name}}_{{method.name}}_Name, | |
24 codec.align({{interface.name}}_{{method.name}}_Params.encodedSize)); | |
25 builder.encodeStruct({{interface.name}}_{{method.name}}_Params, params); | |
26 var message = builder.finish(); | |
27 this.receiver_.accept(message); | |
28 {%- else %} | |
29 return new Promise(function(resolve, reject) { | |
30 var builder = new codec.MessageWithRequestIDBuilder( | |
31 k{{interface.name}}_{{method.name}}_Name, | |
32 codec.align({{interface.name}}_{{method.name}}_Params.encodedSize), | |
33 codec.kMessageExpectsResponse, 0); | |
34 builder.encodeStruct({{interface.name}}_{{method.name}}_Params, params); | |
35 var message = builder.finish(); | |
36 this.receiver_.acceptAndExpectResponse(message).then(function(message) { | |
37 var reader = new codec.MessageReader(message); | |
38 var responseParams = | |
39 reader.decodeStruct({{interface.name}}_{{method.name}}_ResponseParam
s); | |
40 resolve(responseParams); | |
41 }).catch(function(result) { | |
42 reject(Error("Connection error: " + result)); | |
43 }); | |
44 }.bind(this)); | |
45 {%- endif %} | |
46 }; | |
47 {%- endfor %} | |
48 | |
49 function {{interface.name}}Stub(delegate) { | |
50 bindings.StubBase.call(this, delegate); | |
51 } | |
52 {{interface.name}}Stub.prototype = Object.create(bindings.StubBase.prototype); | |
53 | |
54 {%- for method in interface.methods %} | |
55 {%- set js_method_name = method.name|stylize_method %} | |
56 {%- set delegate_expr = "bindings.StubBindings(this).delegate" %} | |
57 {{interface.name}}Stub.prototype.{{js_method_name}} = function({{method.parame
ters|map(attribute='name')|join(', ')}}) { | |
58 return {{delegate_expr}} && {{delegate_expr}}.{{js_method_name}} && {{delega
te_expr}}.{{js_method_name}}({{method.parameters|map('js_stub_method_parameter_v
alue')|join(', ')}}); | |
59 } | |
60 {%- endfor %} | |
61 | |
62 {{interface.name}}Stub.prototype.accept = function(message) { | |
63 var reader = new codec.MessageReader(message); | |
64 switch (reader.messageName) { | |
65 {%- for method in interface.methods %} | |
66 {%- if method.response_parameters == None %} | |
67 case k{{interface.name}}_{{method.name}}_Name: | |
68 var params = reader.decodeStruct({{interface.name}}_{{method.name}}_Params
); | |
69 this.{{method.name|stylize_method}}( | |
70 {%- for parameter in method.parameters -%} | |
71 params.{{parameter.name}}{% if not loop.last %}, {% endif %} | |
72 {%- endfor %}); | |
73 return true; | |
74 {%- endif %} | |
75 {%- endfor %} | |
76 default: | |
77 return false; | |
78 } | |
79 }; | |
80 | |
81 {{interface.name}}Stub.prototype.acceptWithResponder = | |
82 function(message, responder) { | |
83 var reader = new codec.MessageReader(message); | |
84 switch (reader.messageName) { | |
85 {%- for method in interface.methods %} | |
86 {%- if method.response_parameters != None %} | |
87 case k{{interface.name}}_{{method.name}}_Name: | |
88 var params = reader.decodeStruct({{interface.name}}_{{method.name}}_Params
); | |
89 return this.{{method.name|stylize_method}}( | |
90 {%- for parameter in method.parameters -%} | |
91 params.{{parameter.name}}{% if not loop.last %}, {% endif -%} | |
92 {%- endfor %}).then(function(response) { | |
93 var responseParams = | |
94 new {{interface.name}}_{{method.name}}_ResponseParams(); | |
95 {%- for parameter in method.response_parameters %} | |
96 responseParams.{{parameter.name}} = response.{{parameter.name}}; | |
97 {%- endfor %} | |
98 var builder = new codec.MessageWithRequestIDBuilder( | |
99 k{{interface.name}}_{{method.name}}_Name, | |
100 codec.align({{interface.name}}_{{method.name}}_ResponseParams.encode
dSize), | |
101 codec.kMessageIsResponse, reader.requestID); | |
102 builder.encodeStruct({{interface.name}}_{{method.name}}_ResponseParams, | |
103 responseParams); | |
104 var message = builder.finish(); | |
105 responder.accept(message); | |
106 }); | |
107 {%- endif %} | |
108 {%- endfor %} | |
109 default: | |
110 return Promise.reject(Error("Unhandled message: " + reader.messageName)); | |
111 } | |
112 }; | |
113 | |
114 {#--- Validation #} | |
115 | |
116 function validate{{interface.name}}Request(messageValidator) { | |
117 {%- if not(interface.methods) %} | |
118 return validator.validationError.NONE; | |
119 {%- else %} | |
120 var message = messageValidator.message; | |
121 var paramsClass = null; | |
122 switch (message.getName()) { | |
123 {%- for method in interface.methods %} | |
124 case k{{interface.name}}_{{method.name}}_Name: | |
125 {%- if method.response_parameters == None %} | |
126 if (!message.expectsResponse() && !message.isResponse()) | |
127 paramsClass = {{interface.name}}_{{method.name}}_Params; | |
128 {%- else %} | |
129 if (message.expectsResponse()) | |
130 paramsClass = {{interface.name}}_{{method.name}}_Params; | |
131 {%- endif %} | |
132 break; | |
133 {%- endfor %} | |
134 } | |
135 if (paramsClass === null) | |
136 return validator.validationError.NONE; | |
137 return paramsClass.validate(messageValidator, messageValidator.message.getHe
aderNumBytes()); | |
138 {%- endif %} | |
139 } | |
140 | |
141 function validate{{interface.name}}Response(messageValidator) { | |
142 {%- if not(interface|has_callbacks) %} | |
143 return validator.validationError.NONE; | |
144 {%- else %} | |
145 var message = messageValidator.message; | |
146 var paramsClass = null; | |
147 switch (message.getName()) { | |
148 {%- for method in interface.methods %} | |
149 {%- if method.response_parameters != None %} | |
150 case k{{interface.name}}_{{method.name}}_Name: | |
151 if (message.isResponse()) | |
152 paramsClass = {{interface.name}}_{{method.name}}_ResponseParams; | |
153 break; | |
154 {%- endif %} | |
155 {%- endfor %} | |
156 } | |
157 if (paramsClass === null) | |
158 return validator.validationError.NONE; | |
159 return paramsClass.validate(messageValidator, messageValidator.message.getHe
aderNumBytes()); | |
160 {%- endif %} | |
161 } | |
162 | |
163 var {{interface.name}} = { | |
164 name: '{{namespace|replace(".","::")}}::{{interface.name}}', | |
165 proxyClass: {{interface.name}}Proxy, | |
166 stubClass: {{interface.name}}Stub, | |
167 validateRequest: validate{{interface.name}}Request, | |
168 {%- if interface|has_callbacks %} | |
169 validateResponse: validate{{interface.name}}Response, | |
170 {%- else %} | |
171 validateResponse: null, | |
172 {%- endif %} | |
173 }; | |
174 {#--- Interface Constants #} | |
175 {%- for constant in interface.constants %} | |
176 {{interface.name}}.{{constant.name}} = {{constant.value|expression_to_text}}, | |
177 {%- endfor %} | |
178 {#--- Interface Enums #} | |
179 {%- from "enum_definition.tmpl" import enum_def -%} | |
180 {%- for enum in interface.enums %} | |
181 {{ enum_def("%s.%s"|format(interface.name, enum.name), enum) }} | |
182 {%- endfor %} | |
183 {{interface.name}}Stub.prototype.validator = validate{{interface.name}}Request
; | |
184 {%- if interface|has_callbacks %} | |
185 {{interface.name}}Proxy.prototype.validator = validate{{interface.name}}Respon
se; | |
186 {%- else %} | |
187 {{interface.name}}Proxy.prototype.validator = null; | |
188 {%- endif %} | |
OLD | NEW |