aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/src/com/yahoo/jrt/InvocationServer.java
blob: 7704c0019ed911d149787da1fb2edda0da121899 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;


class InvocationServer {

    private Connection conn;
    private Request    request;
    private Method     method;
    private int        replyKey;
    private boolean    noReply;
    private TieBreaker done;

    public InvocationServer(Connection conn, Request request, Method method,
                            int replyKey, boolean noReply) {

        this.conn = conn;
        this.request = request;
        this.method = method;
        this.replyKey = replyKey;
        this.noReply = noReply;
        request.serverHandler(this);

        done = conn.startRequest();
    }

    public Target getTarget() {
        return conn;
    }

    public void invoke() {
        if (method != null) {
            if (method.checkParameters(request)) {
                if (method.requestAccessFilter().allow(request)) {
                    method.invoke(request);
                } else {
                    request.setError(ErrorCode.PERMISSION_DENIED, "Permission denied");
                }
            } else {
                request.setError(ErrorCode.WRONG_PARAMS, "Parameters in " + request + " does not match " + method);
            }
        } else {
            request.setError(ErrorCode.NO_SUCH_METHOD, "No such method");
        }
        if (!request.isDetached()) {
            returnRequest();
        }
    }

    public void returnRequest() {
        if (!conn.completeRequest(done)) {
            throw new IllegalStateException("Request already returned");
        }
        if (noReply) {
            return;
        }
        if (!request.isError() && !method.checkReturnValues(request)) {
            request.setError(ErrorCode.WRONG_RETURN, "Return values in " + request + " does not match " + method);
        }
        if (request.isError()) {
            conn.postPacket(new ErrorPacket(0, replyKey,
                                            request.errorCode(),
                                            request.errorMessage()));
        } else {
            conn.postPacket(new ReplyPacket(0, replyKey,
                                            request.returnValues()));
        }
    }
}