annotate src/main/java/jp/ac/u_ryukyu/treevnc/MulticastQueue.java @ 111:a7988d3c0266

minor fix.
author oc
date Fri, 23 May 2014 20:24:43 +0900
parents f6946d4fe926
children 42fcc9419498
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
12c3a73be47f rename package
one
parents: 4
diff changeset
1 package jp.ac.u_ryukyu.treevnc;
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
3 import java.nio.ByteBuffer;
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 import java.util.concurrent.CountDownLatch;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 public class MulticastQueue<T>
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 {
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
8
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 Node<T> tail;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
11 /**
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
12 * Multicastcast Queue
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
13 * Pass a data to multiple clients.
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
14 * element Node T
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
15 * Another time out thread should be used to limit the total size.
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
16 */
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 public MulticastQueue()
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 tail = new Node<T>(null);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 }
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
21
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
22 /**
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
23 * @param size
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
24 * @return
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
25 *
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
26 * try to allocate byteBuffer.
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
27 * wait until heap is available.
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
28 */
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
29 public ByteBuffer allocate(int size)
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
30 {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
31 ByteBuffer b=null;
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
32 while(true){
76
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
33 try {
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
34 b = ByteBuffer.allocate(size);
76
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
35 } catch (OutOfMemoryError e) {
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
36 b = null;
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
37 System.err.println("multicastqueue : wait for heap : " + e);
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
38 }
76
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
39 if (b!=null) {
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
40 break;
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
41 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
42 try {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
43 wait();
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
44 } catch (InterruptedException e) {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
45 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
46 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
47 return b;
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
48 }
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
50 public synchronized void heapAvailable() {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
51 notifyAll();
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
52 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
53
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
54 /**
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
55 * put item to the queue
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
56 * all client threads start read it
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
57 * @param item
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
58 */
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 public synchronized void put(T item)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 Node<T> next = new Node<T>(item);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 tail.set(next);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 tail = next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
66 /**
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
67 * register new clients. Clients read this queue, if all clients read the queue, item is removed
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
68 * @return
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
69 */
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 public Client<T> newClient()
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 return new Client<T>(tail);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
75 /**
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
76 * @author kono
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
77 * Inner Client class
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
78 * @param <T>
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
79 */
4
b32668b8e83c create multicast function
one
parents: 3
diff changeset
80 public static class Client<T>
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 Node<T> node;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 Client(Node<T> tail)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 node = tail;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
89 /**
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
90 * try to read next item, if not available, wait for the next item
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
91 * All clients wait for a CountDownLatch in the next item.
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
92 * set operation count down it, and all clients get the item.
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
93 * @return
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
94 */
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
95 public T poll()
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 Node<T> next = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 T item = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 do {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 try {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 next = node.next();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 }catch(InterruptedException _e){
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 continue;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 item = next.getItem();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 node = next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 } while ( item == null);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 return item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 static class Node<T>
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 private T item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 private Node<T> next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 private CountDownLatch latch;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
117
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 public Node(T item)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 this.item = item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 this.next = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 latch = new CountDownLatch(1);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
125 public T getItem() {
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 return item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
128
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 public void set(Node<T> next)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 this.next = next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 latch.countDown();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
134
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 public Node<T> next() throws InterruptedException
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 latch.await();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 return next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
140
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
141 public void clear() {
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 item = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 }