1 | import java.util.LinkedList;
|
---|
2 |
|
---|
3 | import org.nbk4.gwt.action.client.ActionRequest;
|
---|
4 |
|
---|
5 |
|
---|
6 | public abstract class ActionResponseHandler {
|
---|
7 | //there may be some sort of concurrency issue to deal with - we'll figure that out later
|
---|
8 | private static Object monitor = new Object();
|
---|
9 | private static LinkedList<ActionResponseHandler> registeredHandlers = new LinkedList<ActionResponseHandler>();
|
---|
10 |
|
---|
11 | private static long getRelativeTimeOutDate(long timeOut) {
|
---|
12 | if(timeOut <= 0) {
|
---|
13 | return 0;
|
---|
14 | }
|
---|
15 | else {
|
---|
16 | return (System.currentTimeMillis() / 1000L) + timeOut;
|
---|
17 | }
|
---|
18 | }
|
---|
19 |
|
---|
20 | public static void addHandler(ActionResponseHandler handler) {
|
---|
21 | synchronized(monitor) {
|
---|
22 | registeredHandlers.add(handler);
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public static boolean handleIfResponse(ActionRequest request) {
|
---|
27 | LinkedList<ActionResponseHandler> opt_filter = new LinkedList<ActionResponseHandler>();
|
---|
28 |
|
---|
29 | //don't want any changes to the object as we use it
|
---|
30 | synchronized(monitor) {
|
---|
31 | boolean done = false;
|
---|
32 | for (ActionResponseHandler handler : registeredHandlers) {
|
---|
33 | if(handler.isTimedOut()) {
|
---|
34 | //we've handled this event - nothing else to do here
|
---|
35 | }
|
---|
36 | else if (!done && handler.tryToHandle(request)) {
|
---|
37 | done = true;
|
---|
38 | }
|
---|
39 | else {
|
---|
40 | opt_filter.add(handler);
|
---|
41 | }
|
---|
42 | }
|
---|
43 | registeredHandlers = opt_filter;
|
---|
44 | }
|
---|
45 | //no matching event handler found
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /*FIELDS*/
|
---|
50 | private long timeOutDate = 0;
|
---|
51 |
|
---|
52 | /*FUNCTIONS*/
|
---|
53 | //timeOut in seconds: use zero, or anything less than 0, for no timeout
|
---|
54 | //If a response is received, then any non-answering call past the timeout will be destructed!
|
---|
55 | protected ActionResponseHandler(long timeOut) {
|
---|
56 | timeOutDate = getRelativeTimeOutDate(timeOut);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | *tryToHandle: should have presupplied a target to handle.
|
---|
61 | *When getting the request, handle it as you decide. Return true to mark that it was
|
---|
62 | *handled, which allows the managing class to destroy this handler.
|
---|
63 | */
|
---|
64 | public abstract boolean tryToHandle(ActionRequest request);
|
---|
65 |
|
---|
66 | //Implementation of this entirely up to end user? Who knows. Set a timeout, destroy self on timeout>?
|
---|
67 |
|
---|
68 | //TODO: convert to milliseconds, change parameter to indicate milliseconds
|
---|
69 | public boolean isTimedOut() {
|
---|
70 | if (timeOutDate != 0 && System.currentTimeMillis() / 1000L >= timeOutDate) {
|
---|
71 | //TODO: Logger
|
---|
72 | System.err.println("DEBUG: Handler has timed out: ");
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 | }
|
---|