1fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato#include "generate_java.h"
2fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato#include "Type.h"
3fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato#include <string.h>
4fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato#include <stdio.h>
5fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato#include <stdlib.h>
6fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato#include <string.h>
7fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
8b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel RomanType* SERVICE_CONTEXT_TYPE = new Type("android.content",
9b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman        "Context", Type::BUILT_IN, false, false, false);
1017a452f00c188173f48d0d490d98750cbb4529caMike LockwoodType* PRESENTER_BASE_TYPE = new Type("android.support.place.connector",
1128087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        "EventListener", Type::BUILT_IN, false, false, false);
1217a452f00c188173f48d0d490d98750cbb4529caMike LockwoodType* PRESENTER_LISTENER_BASE_TYPE = new Type("android.support.place.connector",
1328087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        "EventListener.Listener", Type::BUILT_IN, false, false, false);
1417a452f00c188173f48d0d490d98750cbb4529caMike LockwoodType* RPC_BROKER_TYPE = new Type("android.support.place.connector", "Broker",
15e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        Type::BUILT_IN, false, false, false);
1695a766ddcd0feda233882012b755947b2588352cJoe OnoratoType* RPC_CONTAINER_TYPE = new Type("com.android.athome.connector", "ConnectorContainer",
17e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        Type::BUILT_IN, false, false, false);
1801060b08fbf258d21d344b38d9967b9ef0b50fd5Manuel RomanType* PLACE_INFO_TYPE = new Type("android.support.place.connector", "PlaceInfo",
1901060b08fbf258d21d344b38d9967b9ef0b50fd5Manuel Roman        Type::BUILT_IN, false, false, false);
2095a766ddcd0feda233882012b755947b2588352cJoe Onorato// TODO: Just use Endpoint, so this works for all endpoints.
2117a452f00c188173f48d0d490d98750cbb4529caMike LockwoodType* RPC_CONNECTOR_TYPE = new Type("android.support.place.connector", "Connector",
2295a766ddcd0feda233882012b755947b2588352cJoe Onorato        Type::BUILT_IN, false, false, false);
2317a452f00c188173f48d0d490d98750cbb4529caMike LockwoodType* RPC_ENDPOINT_INFO_TYPE = new UserDataType("android.support.place.rpc",
2495a766ddcd0feda233882012b755947b2588352cJoe Onorato        "EndpointInfo", true, __FILE__, __LINE__);
2517a452f00c188173f48d0d490d98750cbb4529caMike LockwoodType* RPC_RESULT_HANDLER_TYPE = new UserDataType("android.support.place.rpc", "RpcResultHandler",
26e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        true, __FILE__, __LINE__);
2717a452f00c188173f48d0d490d98750cbb4529caMike LockwoodType* RPC_ERROR_LISTENER_TYPE = new Type("android.support.place.rpc", "RpcErrorHandler",
28e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        Type::BUILT_IN, false, false, false);
2917a452f00c188173f48d0d490d98750cbb4529caMike LockwoodType* RPC_CONTEXT_TYPE = new UserDataType("android.support.place.rpc", "RpcContext", true,
30b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman        __FILE__, __LINE__);
31e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
32e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratostatic void generate_create_from_data(Type* t, StatementBlock* addTo, const string& key,
33e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        Variable* v, Variable* data, Variable** cl);
34e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratostatic void generate_new_array(Type* t, StatementBlock* addTo, Variable* v, Variable* from);
35e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratostatic void generate_write_to_data(Type* t, StatementBlock* addTo, Expression* k, Variable* v,
36e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        Variable* data);
37fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
38fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratostatic string
39fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratoformat_int(int n)
40fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
41fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    char str[20];
42fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    sprintf(str, "%d", n);
43fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    return string(str);
44fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
45fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
46fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratostatic string
47fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratoclass_name_leaf(const string& str)
48fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
49fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    string::size_type pos = str.rfind('.');
50fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    if (pos == string::npos) {
51fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        return str;
52fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    } else {
53fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        return string(str, pos+1);
54fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
55fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
56fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
57e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratostatic string
58e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratoresults_class_name(const string& n)
59e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
60e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    string str = n;
61e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    str[0] = toupper(str[0]);
62e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    str.insert(0, "On");
63e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    return str;
64e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
65e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
66e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratostatic string
67e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratoresults_method_name(const string& n)
68e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
69e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    string str = n;
70e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    str[0] = toupper(str[0]);
71e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    str.insert(0, "on");
72e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    return str;
73e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
74e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
75e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratostatic string
76e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratopush_method_name(const string& n)
77e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
78e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    string str = n;
79e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    str[0] = toupper(str[0]);
80e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    str.insert(0, "push");
81e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    return str;
82e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
83e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
84e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato// =================================================
85e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratoclass DispatcherClass : public Class
86e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
87e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratopublic:
88e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    DispatcherClass(const interface_type* iface, Expression* target);
89e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    virtual ~DispatcherClass();
90e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
91e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    void AddMethod(const method_type* method);
92e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    void DoneWithMethods();
93e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
94e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Method* processMethod;
95e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* actionParam;
96e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* requestParam;
97b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    Variable* rpcContextParam;
98e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* errorParam;
99e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* requestData;
100e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* resultData;
101e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    IfStatement* dispatchIfStatement;
102e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Expression* targetExpression;
103e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
104e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratoprivate:
105e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    void generate_process();
106e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato};
107e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
108e24dbeafe64278408b3342fcb77756bc7d091791Joe OnoratoDispatcherClass::DispatcherClass(const interface_type* iface, Expression* target)
109e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    :Class(),
110e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato     dispatchIfStatement(NULL),
111e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato     targetExpression(target)
112e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
113e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    generate_process();
114e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
115e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
116e24dbeafe64278408b3342fcb77756bc7d091791Joe OnoratoDispatcherClass::~DispatcherClass()
117e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
118e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
119e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
120e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratovoid
121e24dbeafe64278408b3342fcb77756bc7d091791Joe OnoratoDispatcherClass::generate_process()
122e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
123b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    // byte[] process(String action, byte[] params, RpcContext context, RpcError status)
124e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->processMethod = new Method;
125e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        this->processMethod->modifiers = PUBLIC;
126e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        this->processMethod->returnType = BYTE_TYPE;
127e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        this->processMethod->returnTypeDimension = 1;
128e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        this->processMethod->name = "process";
129e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        this->processMethod->statements = new StatementBlock;
130e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
131e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->actionParam = new Variable(STRING_TYPE, "action");
132e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->processMethod->parameters.push_back(this->actionParam);
133e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
134e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->requestParam = new Variable(BYTE_TYPE, "requestParam", 1);
135e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->processMethod->parameters.push_back(this->requestParam);
136e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
137b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    this->rpcContextParam = new Variable(RPC_CONTEXT_TYPE, "context", 0);
138b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    this->processMethod->parameters.push_back(this->rpcContextParam);
139b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman
140e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->errorParam = new Variable(RPC_ERROR_TYPE, "errorParam", 0);
141e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->processMethod->parameters.push_back(this->errorParam);
142e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
143e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->requestData = new Variable(RPC_DATA_TYPE, "request");
144e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->processMethod->statements->Add(new VariableDeclaration(requestData,
145e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                new NewExpression(RPC_DATA_TYPE, 1, this->requestParam)));
146e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
147e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->resultData = new Variable(RPC_DATA_TYPE, "resultData");
148e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->processMethod->statements->Add(new VariableDeclaration(this->resultData,
149e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                NULL_VALUE));
150e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
151e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
152e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratovoid
153e24dbeafe64278408b3342fcb77756bc7d091791Joe OnoratoDispatcherClass::AddMethod(const method_type* method)
154e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
155e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    arg_type* arg;
156e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
157e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // The if/switch statement
158e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    IfStatement* ifs = new IfStatement();
159e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        ifs->expression = new MethodCall(new StringLiteralExpression(method->name.data), "equals",
160e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                1, this->actionParam);
161e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    StatementBlock* block = ifs->statements = new StatementBlock;
162e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    if (this->dispatchIfStatement == NULL) {
163e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        this->dispatchIfStatement = ifs;
164e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        this->processMethod->statements->Add(dispatchIfStatement);
165e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    } else {
166e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        this->dispatchIfStatement->elseif = ifs;
167e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        this->dispatchIfStatement = ifs;
168e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    }
169e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
170e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // The call to decl (from above)
171e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    MethodCall* realCall = new MethodCall(this->targetExpression, method->name.data);
172e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
173e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // args
174e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* classLoader = NULL;
175e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    VariableFactory stubArgs("_arg");
176e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    arg = method->args;
177e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    while (arg != NULL) {
178e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        Type* t = NAMES.Search(arg->type.type.data);
179e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        Variable* v = stubArgs.Get(t);
180e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        v->dimension = arg->type.dimension;
181e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
182e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        // Unmarshall the parameter
183e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        block->Add(new VariableDeclaration(v));
184e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        if (convert_direction(arg->direction.data) & IN_PARAMETER) {
185e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            generate_create_from_data(t, block, arg->name.data, v,
186e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                    this->requestData, &classLoader);
187e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        } else {
188e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            if (arg->type.dimension == 0) {
189e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                block->Add(new Assignment(v, new NewExpression(v->type)));
190e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            }
191e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            else if (arg->type.dimension == 1) {
192e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                generate_new_array(v->type, block, v, this->requestData);
193e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            }
194e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            else {
195e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__,
196e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                        __LINE__);
197e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            }
198e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        }
199e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
200e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        // Add that parameter to the method call
201e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        realCall->arguments.push_back(v);
202e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
203e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        arg = arg->next;
204e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    }
205e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
206b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    // Add a final parameter: RpcContext. Contains data about
207b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    // incoming request (e.g., certificate)
208b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    realCall->arguments.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
209e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
210e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Type* returnType = NAMES.Search(method->type.type.data);
211e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    if (returnType == EVENT_FAKE_TYPE) {
212e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        returnType = VOID_TYPE;
213e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    }
214e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
215e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // the real call
216e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    bool first = true;
217e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* _result = NULL;
218e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    if (returnType == VOID_TYPE) {
219e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        block->Add(realCall);
220e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    } else {
221e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        _result = new Variable(returnType, "_result",
222e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                                method->type.dimension);
223e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        block->Add(new VariableDeclaration(_result, realCall));
224e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
225e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        // need the result RpcData
226e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        if (first) {
227e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            block->Add(new Assignment(this->resultData,
228e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                        new NewExpression(RPC_DATA_TYPE)));
229e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            first = false;
230e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        }
231e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
232e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        // marshall the return value
233e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        generate_write_to_data(returnType, block,
234e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                new StringLiteralExpression("_result"), _result, this->resultData);
235e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    }
236e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
237e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // out parameters
238e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    int i = 0;
239e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    arg = method->args;
240e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    while (arg != NULL) {
241e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        Type* t = NAMES.Search(arg->type.type.data);
242e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        Variable* v = stubArgs.Get(i++);
243e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
244e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
245e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            // need the result RpcData
246e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            if (first) {
247e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                block->Add(new Assignment(this->resultData, new NewExpression(RPC_DATA_TYPE)));
248e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                first = false;
249e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            }
250e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
251e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            generate_write_to_data(t, block, new StringLiteralExpression(arg->name.data),
252e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                    v, this->resultData);
253e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        }
254e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
255e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        arg = arg->next;
256e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    }
257e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
258e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
259e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratovoid
260e24dbeafe64278408b3342fcb77756bc7d091791Joe OnoratoDispatcherClass::DoneWithMethods()
261e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
262e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    if (this->dispatchIfStatement == NULL) {
263e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        return;
264e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    }
265e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
266e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->elements.push_back(this->processMethod);
267e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
268e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    IfStatement* fallthrough = new IfStatement();
269e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        fallthrough->statements = new StatementBlock;
270e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        fallthrough->statements->Add(new ReturnStatement(
271b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman                    new MethodCall(SUPER_VALUE, "process", 4,
272b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman                    this->actionParam, this->requestParam,
273b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman                    this->rpcContextParam,
274b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman                    this->errorParam)));
275e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->dispatchIfStatement->elseif = fallthrough;
276e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    IfStatement* s = new IfStatement;
277e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        s->statements = new StatementBlock;
278e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->processMethod->statements->Add(s);
279e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    s->expression = new Comparison(this->resultData, "!=", NULL_VALUE);
280e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    s->statements->Add(new ReturnStatement(new MethodCall(this->resultData, "serialize")));
281e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    s->elseif = new IfStatement;
282e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    s = s->elseif;
283e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    s->statements->Add(new ReturnStatement(NULL_VALUE));
284e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
285e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
286fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato// =================================================
287fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratoclass RpcProxyClass : public Class
288fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
289fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratopublic:
290fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    RpcProxyClass(const interface_type* iface, InterfaceType* interfaceType);
291fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    virtual ~RpcProxyClass();
292fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
293fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* endpoint;
29495a766ddcd0feda233882012b755947b2588352cJoe Onorato    Variable* broker;
295fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
296fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratoprivate:
297fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    void generate_ctor();
2987dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons    void generate_get_endpoint_info();
299fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato};
300fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
301fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe OnoratoRpcProxyClass::RpcProxyClass(const interface_type* iface, InterfaceType* interfaceType)
302fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    :Class()
303fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
304fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->comment = gather_comments(iface->comments_token->extra);
305fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->modifiers = PUBLIC;
306fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->what = Class::CLASS;
307fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->type = interfaceType;
308fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
30995a766ddcd0feda233882012b755947b2588352cJoe Onorato    // broker
31095a766ddcd0feda233882012b755947b2588352cJoe Onorato    this->broker = new Variable(RPC_BROKER_TYPE, "_broker");
31195a766ddcd0feda233882012b755947b2588352cJoe Onorato    this->elements.push_back(new Field(PRIVATE, this->broker));
312fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // endpoint
31395a766ddcd0feda233882012b755947b2588352cJoe Onorato    this->endpoint = new Variable(RPC_ENDPOINT_INFO_TYPE, "_endpoint");
314fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->elements.push_back(new Field(PRIVATE, this->endpoint));
315fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
316fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // methods
317fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    generate_ctor();
3187dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons    generate_get_endpoint_info();
319fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
320fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
321fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe OnoratoRpcProxyClass::~RpcProxyClass()
322fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
323fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
324fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
325fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratovoid
326fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe OnoratoRpcProxyClass::generate_ctor()
327fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
32895a766ddcd0feda233882012b755947b2588352cJoe Onorato    Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
32995a766ddcd0feda233882012b755947b2588352cJoe Onorato    Variable* endpoint = new Variable(RPC_ENDPOINT_INFO_TYPE, "endpoint");
330fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Method* ctor = new Method;
331fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ctor->modifiers = PUBLIC;
332e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        ctor->name = class_name_leaf(this->type->Name());
333fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ctor->statements = new StatementBlock;
33495a766ddcd0feda233882012b755947b2588352cJoe Onorato        ctor->parameters.push_back(broker);
335fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ctor->parameters.push_back(endpoint);
336fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->elements.push_back(ctor);
337fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
33895a766ddcd0feda233882012b755947b2588352cJoe Onorato    ctor->statements->Add(new Assignment(this->broker, broker));
339fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    ctor->statements->Add(new Assignment(this->endpoint, endpoint));
340fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
341fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
3427dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmonsvoid
3437dc2973951e9d25e26cdc5a90297ac6c69e43083Jason SimmonsRpcProxyClass::generate_get_endpoint_info()
3447dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons{
3457dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons    Method* get = new Method;
3467dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons    get->modifiers = PUBLIC;
3477dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons    get->returnType = RPC_ENDPOINT_INFO_TYPE;
3487dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons    get->name = "getEndpointInfo";
3497dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons    get->statements = new StatementBlock;
3507dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons    this->elements.push_back(get);
3517dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons
3527dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons    get->statements->Add(new ReturnStatement(this->endpoint));
3537dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons}
3547dc2973951e9d25e26cdc5a90297ac6c69e43083Jason Simmons
355fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato// =================================================
35628087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onoratoclass EventListenerClass : public DispatcherClass
357e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
358e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratopublic:
35928087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    EventListenerClass(const interface_type* iface, Type* listenerType);
36028087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    virtual ~EventListenerClass();
361e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
362e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* _listener;
363e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
364e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratoprivate:
365e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    void generate_ctor();
366e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato};
367e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
368e24dbeafe64278408b3342fcb77756bc7d091791Joe OnoratoExpression*
369e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratogenerate_get_listener_expression(Type* cast)
370e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
371e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    return new Cast(cast, new MethodCall(THIS_VALUE, "getView"));
372e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
373e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
37428087c63d83b51dfd6533040b2ca5edae0bc278aJoe OnoratoEventListenerClass::EventListenerClass(const interface_type* iface, Type* listenerType)
37528087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    :DispatcherClass(iface, new FieldVariable(THIS_VALUE, "_listener"))
376e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
377e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->modifiers = PRIVATE;
378e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->what = Class::CLASS;
379e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->type = new Type(iface->package ? iface->package : "",
380e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                        append(iface->name.data, ".Presenter"),
381e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                        Type::GENERATED, false, false, false);
382e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->extends = PRESENTER_BASE_TYPE;
383e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
384e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->_listener = new Variable(listenerType, "_listener");
38522a7cb8d4941bfb0a6d843cac7e21257ee12d683Tim Kilbourn    this->elements.push_back(new Field(PRIVATE, this->_listener));
386e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
387e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // methods
388e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    generate_ctor();
389e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
390e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
39128087c63d83b51dfd6533040b2ca5edae0bc278aJoe OnoratoEventListenerClass::~EventListenerClass()
392e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
393e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
394e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
395e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratovoid
39628087c63d83b51dfd6533040b2ca5edae0bc278aJoe OnoratoEventListenerClass::generate_ctor()
397e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
39895a766ddcd0feda233882012b755947b2588352cJoe Onorato    Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
399e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* listener = new Variable(this->_listener->type, "listener");
400e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Method* ctor = new Method;
401e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        ctor->modifiers = PUBLIC;
402e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        ctor->name = class_name_leaf(this->type->Name());
403e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        ctor->statements = new StatementBlock;
40495a766ddcd0feda233882012b755947b2588352cJoe Onorato        ctor->parameters.push_back(broker);
405e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        ctor->parameters.push_back(listener);
406e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->elements.push_back(ctor);
407e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
40828087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    ctor->statements->Add(new MethodCall("super", 2, broker, listener));
409e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    ctor->statements->Add(new Assignment(this->_listener, listener));
410e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
411e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
412e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato// =================================================
413e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratoclass ListenerClass : public Class
414e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
415e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratopublic:
416e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    ListenerClass(const interface_type* iface);
417e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    virtual ~ListenerClass();
418e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
419e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    bool needed;
420e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
421e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratoprivate:
422e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    void generate_ctor();
423e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato};
424e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
425e24dbeafe64278408b3342fcb77756bc7d091791Joe OnoratoListenerClass::ListenerClass(const interface_type* iface)
426e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    :Class(),
427e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato     needed(false)
428e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
429e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->comment = "/** Extend this to listen to the events from this class. */";
430e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->modifiers = STATIC | PUBLIC ;
431e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->what = Class::CLASS;
432e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->type = new Type(iface->package ? iface->package : "",
433e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                        append(iface->name.data, ".Listener"),
434e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                        Type::GENERATED, false, false, false);
435e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->extends = PRESENTER_LISTENER_BASE_TYPE;
436e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
437e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
438e24dbeafe64278408b3342fcb77756bc7d091791Joe OnoratoListenerClass::~ListenerClass()
439e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
440e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
441e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
442e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato// =================================================
44395a766ddcd0feda233882012b755947b2588352cJoe Onoratoclass EndpointBaseClass : public DispatcherClass
444fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
445fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratopublic:
44695a766ddcd0feda233882012b755947b2588352cJoe Onorato    EndpointBaseClass(const interface_type* iface);
44795a766ddcd0feda233882012b755947b2588352cJoe Onorato    virtual ~EndpointBaseClass();
448fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
449fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    bool needed;
450fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
451fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratoprivate:
452fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    void generate_ctor();
453fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato};
454fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
45595a766ddcd0feda233882012b755947b2588352cJoe OnoratoEndpointBaseClass::EndpointBaseClass(const interface_type* iface)
456e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    :DispatcherClass(iface, THIS_VALUE),
457e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato     needed(false)
458fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
459fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->comment = "/** Extend this to implement a link service. */";
460fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->modifiers = STATIC | PUBLIC | ABSTRACT;
461fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->what = Class::CLASS;
462e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    this->type = new Type(iface->package ? iface->package : "",
46395a766ddcd0feda233882012b755947b2588352cJoe Onorato                        append(iface->name.data, ".EndpointBase"),
464e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                        Type::GENERATED, false, false, false);
46595a766ddcd0feda233882012b755947b2588352cJoe Onorato    this->extends = RPC_CONNECTOR_TYPE;
466fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
467fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // methods
468fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    generate_ctor();
469fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
470fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
47195a766ddcd0feda233882012b755947b2588352cJoe OnoratoEndpointBaseClass::~EndpointBaseClass()
472fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
473fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
474fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
475fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratovoid
47695a766ddcd0feda233882012b755947b2588352cJoe OnoratoEndpointBaseClass::generate_ctor()
477fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
47895a766ddcd0feda233882012b755947b2588352cJoe Onorato    Variable* container = new Variable(RPC_CONTAINER_TYPE, "container");
47995a766ddcd0feda233882012b755947b2588352cJoe Onorato    Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
48001060b08fbf258d21d344b38d9967b9ef0b50fd5Manuel Roman	Variable* place = new Variable(PLACE_INFO_TYPE, "placeInfo");
481fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Method* ctor = new Method;
482fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ctor->modifiers = PUBLIC;
483fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ctor->name = class_name_leaf(this->type->Name());
484fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ctor->statements = new StatementBlock;
48595a766ddcd0feda233882012b755947b2588352cJoe Onorato        ctor->parameters.push_back(container);
48695a766ddcd0feda233882012b755947b2588352cJoe Onorato        ctor->parameters.push_back(broker);
48701060b08fbf258d21d344b38d9967b9ef0b50fd5Manuel Roman        ctor->parameters.push_back(place);
488fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->elements.push_back(ctor);
489fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
49001060b08fbf258d21d344b38d9967b9ef0b50fd5Manuel Roman    ctor->statements->Add(new MethodCall("super", 3, container, broker, place));
491fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
492fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
493fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato// =================================================
494fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratoclass ResultDispatcherClass : public Class
495fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
496fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratopublic:
497fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    ResultDispatcherClass();
498fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    virtual ~ResultDispatcherClass();
499fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
500fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    void AddMethod(int index, const string& name, Method** method, Variable** param);
501fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
502fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    bool needed;
503fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* methodId;
504fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* callback;
505fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Method* onResultMethod;
506fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* resultParam;
507fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    SwitchStatement* methodSwitch;
508fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
509fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratoprivate:
510fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    void generate_ctor();
511fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    void generate_onResult();
512fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato};
513fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
514fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe OnoratoResultDispatcherClass::ResultDispatcherClass()
515fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    :Class(),
516fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato     needed(false)
517fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
518fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->modifiers = PRIVATE | FINAL;
519fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->what = Class::CLASS;
5207db766c3fec2795b08d237f29b972f474983684bJoe Onorato    this->type = new Type("_ResultDispatcher", Type::GENERATED, false, false, false);
521fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->interfaces.push_back(RPC_RESULT_HANDLER_TYPE);
522fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
523fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // methodId
524fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->methodId = new Variable(INT_TYPE, "methodId");
525fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->elements.push_back(new Field(PRIVATE, this->methodId));
526fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->callback = new Variable(OBJECT_TYPE, "callback");
527fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->elements.push_back(new Field(PRIVATE, this->callback));
528fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
529fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // methods
530fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    generate_ctor();
531fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    generate_onResult();
532fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
533fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
534fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe OnoratoResultDispatcherClass::~ResultDispatcherClass()
535fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
536fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
537fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
538fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratovoid
539fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe OnoratoResultDispatcherClass::generate_ctor()
540fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
541fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* methodIdParam = new Variable(INT_TYPE, "methId");
542fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* callbackParam = new Variable(OBJECT_TYPE, "cbObj");
543fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Method* ctor = new Method;
544fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ctor->modifiers = PUBLIC;
545e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        ctor->name = class_name_leaf(this->type->Name());
546fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ctor->statements = new StatementBlock;
547fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ctor->parameters.push_back(methodIdParam);
548fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ctor->parameters.push_back(callbackParam);
549fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->elements.push_back(ctor);
550fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
551fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    ctor->statements->Add(new Assignment(this->methodId, methodIdParam));
552fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    ctor->statements->Add(new Assignment(this->callback, callbackParam));
553fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
554fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
555fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratovoid
556fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe OnoratoResultDispatcherClass::generate_onResult()
557fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
558fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->onResultMethod = new Method;
559fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        this->onResultMethod->modifiers = PUBLIC;
560fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        this->onResultMethod->returnType = VOID_TYPE;
561fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        this->onResultMethod->returnTypeDimension = 0;
562fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        this->onResultMethod->name = "onResult";
563fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        this->onResultMethod->statements = new StatementBlock;
564fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->elements.push_back(this->onResultMethod);
565fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
566fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->resultParam = new Variable(BYTE_TYPE, "result", 1);
567fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->onResultMethod->parameters.push_back(this->resultParam);
568fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
569fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->methodSwitch = new SwitchStatement(this->methodId);
570fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->onResultMethod->statements->Add(this->methodSwitch);
571fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
572fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
573fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratovoid
574fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe OnoratoResultDispatcherClass::AddMethod(int index, const string& name, Method** method, Variable** param)
575fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
576fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Method* m = new Method;
577fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        m->modifiers = PUBLIC;
578fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        m->returnType = VOID_TYPE;
579fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        m->returnTypeDimension = 0;
580fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        m->name = name;
581fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        m->statements = new StatementBlock;
582fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    *param = new Variable(BYTE_TYPE, "result", 1);
583fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    m->parameters.push_back(*param);
584fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->elements.push_back(m);
585fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    *method = m;
586fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
587fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Case* c = new Case(format_int(index));
588fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    c->statements->Add(new MethodCall(new LiteralExpression("this"), name, 1, this->resultParam));
58905ffbe7c020c59ee4fa7e13e04641c954d591951Joe Onorato    c->statements->Add(new Break());
590fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
591fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    this->methodSwitch->cases.push_back(c);
592fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
593fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
594fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato// =================================================
595fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratostatic void
596fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratogenerate_new_array(Type* t, StatementBlock* addTo, Variable* v, Variable* from)
597fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
598fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    fprintf(stderr, "aidl: implement generate_new_array %s:%d\n", __FILE__, __LINE__);
599fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    exit(1);
600fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
601fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
602fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratostatic void
603fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratogenerate_create_from_data(Type* t, StatementBlock* addTo, const string& key, Variable* v,
604fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                            Variable* data, Variable** cl)
605fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
606fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Expression* k = new StringLiteralExpression(key);
607fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    if (v->dimension == 0) {
608fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        t->CreateFromRpcData(addTo, k, v, data, cl);
609fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
610fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    if (v->dimension == 1) {
611fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        //t->ReadArrayFromRpcData(addTo, v, data, cl);
612fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        fprintf(stderr, "aidl: implement generate_create_from_data for arrays%s:%d\n",
613fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                __FILE__, __LINE__);
614fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
615fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
616fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
617fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratostatic void
618fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratogenerate_write_to_data(Type* t, StatementBlock* addTo, Expression* k, Variable* v, Variable* data)
619fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
620fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    if (v->dimension == 0) {
621fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        t->WriteToRpcData(addTo, k, v, data, 0);
622fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
623fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    if (v->dimension == 1) {
624fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        //t->WriteArrayToParcel(addTo, v, data);
625fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        fprintf(stderr, "aidl: implement generate_write_to_data for arrays%s:%d\n",
626fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                __FILE__, __LINE__);
627fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
628fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
629fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
630fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato// =================================================
631fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratostatic Type*
632fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratogenerate_results_method(const method_type* method, RpcProxyClass* proxyClass)
633fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
634fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    arg_type* arg;
635fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
636fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    string resultsMethodName = results_method_name(method->name.data);
637fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Type* resultsInterfaceType = new Type(results_class_name(method->name.data),
6387db766c3fec2795b08d237f29b972f474983684bJoe Onorato            Type::GENERATED, false, false, false);
639fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
640fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    if (!method->oneway) {
641fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        Class* resultsClass = new Class;
642fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            resultsClass->modifiers = STATIC | PUBLIC;
643fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            resultsClass->what = Class::INTERFACE;
644fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            resultsClass->type = resultsInterfaceType;
645fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
646fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        Method* resultMethod = new Method;
647fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            resultMethod->comment = gather_comments(method->comments_token->extra);
648fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            resultMethod->modifiers = PUBLIC;
649fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            resultMethod->returnType = VOID_TYPE;
650fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            resultMethod->returnTypeDimension = 0;
651fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            resultMethod->name = resultsMethodName;
652fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        if (0 != strcmp("void", method->type.type.data)) {
653fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            resultMethod->parameters.push_back(new Variable(NAMES.Search(method->type.type.data),
654fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                        "_result", method->type.dimension));
655fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        }
656fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        arg = method->args;
657fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        while (arg != NULL) {
658fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
659fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                resultMethod->parameters.push_back(new Variable(
660fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                                    NAMES.Search(arg->type.type.data), arg->name.data,
661fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                                    arg->type.dimension));
662fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            }
663fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            arg = arg->next;
664fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        }
665fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        resultsClass->elements.push_back(resultMethod);
666fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
667fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        if (resultMethod->parameters.size() > 0) {
668fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            proxyClass->elements.push_back(resultsClass);
669fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            return resultsInterfaceType;
670fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        }
671fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
672fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    //delete resultsInterfaceType;
673fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    return NULL;
674fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
675fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
676fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratostatic void
677fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratogenerate_proxy_method(const method_type* method, RpcProxyClass* proxyClass,
678fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index)
679fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
680fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    arg_type* arg;
681fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Method* proxyMethod = new Method;
682fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        proxyMethod->comment = gather_comments(method->comments_token->extra);
683fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        proxyMethod->modifiers = PUBLIC;
684fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        proxyMethod->returnType = VOID_TYPE;
685fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        proxyMethod->returnTypeDimension = 0;
686fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        proxyMethod->name = method->name.data;
687fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        proxyMethod->statements = new StatementBlock;
688fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    proxyClass->elements.push_back(proxyMethod);
689fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
690fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // The local variables
691fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* _data = new Variable(RPC_DATA_TYPE, "_data");
692fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    proxyMethod->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE)));
693fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
694fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // Add the arguments
695fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    arg = method->args;
696fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    while (arg != NULL) {
697fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        if (convert_direction(arg->direction.data) & IN_PARAMETER) {
698fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            // Function signature
699fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            Type* t = NAMES.Search(arg->type.type.data);
700fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
701fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            proxyMethod->parameters.push_back(v);
702fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
703fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            // Input parameter marshalling
704fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            generate_write_to_data(t, proxyMethod->statements,
705fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                    new StringLiteralExpression(arg->name.data), v, _data);
706fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        }
707fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        arg = arg->next;
708fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
709fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
710fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // If there is a results interface for this class
711fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Expression* resultParameter;
712fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    if (resultsInterfaceType != NULL) {
713fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        // Result interface parameter
714fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        Variable* resultListener = new Variable(resultsInterfaceType, "_result");
715fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        proxyMethod->parameters.push_back(resultListener);
716fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
717fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        // Add the results dispatcher callback
718fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        resultsDispatcherClass->needed = true;
719fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        resultParameter = new NewExpression(resultsDispatcherClass->type, 2,
720fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                new LiteralExpression(format_int(index)), resultListener);
721fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    } else {
722fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        resultParameter = NULL_VALUE;
723fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
724fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
725fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // All proxy methods take an error parameter
726fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* errorListener = new Variable(RPC_ERROR_LISTENER_TYPE, "_errors");
727fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    proxyMethod->parameters.push_back(errorListener);
728fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
729fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // Call the broker
73095a766ddcd0feda233882012b755947b2588352cJoe Onorato    proxyMethod->statements->Add(new MethodCall(new FieldVariable(THIS_VALUE, "_broker"),
73195a766ddcd0feda233882012b755947b2588352cJoe Onorato                "sendRpc", 5,
732fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                proxyClass->endpoint,
73395a766ddcd0feda233882012b755947b2588352cJoe Onorato                new StringLiteralExpression(method->name.data),
734fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                new MethodCall(_data, "serialize"),
735fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                resultParameter,
736fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                errorListener));
737fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
738fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
739fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratostatic void
740fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratogenerate_result_dispatcher_method(const method_type* method,
741fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index)
742fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
743fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    arg_type* arg;
744fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Method* dispatchMethod;
745fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* dispatchParam;
746fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    resultsDispatcherClass->AddMethod(index, method->name.data, &dispatchMethod, &dispatchParam);
747fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
748fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* classLoader = NULL;
749fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Variable* resultData = new Variable(RPC_DATA_TYPE, "resultData");
750fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    dispatchMethod->statements->Add(new VariableDeclaration(resultData,
751fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                new NewExpression(RPC_DATA_TYPE, 1, dispatchParam)));
752fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
753fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // The callback method itself
754fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    MethodCall* realCall = new MethodCall(
755fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            new Cast(resultsInterfaceType, new FieldVariable(THIS_VALUE, "callback")),
756fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            results_method_name(method->name.data));
757fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
758fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // The return value
759fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    {
760fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        Type* t = NAMES.Search(method->type.type.data);
7610a7eaecf6457f9a186b2f2d5c0dbadc8b87e03b9Joe Onorato        if (t != VOID_TYPE) {
7620a7eaecf6457f9a186b2f2d5c0dbadc8b87e03b9Joe Onorato            Variable* rv = new Variable(t, "rv");
7630a7eaecf6457f9a186b2f2d5c0dbadc8b87e03b9Joe Onorato            dispatchMethod->statements->Add(new VariableDeclaration(rv));
7640a7eaecf6457f9a186b2f2d5c0dbadc8b87e03b9Joe Onorato            generate_create_from_data(t, dispatchMethod->statements, "_result", rv,
7650a7eaecf6457f9a186b2f2d5c0dbadc8b87e03b9Joe Onorato                    resultData, &classLoader);
7660a7eaecf6457f9a186b2f2d5c0dbadc8b87e03b9Joe Onorato            realCall->arguments.push_back(rv);
7670a7eaecf6457f9a186b2f2d5c0dbadc8b87e03b9Joe Onorato        }
768fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
769fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
770fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    VariableFactory stubArgs("arg");
771fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    arg = method->args;
772fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    while (arg != NULL) {
773fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
774fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            // Unmarshall the results
775fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            Type* t = NAMES.Search(arg->type.type.data);
776fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            Variable* v = stubArgs.Get(t);
777fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            dispatchMethod->statements->Add(new VariableDeclaration(v));
778fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
779fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            generate_create_from_data(t, dispatchMethod->statements, arg->name.data, v,
780fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                    resultData, &classLoader);
781fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
782fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            // Add the argument to the callback
783fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato            realCall->arguments.push_back(v);
784fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        }
785fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        arg = arg->next;
786fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
787fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
788fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // Call the callback method
7899d98a089756b692eefeae7f92cca15e121196e0cManuel Roman    IfStatement* ifst = new IfStatement;
7909d98a089756b692eefeae7f92cca15e121196e0cManuel Roman        ifst->expression = new Comparison(new FieldVariable(THIS_VALUE, "callback"), "!=", NULL_VALUE);
7919d98a089756b692eefeae7f92cca15e121196e0cManuel Roman    dispatchMethod->statements->Add(ifst);
7929d98a089756b692eefeae7f92cca15e121196e0cManuel Roman    ifst->statements->Add(realCall);
793fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
794fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
795fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratostatic void
796e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratogenerate_regular_method(const method_type* method, RpcProxyClass* proxyClass,
79795a766ddcd0feda233882012b755947b2588352cJoe Onorato        EndpointBaseClass* serviceBaseClass, ResultDispatcherClass* resultsDispatcherClass,
798e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        int index)
799fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
800fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    arg_type* arg;
801fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
802e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // == the callback interface for results ================================
803e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // the service base class
804e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Type* resultsInterfaceType = generate_results_method(method, proxyClass);
805e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
806e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // == the method in the proxy class =====================================
807e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    generate_proxy_method(method, proxyClass, resultsDispatcherClass, resultsInterfaceType, index);
808e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
809e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // == the method in the result dispatcher class =========================
810e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    if (resultsInterfaceType != NULL) {
811e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        generate_result_dispatcher_method(method, resultsDispatcherClass, resultsInterfaceType,
812e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                index);
813e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    }
814e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
815e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // == The abstract method that the service developers implement ==========
816fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    Method* decl = new Method;
817fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        decl->comment = gather_comments(method->comments_token->extra);
818fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        decl->modifiers = PUBLIC | ABSTRACT;
819fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        decl->returnType = NAMES.Search(method->type.type.data);
820fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        decl->returnTypeDimension = method->type.dimension;
821fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        decl->name = method->name.data;
822fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    arg = method->args;
823fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    while (arg != NULL) {
824fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        decl->parameters.push_back(new Variable(
825fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                            NAMES.Search(arg->type.type.data), arg->name.data,
826fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato                            arg->type.dimension));
827fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        arg = arg->next;
828fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
829b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman
830b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    // Add the default RpcContext param to all methods
831b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    decl->parameters.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
832b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman
833fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    serviceBaseClass->elements.push_back(decl);
834fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
835fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
836e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // == the dispatch method in the service base class ======================
837e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    serviceBaseClass->AddMethod(method);
838e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato}
839fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
840e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratostatic void
841e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratogenerate_event_method(const method_type* method, RpcProxyClass* proxyClass,
84295a766ddcd0feda233882012b755947b2588352cJoe Onorato        EndpointBaseClass* serviceBaseClass, ListenerClass* listenerClass,
84328087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        EventListenerClass* presenterClass, int index)
844e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato{
845e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    arg_type* arg;
846e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    listenerClass->needed = true;
847fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
848e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // == the push method in the service base class =========================
849e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Method* push = new Method;
850e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        push->modifiers = PUBLIC;
851e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        push->name = push_method_name(method->name.data);
852e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        push->statements = new StatementBlock;
853e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        push->returnType = VOID_TYPE;
854e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    serviceBaseClass->elements.push_back(push);
85505ffbe7c020c59ee4fa7e13e04641c954d591951Joe Onorato
856e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // The local variables
857e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* _data = new Variable(RPC_DATA_TYPE, "_data");
858e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    push->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE)));
859fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
860e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // Add the arguments
861fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    arg = method->args;
862fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    while (arg != NULL) {
863e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        // Function signature
864fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        Type* t = NAMES.Search(arg->type.type.data);
865e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
866e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        push->parameters.push_back(v);
86705ffbe7c020c59ee4fa7e13e04641c954d591951Joe Onorato
868e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        // Input parameter marshalling
869e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        generate_write_to_data(t, push->statements,
870e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                new StringLiteralExpression(arg->name.data), v, _data);
87105ffbe7c020c59ee4fa7e13e04641c954d591951Joe Onorato
872e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        arg = arg->next;
873e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    }
874fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
875e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // Send the notifications
876e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    push->statements->Add(new MethodCall("pushEvent", 2,
877e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                new StringLiteralExpression(method->name.data),
878e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                new MethodCall(_data, "serialize")));
879e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
880e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // == the event callback dispatcher method  ====================================
881e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    presenterClass->AddMethod(method);
882e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
883e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // == the event method in the listener base class =====================
884e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Method* event = new Method;
885e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        event->modifiers = PUBLIC;
886e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        event->name = method->name.data;
887e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        event->statements = new StatementBlock;
888e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        event->returnType = VOID_TYPE;
889e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    listenerClass->elements.push_back(event);
890e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    arg = method->args;
891e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    while (arg != NULL) {
892e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        event->parameters.push_back(new Variable(
893e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                            NAMES.Search(arg->type.type.data), arg->name.data,
894e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                            arg->type.dimension));
895fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        arg = arg->next;
896fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
897b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman
898b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    // Add a final parameter: RpcContext. Contains data about
899b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    // incoming request (e.g., certificate)
900b71287f42e7cc164d932562d5ff1ee44e1ae4adeManuel Roman    event->parameters.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
901fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
902fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
903fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratostatic void
904e24dbeafe64278408b3342fcb77756bc7d091791Joe Onoratogenerate_listener_methods(RpcProxyClass* proxyClass, Type* presenterType, Type* listenerType)
905fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
906e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // AndroidAtHomePresenter _presenter;
90728087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    // void startListening(Listener listener) {
90828087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    //     stopListening();
90928087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    //     _presenter = new Presenter(_broker, listener);
91028087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    //     _presenter.startListening(_endpoint);
911e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // }
91228087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    // void stopListening() {
913e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    //     if (_presenter != null) {
91428087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    //         _presenter.stopListening();
915e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    //     }
916e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // }
917e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
918e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* _presenter = new Variable(presenterType, "_presenter");
919e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    proxyClass->elements.push_back(new Field(PRIVATE, _presenter));
920e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
921e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    Variable* listener = new Variable(listenerType, "listener");
922e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
92328087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    Method* startListeningMethod = new Method;
92428087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        startListeningMethod->modifiers = PUBLIC;
92528087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        startListeningMethod->returnType = VOID_TYPE;
92628087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        startListeningMethod->name = "startListening";
92728087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        startListeningMethod->statements = new StatementBlock;
92828087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        startListeningMethod->parameters.push_back(listener);
92928087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    proxyClass->elements.push_back(startListeningMethod);
93028087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato
93128087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    startListeningMethod->statements->Add(new MethodCall(THIS_VALUE, "stopListening"));
93228087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    startListeningMethod->statements->Add(new Assignment(_presenter,
93328087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato                new NewExpression(presenterType, 2, proxyClass->broker, listener)));
93428087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    startListeningMethod->statements->Add(new MethodCall(_presenter,
93528087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato                "startListening", 1, proxyClass->endpoint));
93628087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato
93728087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    Method* stopListeningMethod = new Method;
93828087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        stopListeningMethod->modifiers = PUBLIC;
93928087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        stopListeningMethod->returnType = VOID_TYPE;
94028087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        stopListeningMethod->name = "stopListening";
94128087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato        stopListeningMethod->statements = new StatementBlock;
94228087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    proxyClass->elements.push_back(stopListeningMethod);
943e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
944e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    IfStatement* ifst = new IfStatement;
945e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        ifst->expression = new Comparison(_presenter, "!=", NULL_VALUE);
94628087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    stopListeningMethod->statements->Add(ifst);
947e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
94828087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    ifst->statements->Add(new MethodCall(_presenter, "stopListening"));
949e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    ifst->statements->Add(new Assignment(_presenter, NULL_VALUE));
950fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
951fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
952fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe OnoratoClass*
953fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onoratogenerate_rpc_interface_class(const interface_type* iface)
954fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato{
955fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // the proxy class
956fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    InterfaceType* interfaceType = static_cast<InterfaceType*>(
957fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        NAMES.Find(iface->package, iface->name.data));
958fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    RpcProxyClass* proxy = new RpcProxyClass(iface, interfaceType);
959fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
960e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // the listener class
961e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    ListenerClass* listener = new ListenerClass(iface);
962e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
963e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    // the presenter class
96428087c63d83b51dfd6533040b2ca5edae0bc278aJoe Onorato    EventListenerClass* presenter = new EventListenerClass(iface, listener->type);
965e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato
966fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // the service base class
96795a766ddcd0feda233882012b755947b2588352cJoe Onorato    EndpointBaseClass* base = new EndpointBaseClass(iface);
968fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    proxy->elements.push_back(base);
969fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
970fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // the result dispatcher
971fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    ResultDispatcherClass* results = new ResultDispatcherClass();
972fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
973fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // all the declared methods of the proxy
974fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    int index = 0;
975fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    interface_item_type* item = iface->interface_items;
976fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    while (item != NULL) {
977fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        if (item->item_type == METHOD_TYPE) {
978e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            if (NAMES.Search(((method_type*)item)->type.type.data) == EVENT_FAKE_TYPE) {
979e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                generate_event_method((method_type*)item, proxy, base, listener, presenter, index);
980e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            } else {
981e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato                generate_regular_method((method_type*)item, proxy, base, results, index);
982e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato            }
983fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        }
984fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        item = item->next;
985fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        index++;
986fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
987e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    presenter->DoneWithMethods();
988fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    base->DoneWithMethods();
989fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
990fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    // only add this if there are methods with results / out parameters
991fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    if (results->needed) {
992fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato        proxy->elements.push_back(results);
993fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    }
994e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    if (listener->needed) {
995e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        proxy->elements.push_back(listener);
996e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        proxy->elements.push_back(presenter);
997e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato        generate_listener_methods(proxy, presenter->type, listener->type);
998e24dbeafe64278408b3342fcb77756bc7d091791Joe Onorato    }
999fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato
1000fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato    return proxy;
1001fdfe2ff8c60c367a4eb7cecb4cbe1d62b41a8c20Joe Onorato}
1002