New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified request composition #17

Closed
laforge49 opened this Issue Oct 4, 2013 · 1 comment

Comments

Projects
None yet
1 participant
@laforge49
Copy link
Owner

laforge49 commented Oct 4, 2013

Currently, if you want to dynamically add (at run time) a request to a blade, you must create a second blade that contains the new request. For example:

static public AsyncRequest<Printer> stdoutAReq(final Facility _facility) throws Exception {
    return new FacilityAgent<Printer>(_facility) {
        protected void start(final AsyncResponseProcessor<Printer> dis) throws Exception {
            Printer printer = (Printer) getProperty("stdout");
            if (printer == null) {
                printer = new Printer(new IsolationReactor(_facility));
                putProperty("stdout", printer);
            }
            dis.processAsyncResponse(printer);
        }
    }.startAReq();
}

So when, for example, calling stdoutAReq(Facility), first you create an anonymous subclass of FacilityAgent and then create the start request. Two objects where really only one is needed.

Instead, we can just create the new request directly:

static public AsyncRequest&lt;Printer&gt; stdoutAReq(final Facility _facility) throws Exception {
    return new AsyncRequest&lt;Printer&gt;(_facility.getReactor()) {
        public void processAsyncRequest() throws Exception {
            Printer printer = (Printer) _facility.getProperty("stdout");
            if (printer == null) {
                printer = new Printer(new IsolationReactor(_facility));
                _facility.putProperty("stdout", printer);
            }
            processAsyncResponse(printer);
        }
    };
}

Now we are only creating a single object. To get this to work cleanly, we should add two methods to RequestBase:

/**
 * Process the request immediately.
 *
 * @param _syncRequest    The request to be processed.
 * @param &lt;RT&gt; The type of value returned.
 * @return The response from the request.
 */
protected &lt;RT&gt; RT local(final SyncRequest&lt;RT&gt; _syncRequest)
        throws Exception {
    return SyncRequest.doLocal(targetReactor, _syncRequest);
}

/**
 * Process the request immediately.
 *
 * @param _request        The request to be processed.
 * @param &lt;RT&gt; The type of value returned.
 */
protected &lt;RT&gt; void send(final RequestBase&lt;RT&gt; _request,
                                    final AsyncResponseProcessor&lt;RT&gt; _responseProcessor)
        throws Exception {
    RequestBase.doSend(targetReactor, _request, _responseProcessor);
}
@laforge49

This comment has been minimized.

Copy link
Owner

laforge49 commented Oct 6, 2013

closed: f4e2e11

@laforge49 laforge49 closed this Oct 6, 2013

@ghost ghost assigned laforge49 Oct 22, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment