Mercurial > hg > Members > nobuyasu > tightVNCProxy
annotate src/myVncProxy/MulticastQueue.java @ 137:e97c0f82eaf0
modify MyRfbProto
author | e085711 |
---|---|
date | Tue, 30 Aug 2011 19:01:36 +0900 |
parents | 7e60020f0a72 |
children | db5f735fd2b4 |
rev | line source |
---|---|
56
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 package myVncProxy; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 import java.util.concurrent.CountDownLatch; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 public class MulticastQueue<T> |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 Node<T> tail; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 public MulticastQueue() |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 tail = new Node<T>(null); |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 } |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 public synchronized void put(T item) |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 Node<T> next = new Node<T>(item); |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 tail.set(next); |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 tail = next; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 } |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 public Client<T> newClient() |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 return new Client<T>(tail); |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 } |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 static class Client<T> |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 Node<T> node; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 Client(Node<T> tail) |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 node = tail; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 } |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 |
111 | 36 synchronized public T poll() |
56
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 Node<T> next = null; |
109
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
82
diff
changeset
|
39 T item = null; |
82
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
40 do { |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
41 try { |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
42 next = node.next(); |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
43 }catch(InterruptedException _e){ |
109
3f73ebf918bd
add time out to avoid memory overlow caused by suspended clients.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
82
diff
changeset
|
44 continue; |
82
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
45 } |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
46 item = node.getItem(); |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
47 node = next; |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
48 } while ( item == null); |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
49 return item; |
56
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 } |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 } |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 |
82
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
53 static class Node<T> |
56
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 private T item; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 private Node<T> next; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 private CountDownLatch latch; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 public Node(T item) |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 this.item = item; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 this.next = null; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 latch = new CountDownLatch(1); |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 } |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 |
82
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
66 synchronized public T getItem() { |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
67 return item; |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
68 } |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
69 |
56
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 public void set(Node<T> next) |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 this.next = next; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 latch.countDown(); |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74 } |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76 public Node<T> next() throws InterruptedException |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 { |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 latch.await(); |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 return next; |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80 } |
82
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
81 |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
82 synchronized public void clear() { |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
83 item = null; |
0cbe556e2c54
remove item to reduce memory
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
84 } |
56
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 } |
b2604c5c6a25
add MulticastQueue.java
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 } |