| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 1 | #include "generate_java.h" |
| 2 | #include "Type.h" |
| 3 | #include <string.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| Manuel Roman | b71287f | 2011-10-06 10:28:35 -0700 | [diff] [blame] | 8 | Type* SERVICE_CONTEXT_TYPE = new Type("android.content", |
| 9 | "Context", Type::BUILT_IN, false, false, false); |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 10 | Type* PRESENTER_BASE_TYPE = new Type("com.android.athome.connector", |
| 11 | "EventListener", Type::BUILT_IN, false, false, false); |
| 12 | Type* PRESENTER_LISTENER_BASE_TYPE = new Type("com.android.athome.connector", |
| 13 | "EventListener.Listener", Type::BUILT_IN, false, false, false); |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 14 | Type* RPC_BROKER_TYPE = new Type("com.android.athome.connector", "Broker", |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 15 | Type::BUILT_IN, false, false, false); |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 16 | Type* RPC_CONTAINER_TYPE = new Type("com.android.athome.connector", "ConnectorContainer", |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 17 | Type::BUILT_IN, false, false, false); |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 18 | // TODO: Just use Endpoint, so this works for all endpoints. |
| 19 | Type* RPC_CONNECTOR_TYPE = new Type("com.android.athome.connector", "Connector", |
| 20 | Type::BUILT_IN, false, false, false); |
| Joe Onorato | a1c6d90 | 2011-10-09 22:31:16 -0700 | [diff] [blame] | 21 | Type* RPC_ENDPOINT_INFO_TYPE = new UserDataType("com.android.athome.rpc", |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 22 | "EndpointInfo", true, __FILE__, __LINE__); |
| Joe Onorato | a1c6d90 | 2011-10-09 22:31:16 -0700 | [diff] [blame] | 23 | Type* RPC_RESULT_HANDLER_TYPE = new UserDataType("com.android.athome.rpc", "RpcResultHandler", |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 24 | true, __FILE__, __LINE__); |
| 25 | Type* RPC_ERROR_LISTENER_TYPE = new Type("com.android.athome.rpc", "RpcErrorHandler", |
| 26 | Type::BUILT_IN, false, false, false); |
| Joe Onorato | a1c6d90 | 2011-10-09 22:31:16 -0700 | [diff] [blame] | 27 | Type* RPC_CONTEXT_TYPE = new UserDataType("com.android.athome.rpc", "RpcContext", true, |
| Manuel Roman | b71287f | 2011-10-06 10:28:35 -0700 | [diff] [blame] | 28 | __FILE__, __LINE__); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 29 | |
| 30 | static void generate_create_from_data(Type* t, StatementBlock* addTo, const string& key, |
| 31 | Variable* v, Variable* data, Variable** cl); |
| 32 | static void generate_new_array(Type* t, StatementBlock* addTo, Variable* v, Variable* from); |
| 33 | static void generate_write_to_data(Type* t, StatementBlock* addTo, Expression* k, Variable* v, |
| 34 | Variable* data); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 35 | |
| 36 | static string |
| 37 | format_int(int n) |
| 38 | { |
| 39 | char str[20]; |
| 40 | sprintf(str, "%d", n); |
| 41 | return string(str); |
| 42 | } |
| 43 | |
| 44 | static string |
| 45 | class_name_leaf(const string& str) |
| 46 | { |
| 47 | string::size_type pos = str.rfind('.'); |
| 48 | if (pos == string::npos) { |
| 49 | return str; |
| 50 | } else { |
| 51 | return string(str, pos+1); |
| 52 | } |
| 53 | } |
| 54 | |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 55 | static string |
| 56 | results_class_name(const string& n) |
| 57 | { |
| 58 | string str = n; |
| 59 | str[0] = toupper(str[0]); |
| 60 | str.insert(0, "On"); |
| 61 | return str; |
| 62 | } |
| 63 | |
| 64 | static string |
| 65 | results_method_name(const string& n) |
| 66 | { |
| 67 | string str = n; |
| 68 | str[0] = toupper(str[0]); |
| 69 | str.insert(0, "on"); |
| 70 | return str; |
| 71 | } |
| 72 | |
| 73 | static string |
| 74 | push_method_name(const string& n) |
| 75 | { |
| 76 | string str = n; |
| 77 | str[0] = toupper(str[0]); |
| 78 | str.insert(0, "push"); |
| 79 | return str; |
| 80 | } |
| 81 | |
| 82 | // ================================================= |
| 83 | class DispatcherClass : public Class |
| 84 | { |
| 85 | public: |
| 86 | DispatcherClass(const interface_type* iface, Expression* target); |
| 87 | virtual ~DispatcherClass(); |
| 88 | |
| 89 | void AddMethod(const method_type* method); |
| 90 | void DoneWithMethods(); |
| 91 | |
| 92 | Method* processMethod; |
| 93 | Variable* actionParam; |
| 94 | Variable* requestParam; |
| Manuel Roman | b71287f | 2011-10-06 10:28:35 -0700 | [diff] [blame] | 95 | Variable* rpcContextParam; |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 96 | Variable* errorParam; |
| 97 | Variable* requestData; |
| 98 | Variable* resultData; |
| 99 | IfStatement* dispatchIfStatement; |
| 100 | Expression* targetExpression; |
| 101 | |
| 102 | private: |
| 103 | void generate_process(); |
| 104 | }; |
| 105 | |
| 106 | DispatcherClass::DispatcherClass(const interface_type* iface, Expression* target) |
| 107 | :Class(), |
| 108 | dispatchIfStatement(NULL), |
| 109 | targetExpression(target) |
| 110 | { |
| 111 | generate_process(); |
| 112 | } |
| 113 | |
| 114 | DispatcherClass::~DispatcherClass() |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | DispatcherClass::generate_process() |
| 120 | { |
| Manuel Roman | b71287f | 2011-10-06 10:28:35 -0700 | [diff] [blame] | 121 | // byte[] process(String action, byte[] params, RpcContext context, RpcError status) |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 122 | this->processMethod = new Method; |
| 123 | this->processMethod->modifiers = PUBLIC; |
| 124 | this->processMethod->returnType = BYTE_TYPE; |
| 125 | this->processMethod->returnTypeDimension = 1; |
| 126 | this->processMethod->name = "process"; |
| 127 | this->processMethod->statements = new StatementBlock; |
| 128 | |
| 129 | this->actionParam = new Variable(STRING_TYPE, "action"); |
| 130 | this->processMethod->parameters.push_back(this->actionParam); |
| 131 | |
| 132 | this->requestParam = new Variable(BYTE_TYPE, "requestParam", 1); |
| 133 | this->processMethod->parameters.push_back(this->requestParam); |
| 134 | |
| Manuel Roman | b71287f | 2011-10-06 10:28:35 -0700 | [diff] [blame] | 135 | this->rpcContextParam = new Variable(RPC_CONTEXT_TYPE, "context", 0); |
| 136 | this->processMethod->parameters.push_back(this->rpcContextParam); |
| 137 | |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 138 | this->errorParam = new Variable(RPC_ERROR_TYPE, "errorParam", 0); |
| 139 | this->processMethod->parameters.push_back(this->errorParam); |
| 140 | |
| 141 | this->requestData = new Variable(RPC_DATA_TYPE, "request"); |
| 142 | this->processMethod->statements->Add(new VariableDeclaration(requestData, |
| 143 | new NewExpression(RPC_DATA_TYPE, 1, this->requestParam))); |
| 144 | |
| 145 | this->resultData = new Variable(RPC_DATA_TYPE, "resultData"); |
| 146 | this->processMethod->statements->Add(new VariableDeclaration(this->resultData, |
| 147 | NULL_VALUE)); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | DispatcherClass::AddMethod(const method_type* method) |
| 152 | { |
| 153 | arg_type* arg; |
| 154 | |
| 155 | // The if/switch statement |
| 156 | IfStatement* ifs = new IfStatement(); |
| 157 | ifs->expression = new MethodCall(new StringLiteralExpression(method->name.data), "equals", |
| 158 | 1, this->actionParam); |
| 159 | StatementBlock* block = ifs->statements = new StatementBlock; |
| 160 | if (this->dispatchIfStatement == NULL) { |
| 161 | this->dispatchIfStatement = ifs; |
| 162 | this->processMethod->statements->Add(dispatchIfStatement); |
| 163 | } else { |
| 164 | this->dispatchIfStatement->elseif = ifs; |
| 165 | this->dispatchIfStatement = ifs; |
| 166 | } |
| 167 | |
| 168 | // The call to decl (from above) |
| 169 | MethodCall* realCall = new MethodCall(this->targetExpression, method->name.data); |
| 170 | |
| 171 | // args |
| 172 | Variable* classLoader = NULL; |
| 173 | VariableFactory stubArgs("_arg"); |
| 174 | arg = method->args; |
| 175 | while (arg != NULL) { |
| 176 | Type* t = NAMES.Search(arg->type.type.data); |
| 177 | Variable* v = stubArgs.Get(t); |
| 178 | v->dimension = arg->type.dimension; |
| 179 | |
| 180 | // Unmarshall the parameter |
| 181 | block->Add(new VariableDeclaration(v)); |
| 182 | if (convert_direction(arg->direction.data) & IN_PARAMETER) { |
| 183 | generate_create_from_data(t, block, arg->name.data, v, |
| 184 | this->requestData, &classLoader); |
| 185 | } else { |
| 186 | if (arg->type.dimension == 0) { |
| 187 | block->Add(new Assignment(v, new NewExpression(v->type))); |
| 188 | } |
| 189 | else if (arg->type.dimension == 1) { |
| 190 | generate_new_array(v->type, block, v, this->requestData); |
| 191 | } |
| 192 | else { |
| 193 | fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, |
| 194 | __LINE__); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Add that parameter to the method call |
| 199 | realCall->arguments.push_back(v); |
| 200 | |
| 201 | arg = arg->next; |
| 202 | } |
| 203 | |
| Manuel Roman | b71287f | 2011-10-06 10:28:35 -0700 | [diff] [blame] | 204 | // Add a final parameter: RpcContext. Contains data about |
| 205 | // incoming request (e.g., certificate) |
| 206 | realCall->arguments.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0)); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 207 | |
| 208 | Type* returnType = NAMES.Search(method->type.type.data); |
| 209 | if (returnType == EVENT_FAKE_TYPE) { |
| 210 | returnType = VOID_TYPE; |
| 211 | } |
| 212 | |
| 213 | // the real call |
| 214 | bool first = true; |
| 215 | Variable* _result = NULL; |
| 216 | if (returnType == VOID_TYPE) { |
| 217 | block->Add(realCall); |
| 218 | } else { |
| 219 | _result = new Variable(returnType, "_result", |
| 220 | method->type.dimension); |
| 221 | block->Add(new VariableDeclaration(_result, realCall)); |
| 222 | |
| 223 | // need the result RpcData |
| 224 | if (first) { |
| 225 | block->Add(new Assignment(this->resultData, |
| 226 | new NewExpression(RPC_DATA_TYPE))); |
| 227 | first = false; |
| 228 | } |
| 229 | |
| 230 | // marshall the return value |
| 231 | generate_write_to_data(returnType, block, |
| 232 | new StringLiteralExpression("_result"), _result, this->resultData); |
| 233 | } |
| 234 | |
| 235 | // out parameters |
| 236 | int i = 0; |
| 237 | arg = method->args; |
| 238 | while (arg != NULL) { |
| 239 | Type* t = NAMES.Search(arg->type.type.data); |
| 240 | Variable* v = stubArgs.Get(i++); |
| 241 | |
| 242 | if (convert_direction(arg->direction.data) & OUT_PARAMETER) { |
| 243 | // need the result RpcData |
| 244 | if (first) { |
| 245 | block->Add(new Assignment(this->resultData, new NewExpression(RPC_DATA_TYPE))); |
| 246 | first = false; |
| 247 | } |
| 248 | |
| 249 | generate_write_to_data(t, block, new StringLiteralExpression(arg->name.data), |
| 250 | v, this->resultData); |
| 251 | } |
| 252 | |
| 253 | arg = arg->next; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void |
| 258 | DispatcherClass::DoneWithMethods() |
| 259 | { |
| 260 | if (this->dispatchIfStatement == NULL) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | this->elements.push_back(this->processMethod); |
| 265 | |
| 266 | IfStatement* fallthrough = new IfStatement(); |
| 267 | fallthrough->statements = new StatementBlock; |
| 268 | fallthrough->statements->Add(new ReturnStatement( |
| Manuel Roman | b71287f | 2011-10-06 10:28:35 -0700 | [diff] [blame] | 269 | new MethodCall(SUPER_VALUE, "process", 4, |
| 270 | this->actionParam, this->requestParam, |
| 271 | this->rpcContextParam, |
| 272 | this->errorParam))); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 273 | this->dispatchIfStatement->elseif = fallthrough; |
| 274 | IfStatement* s = new IfStatement; |
| 275 | s->statements = new StatementBlock; |
| 276 | this->processMethod->statements->Add(s); |
| 277 | s->expression = new Comparison(this->resultData, "!=", NULL_VALUE); |
| 278 | s->statements->Add(new ReturnStatement(new MethodCall(this->resultData, "serialize"))); |
| 279 | s->elseif = new IfStatement; |
| 280 | s = s->elseif; |
| 281 | s->statements->Add(new ReturnStatement(NULL_VALUE)); |
| 282 | } |
| 283 | |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 284 | // ================================================= |
| 285 | class RpcProxyClass : public Class |
| 286 | { |
| 287 | public: |
| 288 | RpcProxyClass(const interface_type* iface, InterfaceType* interfaceType); |
| 289 | virtual ~RpcProxyClass(); |
| 290 | |
| 291 | Variable* endpoint; |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 292 | Variable* broker; |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 293 | |
| 294 | private: |
| 295 | void generate_ctor(); |
| Jason Simmons | 7dc2973 | 2011-12-16 16:14:17 -0800 | [diff] [blame^] | 296 | void generate_get_endpoint_info(); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | RpcProxyClass::RpcProxyClass(const interface_type* iface, InterfaceType* interfaceType) |
| 300 | :Class() |
| 301 | { |
| 302 | this->comment = gather_comments(iface->comments_token->extra); |
| 303 | this->modifiers = PUBLIC; |
| 304 | this->what = Class::CLASS; |
| 305 | this->type = interfaceType; |
| 306 | |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 307 | // broker |
| 308 | this->broker = new Variable(RPC_BROKER_TYPE, "_broker"); |
| 309 | this->elements.push_back(new Field(PRIVATE, this->broker)); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 310 | // endpoint |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 311 | this->endpoint = new Variable(RPC_ENDPOINT_INFO_TYPE, "_endpoint"); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 312 | this->elements.push_back(new Field(PRIVATE, this->endpoint)); |
| 313 | |
| 314 | // methods |
| 315 | generate_ctor(); |
| Jason Simmons | 7dc2973 | 2011-12-16 16:14:17 -0800 | [diff] [blame^] | 316 | generate_get_endpoint_info(); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | RpcProxyClass::~RpcProxyClass() |
| 320 | { |
| 321 | } |
| 322 | |
| 323 | void |
| 324 | RpcProxyClass::generate_ctor() |
| 325 | { |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 326 | Variable* broker = new Variable(RPC_BROKER_TYPE, "broker"); |
| 327 | Variable* endpoint = new Variable(RPC_ENDPOINT_INFO_TYPE, "endpoint"); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 328 | Method* ctor = new Method; |
| 329 | ctor->modifiers = PUBLIC; |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 330 | ctor->name = class_name_leaf(this->type->Name()); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 331 | ctor->statements = new StatementBlock; |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 332 | ctor->parameters.push_back(broker); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 333 | ctor->parameters.push_back(endpoint); |
| 334 | this->elements.push_back(ctor); |
| 335 | |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 336 | ctor->statements->Add(new Assignment(this->broker, broker)); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 337 | ctor->statements->Add(new Assignment(this->endpoint, endpoint)); |
| 338 | } |
| 339 | |
| Jason Simmons | 7dc2973 | 2011-12-16 16:14:17 -0800 | [diff] [blame^] | 340 | void |
| 341 | RpcProxyClass::generate_get_endpoint_info() |
| 342 | { |
| 343 | Method* get = new Method; |
| 344 | get->modifiers = PUBLIC; |
| 345 | get->returnType = RPC_ENDPOINT_INFO_TYPE; |
| 346 | get->name = "getEndpointInfo"; |
| 347 | get->statements = new StatementBlock; |
| 348 | this->elements.push_back(get); |
| 349 | |
| 350 | get->statements->Add(new ReturnStatement(this->endpoint)); |
| 351 | } |
| 352 | |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 353 | // ================================================= |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 354 | class EventListenerClass : public DispatcherClass |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 355 | { |
| 356 | public: |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 357 | EventListenerClass(const interface_type* iface, Type* listenerType); |
| 358 | virtual ~EventListenerClass(); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 359 | |
| 360 | Variable* _listener; |
| 361 | |
| 362 | private: |
| 363 | void generate_ctor(); |
| 364 | }; |
| 365 | |
| 366 | Expression* |
| 367 | generate_get_listener_expression(Type* cast) |
| 368 | { |
| 369 | return new Cast(cast, new MethodCall(THIS_VALUE, "getView")); |
| 370 | } |
| 371 | |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 372 | EventListenerClass::EventListenerClass(const interface_type* iface, Type* listenerType) |
| 373 | :DispatcherClass(iface, new FieldVariable(THIS_VALUE, "_listener")) |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 374 | { |
| 375 | this->modifiers = PRIVATE; |
| 376 | this->what = Class::CLASS; |
| 377 | this->type = new Type(iface->package ? iface->package : "", |
| 378 | append(iface->name.data, ".Presenter"), |
| 379 | Type::GENERATED, false, false, false); |
| 380 | this->extends = PRESENTER_BASE_TYPE; |
| 381 | |
| 382 | this->_listener = new Variable(listenerType, "_listener"); |
| Tim Kilbourn | 22a7cb8 | 2011-09-27 10:30:53 -0700 | [diff] [blame] | 383 | this->elements.push_back(new Field(PRIVATE, this->_listener)); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 384 | |
| 385 | // methods |
| 386 | generate_ctor(); |
| 387 | } |
| 388 | |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 389 | EventListenerClass::~EventListenerClass() |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 390 | { |
| 391 | } |
| 392 | |
| 393 | void |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 394 | EventListenerClass::generate_ctor() |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 395 | { |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 396 | Variable* broker = new Variable(RPC_BROKER_TYPE, "broker"); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 397 | Variable* listener = new Variable(this->_listener->type, "listener"); |
| 398 | Method* ctor = new Method; |
| 399 | ctor->modifiers = PUBLIC; |
| 400 | ctor->name = class_name_leaf(this->type->Name()); |
| 401 | ctor->statements = new StatementBlock; |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 402 | ctor->parameters.push_back(broker); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 403 | ctor->parameters.push_back(listener); |
| 404 | this->elements.push_back(ctor); |
| 405 | |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 406 | ctor->statements->Add(new MethodCall("super", 2, broker, listener)); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 407 | ctor->statements->Add(new Assignment(this->_listener, listener)); |
| 408 | } |
| 409 | |
| 410 | // ================================================= |
| 411 | class ListenerClass : public Class |
| 412 | { |
| 413 | public: |
| 414 | ListenerClass(const interface_type* iface); |
| 415 | virtual ~ListenerClass(); |
| 416 | |
| 417 | bool needed; |
| 418 | |
| 419 | private: |
| 420 | void generate_ctor(); |
| 421 | }; |
| 422 | |
| 423 | ListenerClass::ListenerClass(const interface_type* iface) |
| 424 | :Class(), |
| 425 | needed(false) |
| 426 | { |
| 427 | this->comment = "/** Extend this to listen to the events from this class. */"; |
| 428 | this->modifiers = STATIC | PUBLIC ; |
| 429 | this->what = Class::CLASS; |
| 430 | this->type = new Type(iface->package ? iface->package : "", |
| 431 | append(iface->name.data, ".Listener"), |
| 432 | Type::GENERATED, false, false, false); |
| 433 | this->extends = PRESENTER_LISTENER_BASE_TYPE; |
| 434 | } |
| 435 | |
| 436 | ListenerClass::~ListenerClass() |
| 437 | { |
| 438 | } |
| 439 | |
| 440 | // ================================================= |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 441 | class EndpointBaseClass : public DispatcherClass |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 442 | { |
| 443 | public: |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 444 | EndpointBaseClass(const interface_type* iface); |
| 445 | virtual ~EndpointBaseClass(); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 446 | |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 447 | bool needed; |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 448 | |
| 449 | private: |
| 450 | void generate_ctor(); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 451 | }; |
| 452 | |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 453 | EndpointBaseClass::EndpointBaseClass(const interface_type* iface) |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 454 | :DispatcherClass(iface, THIS_VALUE), |
| 455 | needed(false) |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 456 | { |
| 457 | this->comment = "/** Extend this to implement a link service. */"; |
| 458 | this->modifiers = STATIC | PUBLIC | ABSTRACT; |
| 459 | this->what = Class::CLASS; |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 460 | this->type = new Type(iface->package ? iface->package : "", |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 461 | append(iface->name.data, ".EndpointBase"), |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 462 | Type::GENERATED, false, false, false); |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 463 | this->extends = RPC_CONNECTOR_TYPE; |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 464 | |
| 465 | // methods |
| 466 | generate_ctor(); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 469 | EndpointBaseClass::~EndpointBaseClass() |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 470 | { |
| 471 | } |
| 472 | |
| 473 | void |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 474 | EndpointBaseClass::generate_ctor() |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 475 | { |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 476 | Variable* container = new Variable(RPC_CONTAINER_TYPE, "container"); |
| 477 | Variable* broker = new Variable(RPC_BROKER_TYPE, "broker"); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 478 | Method* ctor = new Method; |
| 479 | ctor->modifiers = PUBLIC; |
| 480 | ctor->name = class_name_leaf(this->type->Name()); |
| 481 | ctor->statements = new StatementBlock; |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 482 | ctor->parameters.push_back(container); |
| 483 | ctor->parameters.push_back(broker); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 484 | this->elements.push_back(ctor); |
| 485 | |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 486 | ctor->statements->Add(new MethodCall("super", 2, container, broker)); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 489 | // ================================================= |
| 490 | class ResultDispatcherClass : public Class |
| 491 | { |
| 492 | public: |
| 493 | ResultDispatcherClass(); |
| 494 | virtual ~ResultDispatcherClass(); |
| 495 | |
| 496 | void AddMethod(int index, const string& name, Method** method, Variable** param); |
| 497 | |
| 498 | bool needed; |
| 499 | Variable* methodId; |
| 500 | Variable* callback; |
| 501 | Method* onResultMethod; |
| 502 | Variable* resultParam; |
| 503 | SwitchStatement* methodSwitch; |
| 504 | |
| 505 | private: |
| 506 | void generate_ctor(); |
| 507 | void generate_onResult(); |
| 508 | }; |
| 509 | |
| 510 | ResultDispatcherClass::ResultDispatcherClass() |
| 511 | :Class(), |
| 512 | needed(false) |
| 513 | { |
| 514 | this->modifiers = PRIVATE | FINAL; |
| 515 | this->what = Class::CLASS; |
| Joe Onorato | 7db766c | 2011-09-15 21:31:15 -0700 | [diff] [blame] | 516 | this->type = new Type("_ResultDispatcher", Type::GENERATED, false, false, false); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 517 | this->interfaces.push_back(RPC_RESULT_HANDLER_TYPE); |
| 518 | |
| 519 | // methodId |
| 520 | this->methodId = new Variable(INT_TYPE, "methodId"); |
| 521 | this->elements.push_back(new Field(PRIVATE, this->methodId)); |
| 522 | this->callback = new Variable(OBJECT_TYPE, "callback"); |
| 523 | this->elements.push_back(new Field(PRIVATE, this->callback)); |
| 524 | |
| 525 | // methods |
| 526 | generate_ctor(); |
| 527 | generate_onResult(); |
| 528 | } |
| 529 | |
| 530 | ResultDispatcherClass::~ResultDispatcherClass() |
| 531 | { |
| 532 | } |
| 533 | |
| 534 | void |
| 535 | ResultDispatcherClass::generate_ctor() |
| 536 | { |
| 537 | Variable* methodIdParam = new Variable(INT_TYPE, "methId"); |
| 538 | Variable* callbackParam = new Variable(OBJECT_TYPE, "cbObj"); |
| 539 | Method* ctor = new Method; |
| 540 | ctor->modifiers = PUBLIC; |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 541 | ctor->name = class_name_leaf(this->type->Name()); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 542 | ctor->statements = new StatementBlock; |
| 543 | ctor->parameters.push_back(methodIdParam); |
| 544 | ctor->parameters.push_back(callbackParam); |
| 545 | this->elements.push_back(ctor); |
| 546 | |
| 547 | ctor->statements->Add(new Assignment(this->methodId, methodIdParam)); |
| 548 | ctor->statements->Add(new Assignment(this->callback, callbackParam)); |
| 549 | } |
| 550 | |
| 551 | void |
| 552 | ResultDispatcherClass::generate_onResult() |
| 553 | { |
| 554 | this->onResultMethod = new Method; |
| 555 | this->onResultMethod->modifiers = PUBLIC; |
| 556 | this->onResultMethod->returnType = VOID_TYPE; |
| 557 | this->onResultMethod->returnTypeDimension = 0; |
| 558 | this->onResultMethod->name = "onResult"; |
| 559 | this->onResultMethod->statements = new StatementBlock; |
| 560 | this->elements.push_back(this->onResultMethod); |
| 561 | |
| 562 | this->resultParam = new Variable(BYTE_TYPE, "result", 1); |
| 563 | this->onResultMethod->parameters.push_back(this->resultParam); |
| 564 | |
| 565 | this->methodSwitch = new SwitchStatement(this->methodId); |
| 566 | this->onResultMethod->statements->Add(this->methodSwitch); |
| 567 | } |
| 568 | |
| 569 | void |
| 570 | ResultDispatcherClass::AddMethod(int index, const string& name, Method** method, Variable** param) |
| 571 | { |
| 572 | Method* m = new Method; |
| 573 | m->modifiers = PUBLIC; |
| 574 | m->returnType = VOID_TYPE; |
| 575 | m->returnTypeDimension = 0; |
| 576 | m->name = name; |
| 577 | m->statements = new StatementBlock; |
| 578 | *param = new Variable(BYTE_TYPE, "result", 1); |
| 579 | m->parameters.push_back(*param); |
| 580 | this->elements.push_back(m); |
| 581 | *method = m; |
| 582 | |
| 583 | Case* c = new Case(format_int(index)); |
| 584 | c->statements->Add(new MethodCall(new LiteralExpression("this"), name, 1, this->resultParam)); |
| Joe Onorato | 05ffbe7 | 2011-09-02 15:28:36 -0700 | [diff] [blame] | 585 | c->statements->Add(new Break()); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 586 | |
| 587 | this->methodSwitch->cases.push_back(c); |
| 588 | } |
| 589 | |
| 590 | // ================================================= |
| 591 | static void |
| 592 | generate_new_array(Type* t, StatementBlock* addTo, Variable* v, Variable* from) |
| 593 | { |
| 594 | fprintf(stderr, "aidl: implement generate_new_array %s:%d\n", __FILE__, __LINE__); |
| 595 | exit(1); |
| 596 | } |
| 597 | |
| 598 | static void |
| 599 | generate_create_from_data(Type* t, StatementBlock* addTo, const string& key, Variable* v, |
| 600 | Variable* data, Variable** cl) |
| 601 | { |
| 602 | Expression* k = new StringLiteralExpression(key); |
| 603 | if (v->dimension == 0) { |
| 604 | t->CreateFromRpcData(addTo, k, v, data, cl); |
| 605 | } |
| 606 | if (v->dimension == 1) { |
| 607 | //t->ReadArrayFromRpcData(addTo, v, data, cl); |
| 608 | fprintf(stderr, "aidl: implement generate_create_from_data for arrays%s:%d\n", |
| 609 | __FILE__, __LINE__); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | static void |
| 614 | generate_write_to_data(Type* t, StatementBlock* addTo, Expression* k, Variable* v, Variable* data) |
| 615 | { |
| 616 | if (v->dimension == 0) { |
| 617 | t->WriteToRpcData(addTo, k, v, data, 0); |
| 618 | } |
| 619 | if (v->dimension == 1) { |
| 620 | //t->WriteArrayToParcel(addTo, v, data); |
| 621 | fprintf(stderr, "aidl: implement generate_write_to_data for arrays%s:%d\n", |
| 622 | __FILE__, __LINE__); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | // ================================================= |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 627 | static Type* |
| 628 | generate_results_method(const method_type* method, RpcProxyClass* proxyClass) |
| 629 | { |
| 630 | arg_type* arg; |
| 631 | |
| 632 | string resultsMethodName = results_method_name(method->name.data); |
| 633 | Type* resultsInterfaceType = new Type(results_class_name(method->name.data), |
| Joe Onorato | 7db766c | 2011-09-15 21:31:15 -0700 | [diff] [blame] | 634 | Type::GENERATED, false, false, false); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 635 | |
| 636 | if (!method->oneway) { |
| 637 | Class* resultsClass = new Class; |
| 638 | resultsClass->modifiers = STATIC | PUBLIC; |
| 639 | resultsClass->what = Class::INTERFACE; |
| 640 | resultsClass->type = resultsInterfaceType; |
| 641 | |
| 642 | Method* resultMethod = new Method; |
| 643 | resultMethod->comment = gather_comments(method->comments_token->extra); |
| 644 | resultMethod->modifiers = PUBLIC; |
| 645 | resultMethod->returnType = VOID_TYPE; |
| 646 | resultMethod->returnTypeDimension = 0; |
| 647 | resultMethod->name = resultsMethodName; |
| 648 | if (0 != strcmp("void", method->type.type.data)) { |
| 649 | resultMethod->parameters.push_back(new Variable(NAMES.Search(method->type.type.data), |
| 650 | "_result", method->type.dimension)); |
| 651 | } |
| 652 | arg = method->args; |
| 653 | while (arg != NULL) { |
| 654 | if (convert_direction(arg->direction.data) & OUT_PARAMETER) { |
| 655 | resultMethod->parameters.push_back(new Variable( |
| 656 | NAMES.Search(arg->type.type.data), arg->name.data, |
| 657 | arg->type.dimension)); |
| 658 | } |
| 659 | arg = arg->next; |
| 660 | } |
| 661 | resultsClass->elements.push_back(resultMethod); |
| 662 | |
| 663 | if (resultMethod->parameters.size() > 0) { |
| 664 | proxyClass->elements.push_back(resultsClass); |
| 665 | return resultsInterfaceType; |
| 666 | } |
| 667 | } |
| 668 | //delete resultsInterfaceType; |
| 669 | return NULL; |
| 670 | } |
| 671 | |
| 672 | static void |
| 673 | generate_proxy_method(const method_type* method, RpcProxyClass* proxyClass, |
| 674 | ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index) |
| 675 | { |
| 676 | arg_type* arg; |
| 677 | Method* proxyMethod = new Method; |
| 678 | proxyMethod->comment = gather_comments(method->comments_token->extra); |
| 679 | proxyMethod->modifiers = PUBLIC; |
| 680 | proxyMethod->returnType = VOID_TYPE; |
| 681 | proxyMethod->returnTypeDimension = 0; |
| 682 | proxyMethod->name = method->name.data; |
| 683 | proxyMethod->statements = new StatementBlock; |
| 684 | proxyClass->elements.push_back(proxyMethod); |
| 685 | |
| 686 | // The local variables |
| 687 | Variable* _data = new Variable(RPC_DATA_TYPE, "_data"); |
| 688 | proxyMethod->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE))); |
| 689 | |
| 690 | // Add the arguments |
| 691 | arg = method->args; |
| 692 | while (arg != NULL) { |
| 693 | if (convert_direction(arg->direction.data) & IN_PARAMETER) { |
| 694 | // Function signature |
| 695 | Type* t = NAMES.Search(arg->type.type.data); |
| 696 | Variable* v = new Variable(t, arg->name.data, arg->type.dimension); |
| 697 | proxyMethod->parameters.push_back(v); |
| 698 | |
| 699 | // Input parameter marshalling |
| 700 | generate_write_to_data(t, proxyMethod->statements, |
| 701 | new StringLiteralExpression(arg->name.data), v, _data); |
| 702 | } |
| 703 | arg = arg->next; |
| 704 | } |
| 705 | |
| 706 | // If there is a results interface for this class |
| 707 | Expression* resultParameter; |
| 708 | if (resultsInterfaceType != NULL) { |
| 709 | // Result interface parameter |
| 710 | Variable* resultListener = new Variable(resultsInterfaceType, "_result"); |
| 711 | proxyMethod->parameters.push_back(resultListener); |
| 712 | |
| 713 | // Add the results dispatcher callback |
| 714 | resultsDispatcherClass->needed = true; |
| 715 | resultParameter = new NewExpression(resultsDispatcherClass->type, 2, |
| 716 | new LiteralExpression(format_int(index)), resultListener); |
| 717 | } else { |
| 718 | resultParameter = NULL_VALUE; |
| 719 | } |
| 720 | |
| 721 | // All proxy methods take an error parameter |
| 722 | Variable* errorListener = new Variable(RPC_ERROR_LISTENER_TYPE, "_errors"); |
| 723 | proxyMethod->parameters.push_back(errorListener); |
| 724 | |
| 725 | // Call the broker |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 726 | proxyMethod->statements->Add(new MethodCall(new FieldVariable(THIS_VALUE, "_broker"), |
| 727 | "sendRpc", 5, |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 728 | proxyClass->endpoint, |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 729 | new StringLiteralExpression(method->name.data), |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 730 | new MethodCall(_data, "serialize"), |
| 731 | resultParameter, |
| 732 | errorListener)); |
| 733 | } |
| 734 | |
| 735 | static void |
| 736 | generate_result_dispatcher_method(const method_type* method, |
| 737 | ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index) |
| 738 | { |
| 739 | arg_type* arg; |
| 740 | Method* dispatchMethod; |
| 741 | Variable* dispatchParam; |
| 742 | resultsDispatcherClass->AddMethod(index, method->name.data, &dispatchMethod, &dispatchParam); |
| 743 | |
| 744 | Variable* classLoader = NULL; |
| 745 | Variable* resultData = new Variable(RPC_DATA_TYPE, "resultData"); |
| 746 | dispatchMethod->statements->Add(new VariableDeclaration(resultData, |
| 747 | new NewExpression(RPC_DATA_TYPE, 1, dispatchParam))); |
| 748 | |
| 749 | // The callback method itself |
| 750 | MethodCall* realCall = new MethodCall( |
| 751 | new Cast(resultsInterfaceType, new FieldVariable(THIS_VALUE, "callback")), |
| 752 | results_method_name(method->name.data)); |
| 753 | |
| 754 | // The return value |
| 755 | { |
| 756 | Type* t = NAMES.Search(method->type.type.data); |
| Joe Onorato | 0a7eaec | 2011-11-17 16:13:49 -0800 | [diff] [blame] | 757 | if (t != VOID_TYPE) { |
| 758 | Variable* rv = new Variable(t, "rv"); |
| 759 | dispatchMethod->statements->Add(new VariableDeclaration(rv)); |
| 760 | generate_create_from_data(t, dispatchMethod->statements, "_result", rv, |
| 761 | resultData, &classLoader); |
| 762 | realCall->arguments.push_back(rv); |
| 763 | } |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | VariableFactory stubArgs("arg"); |
| 767 | arg = method->args; |
| 768 | while (arg != NULL) { |
| 769 | if (convert_direction(arg->direction.data) & OUT_PARAMETER) { |
| 770 | // Unmarshall the results |
| 771 | Type* t = NAMES.Search(arg->type.type.data); |
| 772 | Variable* v = stubArgs.Get(t); |
| 773 | dispatchMethod->statements->Add(new VariableDeclaration(v)); |
| 774 | |
| 775 | generate_create_from_data(t, dispatchMethod->statements, arg->name.data, v, |
| 776 | resultData, &classLoader); |
| 777 | |
| 778 | // Add the argument to the callback |
| 779 | realCall->arguments.push_back(v); |
| 780 | } |
| 781 | arg = arg->next; |
| 782 | } |
| 783 | |
| 784 | // Call the callback method |
| 785 | dispatchMethod->statements->Add(realCall); |
| 786 | } |
| 787 | |
| 788 | static void |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 789 | generate_regular_method(const method_type* method, RpcProxyClass* proxyClass, |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 790 | EndpointBaseClass* serviceBaseClass, ResultDispatcherClass* resultsDispatcherClass, |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 791 | int index) |
| 792 | { |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 793 | arg_type* arg; |
| 794 | |
| 795 | // == the callback interface for results ================================ |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 796 | // the service base class |
| 797 | Type* resultsInterfaceType = generate_results_method(method, proxyClass); |
| 798 | |
| 799 | // == the method in the proxy class ===================================== |
| 800 | generate_proxy_method(method, proxyClass, resultsDispatcherClass, resultsInterfaceType, index); |
| 801 | |
| 802 | // == the method in the result dispatcher class ========================= |
| 803 | if (resultsInterfaceType != NULL) { |
| 804 | generate_result_dispatcher_method(method, resultsDispatcherClass, resultsInterfaceType, |
| 805 | index); |
| 806 | } |
| 807 | |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 808 | // == The abstract method that the service developers implement ========== |
| 809 | Method* decl = new Method; |
| 810 | decl->comment = gather_comments(method->comments_token->extra); |
| 811 | decl->modifiers = PUBLIC | ABSTRACT; |
| 812 | decl->returnType = NAMES.Search(method->type.type.data); |
| 813 | decl->returnTypeDimension = method->type.dimension; |
| 814 | decl->name = method->name.data; |
| 815 | arg = method->args; |
| 816 | while (arg != NULL) { |
| 817 | decl->parameters.push_back(new Variable( |
| 818 | NAMES.Search(arg->type.type.data), arg->name.data, |
| 819 | arg->type.dimension)); |
| 820 | arg = arg->next; |
| 821 | } |
| Manuel Roman | b71287f | 2011-10-06 10:28:35 -0700 | [diff] [blame] | 822 | |
| 823 | // Add the default RpcContext param to all methods |
| 824 | decl->parameters.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0)); |
| 825 | |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 826 | serviceBaseClass->elements.push_back(decl); |
| 827 | |
| 828 | |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 829 | // == the dispatch method in the service base class ====================== |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 830 | serviceBaseClass->AddMethod(method); |
| 831 | } |
| 832 | |
| 833 | static void |
| 834 | generate_event_method(const method_type* method, RpcProxyClass* proxyClass, |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 835 | EndpointBaseClass* serviceBaseClass, ListenerClass* listenerClass, |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 836 | EventListenerClass* presenterClass, int index) |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 837 | { |
| 838 | arg_type* arg; |
| 839 | listenerClass->needed = true; |
| 840 | |
| 841 | // == the push method in the service base class ========================= |
| 842 | Method* push = new Method; |
| 843 | push->modifiers = PUBLIC; |
| 844 | push->name = push_method_name(method->name.data); |
| 845 | push->statements = new StatementBlock; |
| 846 | push->returnType = VOID_TYPE; |
| 847 | serviceBaseClass->elements.push_back(push); |
| 848 | |
| 849 | // The local variables |
| 850 | Variable* _data = new Variable(RPC_DATA_TYPE, "_data"); |
| 851 | push->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE))); |
| 852 | |
| 853 | // Add the arguments |
| 854 | arg = method->args; |
| 855 | while (arg != NULL) { |
| 856 | // Function signature |
| 857 | Type* t = NAMES.Search(arg->type.type.data); |
| 858 | Variable* v = new Variable(t, arg->name.data, arg->type.dimension); |
| 859 | push->parameters.push_back(v); |
| 860 | |
| 861 | // Input parameter marshalling |
| 862 | generate_write_to_data(t, push->statements, |
| 863 | new StringLiteralExpression(arg->name.data), v, _data); |
| 864 | |
| 865 | arg = arg->next; |
| 866 | } |
| 867 | |
| 868 | // Send the notifications |
| 869 | push->statements->Add(new MethodCall("pushEvent", 2, |
| 870 | new StringLiteralExpression(method->name.data), |
| 871 | new MethodCall(_data, "serialize"))); |
| 872 | |
| 873 | // == the event callback dispatcher method ==================================== |
| 874 | presenterClass->AddMethod(method); |
| 875 | |
| 876 | // == the event method in the listener base class ===================== |
| 877 | Method* event = new Method; |
| 878 | event->modifiers = PUBLIC; |
| 879 | event->name = method->name.data; |
| 880 | event->statements = new StatementBlock; |
| 881 | event->returnType = VOID_TYPE; |
| 882 | listenerClass->elements.push_back(event); |
| 883 | arg = method->args; |
| 884 | while (arg != NULL) { |
| 885 | event->parameters.push_back(new Variable( |
| 886 | NAMES.Search(arg->type.type.data), arg->name.data, |
| 887 | arg->type.dimension)); |
| 888 | arg = arg->next; |
| 889 | } |
| Manuel Roman | b71287f | 2011-10-06 10:28:35 -0700 | [diff] [blame] | 890 | |
| 891 | // Add a final parameter: RpcContext. Contains data about |
| 892 | // incoming request (e.g., certificate) |
| 893 | event->parameters.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0)); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | static void |
| 897 | generate_listener_methods(RpcProxyClass* proxyClass, Type* presenterType, Type* listenerType) |
| 898 | { |
| 899 | // AndroidAtHomePresenter _presenter; |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 900 | // void startListening(Listener listener) { |
| 901 | // stopListening(); |
| 902 | // _presenter = new Presenter(_broker, listener); |
| 903 | // _presenter.startListening(_endpoint); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 904 | // } |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 905 | // void stopListening() { |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 906 | // if (_presenter != null) { |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 907 | // _presenter.stopListening(); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 908 | // } |
| 909 | // } |
| 910 | |
| 911 | Variable* _presenter = new Variable(presenterType, "_presenter"); |
| 912 | proxyClass->elements.push_back(new Field(PRIVATE, _presenter)); |
| 913 | |
| 914 | Variable* listener = new Variable(listenerType, "listener"); |
| 915 | |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 916 | Method* startListeningMethod = new Method; |
| 917 | startListeningMethod->modifiers = PUBLIC; |
| 918 | startListeningMethod->returnType = VOID_TYPE; |
| 919 | startListeningMethod->name = "startListening"; |
| 920 | startListeningMethod->statements = new StatementBlock; |
| 921 | startListeningMethod->parameters.push_back(listener); |
| 922 | proxyClass->elements.push_back(startListeningMethod); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 923 | |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 924 | startListeningMethod->statements->Add(new MethodCall(THIS_VALUE, "stopListening")); |
| 925 | startListeningMethod->statements->Add(new Assignment(_presenter, |
| 926 | new NewExpression(presenterType, 2, proxyClass->broker, listener))); |
| 927 | startListeningMethod->statements->Add(new MethodCall(_presenter, |
| 928 | "startListening", 1, proxyClass->endpoint)); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 929 | |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 930 | Method* stopListeningMethod = new Method; |
| 931 | stopListeningMethod->modifiers = PUBLIC; |
| 932 | stopListeningMethod->returnType = VOID_TYPE; |
| 933 | stopListeningMethod->name = "stopListening"; |
| 934 | stopListeningMethod->statements = new StatementBlock; |
| 935 | proxyClass->elements.push_back(stopListeningMethod); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 936 | |
| 937 | IfStatement* ifst = new IfStatement; |
| 938 | ifst->expression = new Comparison(_presenter, "!=", NULL_VALUE); |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 939 | stopListeningMethod->statements->Add(ifst); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 940 | |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 941 | ifst->statements->Add(new MethodCall(_presenter, "stopListening")); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 942 | ifst->statements->Add(new Assignment(_presenter, NULL_VALUE)); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | Class* |
| 946 | generate_rpc_interface_class(const interface_type* iface) |
| 947 | { |
| 948 | // the proxy class |
| 949 | InterfaceType* interfaceType = static_cast<InterfaceType*>( |
| 950 | NAMES.Find(iface->package, iface->name.data)); |
| 951 | RpcProxyClass* proxy = new RpcProxyClass(iface, interfaceType); |
| 952 | |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 953 | // the listener class |
| 954 | ListenerClass* listener = new ListenerClass(iface); |
| 955 | |
| 956 | // the presenter class |
| Joe Onorato | 28087c6 | 2011-10-12 23:22:42 -0700 | [diff] [blame] | 957 | EventListenerClass* presenter = new EventListenerClass(iface, listener->type); |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 958 | |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 959 | // the service base class |
| Joe Onorato | 95a766d | 2011-10-09 21:51:46 -0700 | [diff] [blame] | 960 | EndpointBaseClass* base = new EndpointBaseClass(iface); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 961 | proxy->elements.push_back(base); |
| 962 | |
| 963 | // the result dispatcher |
| 964 | ResultDispatcherClass* results = new ResultDispatcherClass(); |
| 965 | |
| 966 | // all the declared methods of the proxy |
| 967 | int index = 0; |
| 968 | interface_item_type* item = iface->interface_items; |
| 969 | while (item != NULL) { |
| 970 | if (item->item_type == METHOD_TYPE) { |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 971 | if (NAMES.Search(((method_type*)item)->type.type.data) == EVENT_FAKE_TYPE) { |
| 972 | generate_event_method((method_type*)item, proxy, base, listener, presenter, index); |
| 973 | } else { |
| 974 | generate_regular_method((method_type*)item, proxy, base, results, index); |
| 975 | } |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 976 | } |
| 977 | item = item->next; |
| 978 | index++; |
| 979 | } |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 980 | presenter->DoneWithMethods(); |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 981 | base->DoneWithMethods(); |
| 982 | |
| 983 | // only add this if there are methods with results / out parameters |
| 984 | if (results->needed) { |
| 985 | proxy->elements.push_back(results); |
| 986 | } |
| Joe Onorato | e24dbea | 2011-09-23 15:17:52 -0700 | [diff] [blame] | 987 | if (listener->needed) { |
| 988 | proxy->elements.push_back(listener); |
| 989 | proxy->elements.push_back(presenter); |
| 990 | generate_listener_methods(proxy, presenter->type, listener->type); |
| 991 | } |
| Joe Onorato | fdfe2ff | 2011-08-30 17:24:17 -0700 | [diff] [blame] | 992 | |
| 993 | return proxy; |
| 994 | } |