1 // NAME
2 // $RCSfile: RawPduReceivedSupport.java,v $
3 // DESCRIPTION
4 // [given below in javadoc format]
5 // DELTA
6 // $Revision: 1.5 $
7 // CREATED
8 // $Date: 2006/02/09 14:30:18 $
9 // COPYRIGHT
10 // Westhawk Ltd
11 // TO DO
12 //
13
14 /*
15 * Copyright (C) 2005 - 2006 by Westhawk Ltd
16 * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
17 *
18 * Permission to use, copy, modify, and distribute this software
19 * for any purpose and without fee is hereby granted, provided
20 * that the above copyright notices appear in all copies and that
21 * both the copyright notice and this permission notice appear in
22 * supporting documentation.
23 * This software is provided "as is" without express or implied
24 * warranty.
25 * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
26 */
27 package uk.co.westhawk.snmp.event;
28
29 /*-
30 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
31 * SNMP Java Client
32 * ჻჻჻჻჻჻
33 * Copyright 2023 MetricsHub, Westhawk
34 * ჻჻჻჻჻჻
35 * This program is free software: you can redistribute it and/or modify
36 * it under the terms of the GNU Lesser General Public License as
37 * published by the Free Software Foundation, either version 3 of the
38 * License, or (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Lesser Public License for more details.
44 *
45 * You should have received a copy of the GNU General Lesser Public
46 * License along with this program. If not, see
47 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
48 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
49 */
50
51 import java.util.*;
52 import uk.co.westhawk.snmp.stack.*;
53
54 /**
55 * This is a utility class that can be used by classes that support
56 * raw pdu listener functionality.
57 * You can use an instance of this class as a member field
58 * of your class and delegate various work to it.
59 *
60 * @since 4_14
61 * @author <a href="mailto:snmp@westhawk.co.uk">Birgit Arkesteijn</a>
62 * @version $Revision: 1.5 $ $Date: 2006/02/09 14:30:18 $
63 */
64 public class RawPduReceivedSupport {
65 public static final String version_id = "@(#)$Id: RawPduReceivedSupport.java,v 1.5 2006/02/09 14:30:18 birgit Exp $ Copyright Westhawk Ltd";
66
67 private Object source;
68 private transient Vector pduListeners;
69
70 /**
71 * The constructor.
72 *
73 * @param src The source (ListeningContext) of the pdu events when they are
74 * fired.
75 */
76 public RawPduReceivedSupport(Object src) {
77 source = src;
78 }
79
80 /**
81 * Removes all the listeners.
82 */
83 public synchronized void empty() {
84 if (pduListeners != null) {
85 pduListeners.removeAllElements();
86 }
87 }
88
89 /**
90 * Returns the number of listeners.
91 *
92 * @return The number of listeners.
93 */
94 public synchronized int getListenerCount() {
95 int c = 0;
96 if (pduListeners != null) {
97 c = pduListeners.size();
98 }
99 return c;
100 }
101
102 /**
103 * Adds the specified pdu listener to receive pdus.
104 */
105 public synchronized void addRawPduListener(RawPduListener listener) {
106 if (pduListeners == null) {
107 pduListeners = new Vector(5);
108 }
109 if (pduListeners.contains(listener) == false) {
110 pduListeners.addElement(listener);
111 }
112 }
113
114 /**
115 * Removes the specified pdu listener.
116 */
117 public synchronized void removeRawPduListener(RawPduListener listener) {
118 if (pduListeners != null) {
119 pduListeners.removeElement(listener);
120 }
121 }
122
123 /**
124 * Fires an undecoded pdu event.
125 * The event is fired to all listeners, unless one of them consumes it.
126 * The idea is that for undecoded pdus it is very unlikely that more
127 * than one party (usually SnmpContext objects) is interested.
128 *
129 * @param version The SNMP version of the pdu
130 * @param hostAddress The IP address of the host where the pdu came from
131 * @param hostPort The remote port number of the host where the pdu came from
132 * @param message The pdu in bytes
133 *
134 * @return Whether or not the event has been consumed.
135 */
136 public boolean fireRawPduReceived(int version, String hostAddress, int hostPort, byte[] message) {
137 boolean isConsumed = false;
138 Vector copyOfListeners = null;
139 if (pduListeners != null) {
140 synchronized (pduListeners) {
141 copyOfListeners = (Vector) pduListeners.clone();
142 }
143 }
144
145 if (copyOfListeners != null) {
146 int l = message.length;
147 int sz = copyOfListeners.size();
148 for (int i = sz - 1; i >= 0 && isConsumed == false; i--) {
149 RawPduListener listener = (RawPduListener) copyOfListeners.elementAt(i);
150
151 byte[] copyOfMessage = new byte[l];
152 System.arraycopy(message, 0, copyOfMessage, 0, l);
153 RawPduEvent evt = new RawPduEvent(source, version, hostAddress, copyOfMessage, hostPort);
154 listener.rawPduReceived(evt);
155 isConsumed = (evt.isConsumed());
156 }
157 }
158 return isConsumed;
159 }
160
161 }