1 {%- for method in interface.methods %}
2 var k{{interface.name}}_{{method.name}}_Name = {{method.ordinal}};
5 function {{interface.name}}Proxy(receiver) {
6 connection.initProxyInstance(this, {{interface.name}}, receiver);
9 {{interface.name}}Proxy.prototype.getConnection$ = function() {
10 return connection.getProxyConnection(this, {{interface.name}});
13 {%- for method in interface.methods %}
14 {{interface.name}}Proxy.prototype.{{method.name|stylize_method}} = function(
15 {%- for parameter in method.parameters -%}
16 {{parameter.name}}{% if not loop.last %}, {% endif %}
19 var params = new {{interface.name}}_{{method.name}}_Params();
20 {%- for parameter in method.parameters %}
21 {%- if parameter|is_interface_parameter %}
22 if ({{parameter.name}} instanceof {{parameter.kind|js_type}}.stubClass)
23 params.{{parameter.name}} = {{parameter.name}}.getConnection$().messagePipeHandle;
25 params.{{parameter.name}} = {{parameter.name}};
26 {%- elif parameter|is_interface_request_parameter %}
27 if ({{parameter.name}} instanceof {{parameter.kind.kind|js_type}}.proxyClass)
28 params.{{parameter.name}} = {{parameter.name}}.getConnection$().messagePipeHandle;
30 params.{{parameter.name}} = {{parameter.name}};
32 params.{{parameter.name}} = {{parameter.name}};
36 {%- if method.response_parameters == None %}
37 var builder = new codec.MessageBuilder(
38 k{{interface.name}}_{{method.name}}_Name,
39 codec.align({{interface.name}}_{{method.name}}_Params.encodedSize));
40 builder.encodeStruct({{interface.name}}_{{method.name}}_Params, params);
41 var message = builder.finish();
42 this.receiver_.accept(message);
44 return new Promise(function(resolve, reject) {
45 var builder = new codec.MessageWithRequestIDBuilder(
46 k{{interface.name}}_{{method.name}}_Name,
47 codec.align({{interface.name}}_{{method.name}}_Params.encodedSize),
48 codec.kMessageExpectsResponse, 0);
49 builder.encodeStruct({{interface.name}}_{{method.name}}_Params, params);
50 var message = builder.finish();
51 this.receiver_.acceptAndExpectResponse(message).then(function(message) {
52 var reader = new codec.MessageReader(message);
54 reader.decodeStruct({{interface.name}}_{{method.name}}_ResponseParams);
55 resolve(responseParams);
56 }).catch(function(result) {
57 reject(Error("Connection error: " + result));
64 function {{interface.name}}Stub(delegate) {
65 this.delegate$ = delegate;
68 {{interface.name}}Stub.prototype.getConnection$ = function() {
69 return connection.getStubConnection(this, {{interface.name}});
72 {%- for method in interface.methods %}
73 {% macro stub_method_parameters() -%}
74 {%- for parameter in method.parameters -%}
75 {{parameter.name}}{% if not loop.last %}, {% endif %}
78 {{interface.name}}Stub.prototype.{{method.name|stylize_method}} = function({{stub_method_parameters()}}) {
79 if (this.delegate$.{{method.name|stylize_method}})
80 return this.delegate$.{{method.name|stylize_method}}({{stub_method_parameters()}});
84 {{interface.name}}Stub.prototype.accept = function(message) {
85 var reader = new codec.MessageReader(message);
86 switch (reader.messageName) {
87 {%- for method in interface.methods %}
88 {%- if method.response_parameters == None %}
89 case k{{interface.name}}_{{method.name}}_Name:
90 var params = reader.decodeStruct({{interface.name}}_{{method.name}}_Params);
91 this.{{method.name|stylize_method}}(
92 {%- for parameter in method.parameters -%}
93 params.{{parameter.name}}{% if not loop.last %}, {% endif %}
103 {{interface.name}}Stub.prototype.acceptWithResponder =
104 function(message, responder) {
105 var reader = new codec.MessageReader(message);
106 switch (reader.messageName) {
107 {%- for method in interface.methods %}
108 {%- if method.response_parameters != None %}
109 case k{{interface.name}}_{{method.name}}_Name:
110 var params = reader.decodeStruct({{interface.name}}_{{method.name}}_Params);
111 return this.{{method.name|stylize_method}}(
112 {%- for parameter in method.parameters -%}
113 params.{{parameter.name}}{% if not loop.last %}, {% endif -%}
114 {%- endfor %}).then(function(response) {
116 new {{interface.name}}_{{method.name}}_ResponseParams();
117 {%- for parameter in method.response_parameters %}
118 responseParams.{{parameter.name}} = response.{{parameter.name}};
120 var builder = new codec.MessageWithRequestIDBuilder(
121 k{{interface.name}}_{{method.name}}_Name,
122 codec.align({{interface.name}}_{{method.name}}_ResponseParams.encodedSize),
123 codec.kMessageIsResponse, reader.requestID);
124 builder.encodeStruct({{interface.name}}_{{method.name}}_ResponseParams,
126 var message = builder.finish();
127 responder.accept(message);
132 return Promise.reject(Error("Unhandled message: " + reader.messageName));
138 function validate{{interface.name}}Request(messageValidator) {
139 {%- if not(interface.methods) %}
140 return validator.validationError.NONE;
142 var message = messageValidator.message;
143 var paramsClass = null;
144 switch (message.getName()) {
145 {%- for method in interface.methods %}
146 case k{{interface.name}}_{{method.name}}_Name:
147 {%- if method.response_parameters == None %}
148 if (!message.expectsResponse() && !message.isResponse())
149 paramsClass = {{interface.name}}_{{method.name}}_Params;
151 if (message.expectsResponse())
152 paramsClass = {{interface.name}}_{{method.name}}_Params;
157 if (paramsClass === null)
158 return validator.validationError.NONE;
159 return paramsClass.validate(messageValidator, messageValidator.message.getHeaderNumBytes());
163 function validate{{interface.name}}Response(messageValidator) {
164 {%- if not(interface|has_callbacks) %}
165 return validator.validationError.NONE;
167 var message = messageValidator.message;
168 var paramsClass = null;
169 switch (message.getName()) {
170 {%- for method in interface.methods %}
171 {%- if method.response_parameters != None %}
172 case k{{interface.name}}_{{method.name}}_Name:
173 if (message.isResponse())
174 paramsClass = {{interface.name}}_{{method.name}}_ResponseParams;
179 if (paramsClass === null)
180 return validator.validationError.NONE;
181 return paramsClass.validate(messageValidator, messageValidator.message.getHeaderNumBytes());
185 var {{interface.name}} = {
186 name: '{{namespace|replace(".","::")}}::{{interface.name}}',
187 proxyClass: {{interface.name}}Proxy,
188 stubClass: {{interface.name}}Stub,
189 validateRequest: validate{{interface.name}}Request,
190 {%- if interface|has_callbacks %}
191 validateResponse: validate{{interface.name}}Response,
193 validateResponse: null,
196 {#--- Interface Constants #}
197 {%- for constant in interface.constants %}
198 {{interface.name}}.{{constant.name}} = {{constant.value|expression_to_text}},
200 {#--- Interface Enums #}
201 {%- from "enum_definition.tmpl" import enum_def -%}
202 {%- for enum in interface.enums %}
203 {{ enum_def("%s.%s"|format(interface.name, enum.name), enum) }}
205 {{interface.name}}Stub.prototype.validator = validate{{interface.name}}Request;
206 {%- if interface|has_callbacks %}
207 {{interface.name}}Proxy.prototype.validator = validate{{interface.name}}Response;
209 {{interface.name}}Proxy.prototype.validator = null;