Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

new Class<T>(params) considered harmful #121

Open
kflynn opened this issue Apr 5, 2016 · 0 comments
Open

new Class<T>(params) considered harmful #121

kflynn opened this issue Apr 5, 2016 · 0 comments

Comments

@kflynn
Copy link
Contributor

kflynn commented Apr 5, 2016

This:

    class Server<T> extends HTTPServlet {

        T impl;
        bool sendCORS;

        Server(T impl) {
            self.impl = impl;
            self.sendCORS = false;
        }

        static Server<T> withCORS(T impl) {
            Server<T> server = new Server<T>(impl);
            server.sendCORS = true;

            return server;
        }

        void onHTTPRequest(HTTPRequest request, HTTPResponse response) {
            String body = request.getBody();
            JSONObject envelope = body.parseJSON();
            if (envelope["$method"] == envelope.undefined() ||
                envelope["rpc"] == envelope.undefined()) {
                response.setBody("Failed to understand request.\n\n" + body + "\n");
                response.setCode(400);
                concurrent.Context.runtime().respond(request, response);
            } else {
                String methodName = envelope["$method"];
                JSONObject json = envelope["rpc"];
                // XXX: contexty stuff
                reflect.Method method = self.getClass().getField("impl").getType().getMethod(methodName);
                List<reflect.Class> params = method.getParameters();
                List<Object> args = [];
                int idx = 0;
                while (idx < params.size()) {
                    args.add(fromJSON(params[idx], null, json.getListItem(idx)));
                    idx = idx + 1;
                }
                concurrent.Future result = ?method.invoke(impl, args);
                result.onFinished(new ServerResponder(self.sendCORS, request, response));
                // XXX: we should also start a timeout here if the impl does not .finish() the result
            }
        }

        void onServletError(String url, String message) {
            concurrent.Context.runtime().fail("RPC Server failed to register " + url + " due to: " + message);
        }

    }

causes quark install --python to hang.

If you comment out this method:

        static Server<T> withCORS(T impl) {
            Server<T> server = new Server<T>(impl);
            server.sendCORS = true;

            return server;
        }

it works Just Fine™.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant