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
26
27
28 package uk.co.westhawk.snmp.stack;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 import java.util.*;
53 import uk.co.westhawk.snmp.event.*;
54
55
56
57
58
59
60
61
62
63
64
65 public class ListeningContextPool implements ListeningContextFace {
66 private static final String version_id = "@(#)$Id: ListeningContextPool.java,v 3.7 2009/03/05 13:27:41 birgita Exp $ Copyright Westhawk Ltd";
67
68 protected static Hashtable contextPool;
69
70 protected ListeningContext context = null;
71 protected String socketType;
72 protected String bindAddr;
73 protected int hostPort;
74
75
76
77
78
79
80
81
82
83 public ListeningContextPool(int port) {
84 this(port, null, SnmpContextBasisFace.STANDARD_SOCKET);
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 public ListeningContextPool(int port, String bindAddress) {
107 this(port, bindAddress, SnmpContextBasisFace.STANDARD_SOCKET);
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public ListeningContextPool(int port, String bindAddress, String typeSocket) {
141 initPools();
142 hostPort = port;
143 bindAddr = bindAddress;
144 socketType = typeSocket;
145
146 context = getMatchingContext();
147 }
148
149 private static synchronized void initPools() {
150 if (contextPool == null) {
151 contextPool = new Hashtable(5);
152 }
153 }
154
155 public int getPort() {
156 return hostPort;
157 }
158
159 public String getBindAddress() {
160 return bindAddr;
161 }
162
163 public String getTypeSocket() {
164 return socketType;
165 }
166
167 public int getMaxRecvSize() {
168 int res = SnmpContextBasisFace.MSS;
169 if (context != null) {
170 res = context.getMaxRecvSize();
171 }
172 return res;
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186 public void setMaxRecvSize(int no) {
187 if (context == null) {
188 context = getMatchingContext();
189 }
190 context.setMaxRecvSize(no);
191 }
192
193
194
195
196
197
198
199
200
201
202
203
204
205 public void destroy() {
206 synchronized (contextPool) {
207 if (context != null) {
208 String hashKey = context.getHashKey();
209
210 int count = 0;
211 Item item = (Item) contextPool.get(hashKey);
212
213 if (item != null) {
214 count = item.getCounter();
215 count--;
216 item.setCounter(count);
217 }
218
219 if (count <= 0) {
220 contextPool.remove(hashKey);
221 context.destroy();
222 }
223 context = null;
224 }
225 }
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239 public void destroyPool() {
240 Hashtable copyOfPool = null;
241
242 synchronized (contextPool) {
243 synchronized (contextPool) {
244 copyOfPool = (Hashtable) contextPool.clone();
245 }
246 contextPool.clear();
247 }
248 context = null;
249
250 Enumeration keys = copyOfPool.keys();
251 while (keys.hasMoreElements()) {
252 String key = (String) keys.nextElement();
253 Item item = (Item) copyOfPool.get(key);
254 if (item != null) {
255 ListeningContext cntxt = (ListeningContext) item.getContext();
256 cntxt.destroy();
257 }
258 }
259 copyOfPool.clear();
260 }
261
262
263
264
265
266
267
268
269
270
271 protected ListeningContext getMatchingContext() {
272 Item item = null;
273 ListeningContext newContext = null;
274 String hashKey = getHashKey();
275
276 destroy();
277 synchronized (contextPool) {
278 int count = 0;
279 if (contextPool.containsKey(hashKey)) {
280 item = (Item) contextPool.get(hashKey);
281 newContext = item.getContext();
282 count = item.getCounter();
283 } else {
284 newContext = new ListeningContext(hostPort, bindAddr, socketType);
285 item = new Item(newContext);
286 contextPool.put(hashKey, item);
287 }
288 count++;
289 item.setCounter(count);
290 }
291 return newContext;
292 }
293
294
295
296
297
298
299 public void dumpContexts(String title) {
300 Hashtable copyOfPool = null;
301 synchronized (contextPool) {
302 copyOfPool = (Hashtable) contextPool.clone();
303 }
304
305 System.out.println(title + " " + copyOfPool.size());
306 Enumeration keys = copyOfPool.keys();
307 int i = 0;
308 while (keys.hasMoreElements()) {
309 String key = (String) keys.nextElement();
310 Item item = (Item) copyOfPool.get(key);
311
312 if (item != null) {
313 int count = item.getCounter();
314 ListeningContext cntxt = item.getContext();
315
316 System.out.println("\tcontext: " + key + ", count: " + count
317 + ", index: " + i + ", " + cntxt.toString());
318 if (cntxt == context) {
319 System.out.println("\t\tcurrent context");
320 }
321 i++;
322 }
323 }
324 }
325
326
327
328
329
330
331
332 public String getHashKey() {
333 String str = hostPort
334 + "_" + bindAddr
335 + "_" + socketType;
336 return str;
337 }
338
339 public void addRawPduListener(RawPduListener l)
340 throws java.io.IOException {
341 if (context != null) {
342 context.addRawPduListener(l);
343 }
344 }
345
346 public void removeRawPduListener(RawPduListener l) {
347 if (context != null) {
348 context.removeRawPduListener(l);
349 }
350 }
351
352
353
354
355
356
357 public void removeRawPduListenerFromPool(RawPduListener l) {
358 Hashtable copyOfPool = null;
359
360 if (contextPool != null) {
361 synchronized (contextPool) {
362 copyOfPool = (Hashtable) contextPool.clone();
363 }
364
365 Enumeration keys = copyOfPool.keys();
366 while (keys.hasMoreElements()) {
367 String key = (String) keys.nextElement();
368 Item item = (Item) copyOfPool.get(key);
369
370 if (item != null) {
371 ListeningContext cntxt = item.getContext();
372 cntxt.removeRawPduListener(l);
373 }
374 }
375 }
376 }
377
378 public void addUnhandledRawPduListener(RawPduListener l)
379 throws java.io.IOException {
380 if (context != null) {
381 context.addUnhandledRawPduListener(l);
382 }
383 }
384
385 public void removeUnhandledRawPduListener(RawPduListener l) {
386 if (context != null) {
387 context.removeUnhandledRawPduListener(l);
388 }
389 }
390
391
392
393
394
395
396 public String toString() {
397 String res = "";
398 if (context != null) {
399 res = context.toString();
400 }
401 return res;
402 }
403
404 class Item {
405 private ListeningContext context = null;
406 private int counter = 0;
407
408
409
410
411
412
413 Item(ListeningContext con) {
414 context = con;
415 counter = 0;
416 }
417
418 ListeningContext getContext() {
419 return context;
420 }
421
422 int getCounter() {
423 return counter;
424 }
425
426 void setCounter(int i) {
427 counter = i;
428 }
429
430
431
432
433
434
435 public String toString() {
436 StringBuffer buffer = new StringBuffer("Item[");
437 buffer.append("context=").append(context.toString());
438 buffer.append(", counter=").append(counter);
439 buffer.append("]");
440 return buffer.toString();
441 }
442 }
443
444 }