View Javadoc
1   // NAME
2   //      $RCSfile: RequestPduReceivedSupport.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 1.4 $
7   // CREATED
8   //      $Date: 2006/01/17 17:59:33 $
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   * request 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.4 $ $Date: 2006/01/17 17:59:33 $
63   */
64  public class RequestPduReceivedSupport {
65      public static final String version_id = "@(#)$Id: RequestPduReceivedSupport.java,v 1.4 2006/01/17 17:59:33 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 (SnmpContext) of the pdu events when they are fired.
74       */
75      public RequestPduReceivedSupport(Object src) {
76          source = src;
77      }
78  
79      /**
80       * Removes all the listeners.
81       */
82      public synchronized void empty() {
83          if (pduListeners != null) {
84              pduListeners.removeAllElements();
85          }
86      }
87  
88      /**
89       * Returns the number of listeners.
90       *
91       * @return The number of listeners.
92       */
93      public synchronized int getListenerCount() {
94          int c = 0;
95          if (pduListeners != null) {
96              c = pduListeners.size();
97          }
98          return c;
99      }
100 
101     /**
102      * Adds the specified pdu listener to receive pdus.
103      */
104     public synchronized void addRequestPduListener(RequestPduListener listener) {
105         if (pduListeners == null) {
106             pduListeners = new Vector(5);
107         }
108         if (pduListeners.contains(listener) == false) {
109             pduListeners.addElement(listener);
110         }
111     }
112 
113     /**
114      * Removes the specified pdu listener.
115      */
116     public synchronized void removeRequestPduListener(RequestPduListener listener) {
117         if (pduListeners != null) {
118             pduListeners.removeElement(listener);
119         }
120     }
121 
122     /**
123      * Fires a decoded pdu event.
124      * The event is fired to all listeners, whether they consume it or not.
125      * 
126      * @param pdu The decoded pdu pdu.
127      */
128     public void fireRequestPduReceived(Pdu pdu, int hostPort) {
129         Vector copyOfListeners = null;
130         if (pduListeners != null) {
131             synchronized (pduListeners) {
132                 copyOfListeners = (Vector) pduListeners.clone();
133             }
134         }
135 
136         if (copyOfListeners != null) {
137             int sz = copyOfListeners.size();
138             for (int i = sz - 1; i >= 0; i--) {
139                 RequestPduListener listener = (RequestPduListener) copyOfListeners.elementAt(i);
140 
141                 RequestPduEvent evt = new RequestPduEvent(source, pdu, hostPort);
142                 listener.requestPduReceived(evt);
143             }
144         }
145     }
146 
147 }