Debian Codegraph @

src/java/org/astrogrid/samp/web/WebCallableClient.java

raw
1 package org.astrogrid.samp.web;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6 import org.astrogrid.samp.Message;
7 import org.astrogrid.samp.Response;
8 import org.astrogrid.samp.client.CallableClient;
9 import org.astrogrid.samp.client.SampException;
10
11 /**
12 * CallableClient implementation used internally by the Web Profile hub.
13 *
14 * @author Mark Taylor
15 * @since 2 Feb 2011
16 */
17 class WebCallableClient implements CallableClient {
18
19 private final List queue_;
20 private final int capacity_;
21 private boolean ended_;
22
23 /** Default maximum for queued callbacks. */
24 public final static int DEFAULT_CAPACITY = 4096;
25
26 /**
27 * Constructs a callable client with default maximum capacity.
28 */
29 public WebCallableClient() {
30 this( DEFAULT_CAPACITY );
31 }
32
33 /**
34 * Constructs a callable client with a given maximum callback capacity.
35 *
36 * @param capacity maximum number of queued callbacks
37 */
38 public WebCallableClient( int capacity ) {
39 capacity_ = capacity;
40 queue_ = new ArrayList();
41 }
42
43 /**
44 * Blocks for up to a given number of seconds or until any callbacks
45 * are ready, then returns any ready callbacks.
46 *
47 * @param timeout timeout in seconds
48 * @return list of {@link Callback}-like Maps
49 */
50 public List pullCallbacks( int timeout ) throws SampException {
51
52 // Calculate the timeout epoch as currentTimeMillis.
53 long end = timeout >= 0 ? System.currentTimeMillis() + timeout * 1000
54 : Long.MAX_VALUE;
55
56 // Wait until either there are callbacks or timeout is reached.
57 try {
58 synchronized ( queue_ ) {
59 while ( queue_.isEmpty() &&
60 end - System.currentTimeMillis() > 0 &&
61 ! ended_ ) {
62 queue_.wait( end - System.currentTimeMillis() );
63 }
64
65 // Remove available callbacks from the queue, if any,
66 // and return them.
67 List callbacks = new ArrayList( queue_ );
68 queue_.clear();
69 return callbacks;
70 }
71 }
72 catch ( InterruptedException e ) {
73 throw new SampException( "Interrupted", e );
74 }
75 }
76
77 public void receiveNotification( String senderId, Message message ) {
78 enqueue( "receiveNotification",
79 new Object[] { senderId, message } );
80 }
81
82 public void receiveCall( String senderId, String msgId, Message message ) {
83 enqueue( "receiveCall",
84 new Object[] { senderId, msgId, message } );
85 }
86
87 public void receiveResponse( String responderId, String msgTag,
88 Response response ) {
89 enqueue( "receiveResponse",
90 new Object[] { responderId, msgTag, response } );
91 }
92
93 /**
94 * Informs this client that no further callbacks (receive* methods)
95 * will be made on it.
96 */
97 public void endCallbacks() {
98 ended_ = true;
99 synchronized ( queue_ ) {
100 queue_.notifyAll();
101 }
102 }
103
104 /**
105 * Adds a new callback to the queue which can be passed out via the
106 * {@link #pullCallbacks} method.
107 *
108 * @param methodName callback method name
109 * @param params callback parameter list
110 */
111 private void enqueue( String methodName, Object[] params ) {
112 Callback callback =
113 new Callback( WebClientProfile.WEBSAMP_CLIENT_PREFIX + methodName,
114 Arrays.asList( params ) );
115 callback.check();
116 synchronized ( queue_ ) {
117 if ( queue_.size() < capacity_ ) {
118 queue_.add( callback );
119 queue_.notifyAll();
120 }
121 else {
122 throw new IllegalStateException( "Callback queue is full"
123 + " (" + capacity_
124 + " objects)" );
125 }
126 }
127 }
128 }