View Javadoc
1   // NAME
2   //      $RCSfile: Transmitter.java,v $
3   // DESCRIPTION
4   //      [given below in javadoc format]
5   // DELTA
6   //      $Revision: 3.15 $
7   // CREATED
8   //      $Date: 2007/02/05 15:01:20 $
9   // COPYRIGHT
10  //      Westhawk Ltd
11  // TO DO
12  //
13  
14  /*
15   * Copyright (C) 1996 - 1998 by Westhawk Ltd (www.westhawk.nl)
16   * Copyright (C) 1998 - 2006 by Westhawk Ltd 
17   * <a href="www.westhawk.co.uk">www.westhawk.co.uk</a>
18   *
19   * Permission to use, copy, modify, and distribute this software
20   * for any purpose and without fee is hereby granted, provided
21   * that the above copyright notices appear in all copies and that
22   * both the copyright notice and this permission notice appear in
23   * supporting documentation.
24   * This software is provided "as is" without express or implied
25   * warranty.
26   * author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
27   */
28  
29  package uk.co.westhawk.snmp.stack;
30  
31  /*-
32   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
33   * SNMP Java Client
34   * ჻჻჻჻჻჻
35   * Copyright 2023 MetricsHub, Westhawk
36   * ჻჻჻჻჻჻
37   * This program is free software: you can redistribute it and/or modify
38   * it under the terms of the GNU Lesser General Public License as
39   * published by the Free Software Foundation, either version 3 of the
40   * License, or (at your option) any later version.
41   *
42   * This program is distributed in the hope that it will be useful,
43   * but WITHOUT ANY WARRANTY; without even the implied warranty of
44   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45   * GNU General Lesser Public License for more details.
46   *
47   * You should have received a copy of the GNU General Lesser Public
48   * License along with this program.  If not, see
49   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
50   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
51   */
52  
53  import java.util.*;
54  
55  /**
56   * Transmitter is a thread that sends PDUs, when done
57   * it waits.
58   *
59   * @author <a href="mailto:snmp@westhawk.co.uk">Tim Panton</a>
60   * @version $Revision: 3.15 $ $Date: 2007/02/05 15:01:20 $
61  */
62  class Transmitter extends Object implements Runnable {
63      private static final String version_id = "@(#)$Id: Transmitter.java,v 3.15 2007/02/05 15:01:20 birgita Exp $ Copyright Westhawk Ltd";
64  
65      Pdu pdu = null;
66      Thread me;
67      String myName;
68  
69      Transmitter(String name) {
70          me = new Thread(this, name);
71          me.setPriority(me.MIN_PRIORITY);
72          if (AsnObject.debug > 12) {
73              System.out.println("Transmitter(): Made thread " + name);
74          }
75          myName = name;
76          me.start();
77      }
78  
79      /**
80       * The method for the Runnable interface.
81       * 
82       * It will do a sit() untill stand() wakes it up. The Pdu will then
83       * be transmitted
84       *
85       * @see #sit()
86       * @see #stand()
87       * @see Pdu#transmit()
88       */
89      public void run() {
90          while (me != null) {
91              sit();
92              synchronized (this) {
93                  if (pdu != null) {
94                      pdu.transmit();
95                      // I will say this only once....
96                      pdu = null;
97                  }
98              }
99          }
100     }
101 
102     /**
103      * Returns the string representation of the Transmitter.
104      *
105      * @return The string of the Transmitter
106      */
107     public String toString() {
108         StringBuffer buffer = new StringBuffer(getClass().getName());
109         buffer.append("[");
110         buffer.append("name=").append(myName);
111         buffer.append("]");
112         return buffer.toString();
113     }
114 
115     synchronized void setPdu(Pdu p) {
116         pdu = p;
117     }
118 
119     /**
120      * This method is the counterpart of stand().
121      *
122      * It does not more than waiting to be notified.
123      *
124      * @see #stand()
125      * @see #run()
126      */
127     synchronized void sit() {
128         while ((me != null) && (pdu == null)) {
129             try {
130                 wait();
131             } catch (InterruptedException iw) {
132                 ;
133             }
134 
135         }
136     }
137 
138     /**
139      * This method is the counterpart of sit().
140      *
141      * The Pdu will call this method when it is sent.
142      * This method will notify itself and in the end it is transmitted
143      * in run.
144      *
145      * @see #sit()
146      * @see #run()
147      * @see Pdu#send
148      */
149     synchronized void stand() {
150         notifyAll();
151     }
152 
153     /**
154      * It may be sleeping (as opposed to wait()ing
155      * so send it a kick.
156      */
157     void interruptMe() {
158         // unsafe ?
159         if (me != null) {
160             me.interrupt();
161         }
162     }
163 
164     void destroy() {
165         me = null;
166         pdu = null;
167         stand();
168     }
169 }