annotate src/main/java/jp/ac/u_ryukyu/treevnc/MulticastQueue.java @ 518:c4d1a275b7d5

fx
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 22 Feb 2019 14:19:39 +0900
parents 57e0d052b126
children
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;
518
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 492
diff changeset
4 import java.nio.ByteOrder;
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 import java.util.concurrent.CountDownLatch;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 public class MulticastQueue<T>
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 {
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
9
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 Node<T> tail;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
12 /**
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
13 * Multicastcast Queue
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
14 * Pass a data to multiple clients.
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
15 * element Node T
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
16 * 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
17 */
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 public MulticastQueue()
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 tail = new Node<T>(null);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 }
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
22
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
23 /**
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
24 * @param size
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
25 * @return
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
26 *
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
27 * try to allocate byteBuffer.
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
28 * wait until heap is available.
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
29 */
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
30 public ByteBuffer allocate(int size)
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
31 {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
32 ByteBuffer b=null;
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
33 while(true){
76
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
34 try {
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
35 b = ByteBuffer.allocate(size);
76
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
36 } catch (OutOfMemoryError e) {
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
37 b = null;
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
38 System.err.println("multicastqueue : wait for heap : " + e);
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
39 }
76
ef3586035d25 minor change
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
40 if (b!=null) {
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
41 break;
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
42 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
43 try {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
44 wait();
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
45 } catch (InterruptedException e) {
354
7ef4ac588459 remove flag in writeToClinet
oc
parents: 331
diff changeset
46 System.out.println("thread has interrupted the current thread.");
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
47 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
48 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
49 return b;
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
50 }
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
74
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
52 public synchronized void heapAvailable() {
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
53 notifyAll();
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
54 }
f1938dd3b518 don't heap space error.
oc
parents: 12
diff changeset
55
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
56 /**
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
57 * put item to the queue
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
58 * all client threads start read it
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
59 * @param item
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
60 */
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 public synchronized void put(T item)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 Node<T> next = new Node<T>(item);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 tail.set(next);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 tail = next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 }
492
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
67
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
68 /**
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
69 * waitput item to the queue
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
70 * all client threads start read it
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
71 * @param item
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
72 */
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
73 public synchronized void waitput(T item) throws InterruptedException
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
74 {
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
75 Node<T> next = new Node<T>(item);
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
76 tail.set(next);
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
77 tail = next;
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
78 wait(); //wait for send completion
57e0d052b126 add blockedReadSendData
oshiro
parents: 354
diff changeset
79 }
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
81 /**
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
82 * 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
83 * @return
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
84 */
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 public Client<T> newClient()
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 return new Client<T>(tail);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
90 /**
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
91 * @author kono
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
92 * Inner Client class
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
93 * @param <T>
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
94 */
4
b32668b8e83c create multicast function
one
parents: 3
diff changeset
95 public static class Client<T>
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> node;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 Client(Node<T> tail)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 node = tail;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
104 /**
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
105 * 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
106 * 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
107 * 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
108 * @return
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
109 */
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
110 public T poll()
3
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 Node<T> next = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 T item = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 do {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 try {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 next = node.next();
354
7ef4ac588459 remove flag in writeToClinet
oc
parents: 331
diff changeset
117 } catch(InterruptedException _e) {
7ef4ac588459 remove flag in writeToClinet
oc
parents: 331
diff changeset
118 System.out.println("thread has interrupted the current thread.");
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 continue;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 item = next.getItem();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 node = next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 } while ( item == null);
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 return item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 }
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 static class Node<T>
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 private T item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 private Node<T> next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 private CountDownLatch latch;
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 public Node(T item)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 this.item = item;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 this.next = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 latch = new CountDownLatch(1);
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 T getItem() {
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 return item;
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 public void set(Node<T> next)
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 this.next = next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 latch.countDown();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 public Node<T> next() throws InterruptedException
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 {
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 latch.await();
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 return next;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
156
86
f6946d4fe926 add comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
157 public void clear() {
3
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 item = null;
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 }
e7ce2b2ffed8 add and modify files
Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 }