1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.metricshub.wbem.sblim.slp.internal.ua;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 import java.util.ArrayList;
48 import java.util.Iterator;
49 import java.util.LinkedHashSet;
50 import java.util.NoSuchElementException;
51 import org.metricshub.wbem.sblim.slp.internal.TRC;
52 import org.metricshub.wbem.sblim.slp.internal.msg.ReplyMessage;
53
54
55
56
57
58 public class ResultTable implements Iterator<Object> {
59 private ArrayList<DatagramRequester> iRequesters = new ArrayList<DatagramRequester>();
60
61 private ArrayList<Exception> iExceptions = new ArrayList<Exception>();
62
63
64
65
66
67 private LinkedHashSet<Object> iInSet = new LinkedHashSet<Object>();
68
69 private LinkedHashSet<Object> iOutSet = new LinkedHashSet<Object>();
70
71
72
73
74
75
76
77
78 public synchronized void registerRequester(DatagramRequester pReq) {
79 for (int i = 0; i < this.iRequesters.size(); i++) if (pReq == this.iRequesters.get(i)) return;
80 this.iRequesters.add(pReq);
81 }
82
83
84
85
86
87
88
89 public synchronized void unregisterRequester(DatagramRequester pReq) {
90 for (int i = 0; i < this.iRequesters.size(); i++) {
91 if (pReq == this.iRequesters.get(i)) {
92 this.iRequesters.remove(i);
93 if (this.iRequesters.size() == 0) wakeUp();
94 return;
95 }
96 }
97 }
98
99
100
101
102
103
104 public void addResults(ReplyMessage pReplyMsg) {
105 addResults(pReplyMsg.getResultIterator());
106 }
107
108
109
110
111
112
113 public synchronized void addResults(Iterator<?> pResItr) {
114 if (pResItr == null) return;
115 while (pResItr.hasNext()) addResult(pResItr.next());
116
117 if (this.iInSet.size() > 0) wakeUp();
118 }
119
120
121
122
123
124
125 public synchronized void addExceptions(ReplyMessage pReplyMsg) {
126 addExceptions(pReplyMsg.getExceptionIterator());
127 }
128
129
130
131
132
133
134 public synchronized void addExceptions(Iterator<?> pExceptionItr) {
135 if (pExceptionItr == null) return;
136 while (pExceptionItr.hasNext()) addException((Exception) pExceptionItr.next());
137 }
138
139
140
141
142
143
144 public synchronized void addException(Exception pE) {
145 this.iExceptions.add(pE);
146 if (this.iExceptions.size() > 0) wakeUp();
147 }
148
149
150
151
152
153
154 public synchronized int getTotalResponses() {
155 return this.iOutSet.size() + this.iInSet.size();
156 }
157
158
159
160
161
162 public synchronized boolean hasNext() {
163 if (hasData()) return true;
164
165 if (this.iRequesters.size() == 0) return false;
166
167
168
169 try {
170 wait();
171 } catch (InterruptedException e) {
172 TRC.error(e);
173 }
174 return hasData();
175 }
176
177
178
179
180
181 public synchronized Object next() throws NoSuchElementException {
182 Iterator<Object> itr = this.iInSet.iterator();
183 Object res = itr.next();
184 this.iInSet.remove(res);
185 this.iOutSet.add(res);
186 return res;
187 }
188
189
190
191
192
193 public Object nextException() throws NoSuchElementException {
194 Iterator<Exception> itr = this.iExceptions.iterator();
195 Object res = itr.next();
196 this.iExceptions.remove(res);
197 return res;
198 }
199
200
201
202
203 public boolean hasMoreExceptions() {
204 return this.iExceptions.size() > 0;
205 }
206
207 public void remove() {
208 throw new UnsupportedOperationException();
209 }
210
211 private void addResult(Object pResult) {
212 if (this.iOutSet.contains(pResult) || this.iInSet.contains(pResult)) return;
213 this.iInSet.add(pResult);
214 }
215
216 private void wakeUp() {
217 try {
218 notifyAll();
219 } catch (IllegalMonitorStateException e) {
220 TRC.error(e);
221 }
222 }
223
224 private boolean hasData() {
225 return this.iInSet.size() > 0;
226 }
227 }