1 package org.simantics.databoard.channel;
\r
3 import java.io.IOException;
\r
4 import java.util.concurrent.Executor;
\r
5 import java.util.concurrent.TimeUnit;
\r
6 import java.util.concurrent.TimeoutException;
\r
8 import org.simantics.databoard.binding.Binding;
\r
11 * Delayed response object. The result or error may be or become available to
\r
12 * this object at any given time.
\r
14 * This interface is intended to be used by clients that make service requests
\r
15 * over command channel.
\r
17 * @author Toni Kalajainen <toni.kalajainen@iki.fi>
\r
19 public interface AsyncRequest {
\r
22 * Get the current request status. <p>
\r
24 * If Succeeded or Failed, then result or error is available and can be read
\r
27 * Succeeded and Failed statuses are final. <p>
\r
29 enum Status {Waiting, Succeed, Failed} Status getStatus();
\r
32 * Get result if available.
\r
35 * @return result or <tt>null</tt>
\r
36 * @throws RequestException
\r
38 Object getResult(Binding binding) throws RequestException;
\r
41 * Get error if available.
\r
43 * Cause is (typically) one of these:
\r
44 * o Communication problems {@link IOException}
\r
45 * o Timeout {@link TimeoutException}
\r
46 * o Service(handler) problem {@link ServiceException}
\r
48 * @return error or <tt>null</tt>
\r
50 Exception getError();
\r
53 * Synchronous wait for result until default timeout.
\r
54 * Default timeout is configured to the channel.
\r
55 * If timeout occurs TimeoutException is thrown wrapped in RequestException.
\r
57 * @param binding the format for the result
\r
58 * @return the result object
\r
59 * @throws RequestException
\r
61 Object waitForResult(Binding binding) throws RequestException;
\r
64 * Wait for result or break after until timeout.
\r
65 * If timeout occurs TimeoutException is thrown wrapped in RequestException.
\r
71 * @throws RequestException
\r
73 Object waitForResult(Binding binding, long timeout, TimeUnit unit) throws RequestException;
\r
76 * Set a listener. If the result is already available, the event
\r
77 * schedueled immediately.
\r
79 * @param listener (listener may not block) or null to remove listener
\r
81 void setListener(RequestListener listener);
\r
83 interface RequestListener {
\r
86 * Get thread where the listening is to be processed
\r
88 * @return thread to run listener events
\r
90 Executor getThread();
\r
93 * Request completed, the result is available
\r
95 * @param result the result
\r
97 void onCompleted(Object result);
\r
100 * There was an error in processing the request
\r
102 * @param error the error
\r
104 void onError(ServiceException error);
\r