Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/manager
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23725/src/java/net/sf/asterisk/manager
Added Files:
PingThread.java
Log Message:
Added PingThread.
--- NEW FILE: PingThread.java ---
/*
* Copyright 2004-2005 Stefan Reuter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sf.asterisk.manager;
import java.io.IOException;
import net.sf.asterisk.manager.action.PingAction;
import net.sf.asterisk.manager.response.ManagerResponse;
import net.sf.asterisk.util.Log;
import net.sf.asterisk.util.LogFactory;
/**
* A Thread that pings the Asterisk server at a given interval.<br>
* You can use this to prevent the connection being shut down when there is no
* traffic.
*
* @author srt
* @version $Id: PingThread.java,v 1.1 2005/07/16 19:35:19 srt Exp $
*/
public class PingThread extends Thread
{
/**
* Default value for the interval attribute.
*/
private static final long DEFAULT_INTERVAL = 2000;
/**
* Instance logger.
*/
private final Log logger = LogFactory.getLog(getClass());
private long interval;
private boolean die;
private ManagerConnection connection;
/**
* Creates a new PingThread that uses the given ManagerConnection.
*
* @param connection ManagerConnection that is pinged
*/
public PingThread(ManagerConnection connection)
{
this.connection = connection;
this.interval = DEFAULT_INTERVAL;
this.die = false;
setName("Ping");
}
/**
* Adjusts how often a PingAction is sent.<br>
* Default is 2000ms.
*
* @param interval the interval in milliseconds
*/
public void setInterval(long interval)
{
this.interval = interval;
}
/**
* Terminates this PingThread.
*/
public void die()
{
this.die = true;
interrupt();
}
public void run()
{
ManagerResponse response;
while (!die)
{
try
{
sleep(interval);
}
catch (InterruptedException e)
{
// swallow
}
if (die)
{
break;
}
try
{
response = connection.sendAction(new PingAction());
logger.debug("Ping response: " + response);
}
catch (IOException e)
{
logger.warn("IOException on sending Ping", e);
}
catch (TimeoutException e)
{
logger.warn("Timeout on sending Ping", e);
}
}
}
}
|