View Javadoc

1   /*
2    * Copyright  2004-2005 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package net.sf.asterisk.manager;
18  
19  import net.sf.asterisk.manager.action.PingAction;
20  import net.sf.asterisk.manager.response.ManagerResponse;
21  import net.sf.asterisk.util.Log;
22  import net.sf.asterisk.util.LogFactory;
23  
24  /***
25   * A Thread that pings the Asterisk server at a given interval.<br>
26   * You can use this to prevent the connection being shut down when there is no
27   * traffic.
28   * 
29   * @author srt
30   * @version $Id: PingThread.java,v 1.2 2005/07/16 20:07:49 srt Exp $
31   */
32  public class PingThread extends Thread
33  {
34      /***
35       * Default value for the interval attribute.
36       */
37      private static final long DEFAULT_INTERVAL = 2000;
38  
39      /***
40       * Instance logger.
41       */
42      private final Log logger = LogFactory.getLog(getClass());
43  
44      private long interval;
45      private boolean die;
46      private ManagerConnection connection;
47  
48      /***
49       * Creates a new PingThread that uses the given ManagerConnection.
50       * 
51       * @param connection ManagerConnection that is pinged
52       */
53      public PingThread(ManagerConnection connection)
54      {
55          this.connection = connection;
56          this.interval = DEFAULT_INTERVAL;
57          this.die = false;
58          setName("Ping");
59      }
60  
61      /***
62       * Adjusts how often a PingAction is sent.<br>
63       * Default is 2000ms.
64       * 
65       * @param interval the interval in milliseconds
66       */
67      public void setInterval(long interval)
68      {
69          this.interval = interval;
70      }
71  
72      /***
73       * Terminates this PingThread.
74       */
75      public void die()
76      {
77          this.die = true;
78          interrupt();
79      }
80  
81      public void run()
82      {
83          ManagerResponse response;
84  
85          while (!die)
86          {
87              try
88              {
89                  sleep(interval);
90              }
91              catch (InterruptedException e)
92              {
93                  // swallow
94              }
95  
96              if (die)
97              {
98                  break;
99              }
100 
101             try
102             {
103                 response = connection.sendAction(new PingAction());
104                 logger.debug("Ping response: " + response);
105             }
106             catch (Exception e)
107             {
108                 logger.warn("Exception on sending Ping", e);
109             }
110         }
111     }
112 }