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