Skip to content

Commit

Permalink
Issue #4 adding initial implementations
Browse files Browse the repository at this point in the history
(cherry picked from commit 4305c093cacfb8351b4ed02a205471aa26d82e9f)

Conflicts:
	pom.xml
  • Loading branch information
Jean Deruelle committed Dec 11, 2015
1 parent cbd127d commit c8e921a
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 2 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry combineaccessrules="false" kind="src" path="/commons-congestion"/>
<classpathentry kind="var" path="M2_REPO/javolution/javolution/5.5.1/javolution-5.5.1.jar"/>
<classpathentry kind="var" path="M2_REPO/io/netty/netty-all/4.0.33.Final/netty-all-4.0.33.Final.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/jain-sip"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
17 changes: 15 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<parent>
<groupId>org.mobicents</groupId>
<artifactId>mobicents-parent</artifactId>
<version>2.27.4</version>
<version>2.27.21</version>
</parent>

<groupId>org.mobicents.javax.sip</groupId>
<artifactId>mobicents-jain-sip-ext</artifactId>
<version>1.3.0-SNAPSHOT</version>
<version>1.4.0-SNAPSHOT</version>

<name>Mobicents JAIN SIP Ext</name>
<description>Mobicents JAIN SIP Extensions</description>
Expand Down Expand Up @@ -61,6 +61,19 @@
<version>1.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1</version>
<scope>test</scope>
</dependency>
<!-- for hashed wheel timers and Netty IO layer -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.33.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Conditions Of Use
*
* This software was developed by employees of the National Institute of
* Standards and Technology (NIST), an agency of the Federal Government.
* Pursuant to title 15 Untied States Code Section 105, works of NIST
* employees are not subject to copyright protection in the United States
* and are considered to be in the public domain. As a result, a formal
* license is not needed to use the software.
*
* This software is provided by NIST as a service and is expressly
* provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY. NIST does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement
*
* .
*
*/
package org.mobicents.ext.javax.sip.timers;

import gov.nist.core.CommonLogger;
import gov.nist.core.NamingThreadFactory;
import gov.nist.core.StackLogger;
import gov.nist.javax.sip.SipStackImpl;
import gov.nist.javax.sip.stack.SIPStackTimerTask;
import gov.nist.javax.sip.stack.timers.SipTimer;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;

import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* https://github.com/Mobicents/jain-sip.ext/issues/4
* Implementation of the SIP Timer based on io.netty.util.HashedWheelTimer
*
* @author [email protected]
*
*/
public class HashedWheelSipTimer implements SipTimer {
private static StackLogger logger = CommonLogger.getLogger(HashedWheelSipTimer.class);
protected SipStackImpl sipStackImpl;
HashedWheelTimer hashedWheelSipTimer;
protected AtomicBoolean started = new AtomicBoolean(false);

public HashedWheelSipTimer() {
hashedWheelSipTimer = new HashedWheelTimer(new NamingThreadFactory("hashed_wheel_sip_timer"), 50L, TimeUnit.MILLISECONDS);
}

private class HashedWheelSipTimerTask implements TimerTask {
private SIPStackTimerTask task;
private Timeout timeout;
private long period;

public HashedWheelSipTimerTask(SIPStackTimerTask task, long period) {
this.task= task;
task.setSipTimerTask(this);
this.period = period;
}

public void run(Timeout timeout) {
try {
// task can be null if it has been cancelled
if(task != null) {
task.runTask();
}
if(period > 0) {
timeout = hashedWheelSipTimer.newTimeout(this, period, TimeUnit.MILLISECONDS);
}
} catch (Exception e) {
logger.logError("SIP stack timer task failed due to exception:", e);
}
}

public boolean cancel() {
if(task != null) {
task.cleanUpBeforeCancel();
task = null;
}
return timeout.cancel();
}

/**
* @return the timeout
*/
public Timeout getTimeout() {
return timeout;
}

/**
* @param timeout the timeout to set
*/
public void setTimeout(Timeout timeout) {
this.timeout = timeout;
}
}

/* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#stop()
*/
public void stop() {
started.set(false);
hashedWheelSipTimer.stop();
logger.logStackTrace(StackLogger.TRACE_DEBUG);
if(logger.isLoggingEnabled(StackLogger.TRACE_INFO)) {
logger.logInfo("the sip stack timer " + this.getClass().getName() + " has been stopped");
}
}

/* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#schedule(gov.nist.javax.sip.stack.SIPStackTimerTask, long)
*/
public boolean schedule(SIPStackTimerTask task, long delay) {
if(!started.get()) {
throw new IllegalStateException("The SIP Stack Timer has been stopped, no new tasks can be scheduled !");
}
HashedWheelSipTimerTask timerTask = new HashedWheelSipTimerTask(task, -1);
Timeout timeout = hashedWheelSipTimer.newTimeout(timerTask, delay, TimeUnit.MILLISECONDS);
timerTask.setTimeout(timeout);
return true;
}

/*
* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#scheduleWithFixedDelay(gov.nist.javax.sip.stack.SIPStackTimerTask, long, long)
*/
public boolean scheduleWithFixedDelay(SIPStackTimerTask task, long delay,
long period) {
if(!started.get()) {
throw new IllegalStateException("The SIP Stack Timer has been stopped, no new tasks can be scheduled !");
}
HashedWheelSipTimerTask timerTask = new HashedWheelSipTimerTask(task, period);
Timeout timeout = hashedWheelSipTimer.newTimeout(timerTask, delay, TimeUnit.MILLISECONDS);
timerTask.setTimeout(timeout);
return true;
}

/*
* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#start(gov.nist.javax.sip.SipStackImpl, java.util.Properties)
*/
public void start(SipStackImpl sipStack, Properties configurationProperties) {
sipStackImpl= sipStack;
// TODO have a param in the stack properties to set the number of thread for the timer executor
hashedWheelSipTimer.start();
started.set(true);
if(logger.isLoggingEnabled(StackLogger.TRACE_INFO)) {
logger.logInfo("the sip stack timer " + this.getClass().getName() + " has been started");
}
}
/*
* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#cancel(gov.nist.javax.sip.stack.SIPStackTimerTask)
*/
public boolean cancel(SIPStackTimerTask task) {
return ((HashedWheelSipTimerTask)task.getSipTimerTask()).cancel();
}

/*
* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#isStarted()
*/
public boolean isStarted() {
return started.get();
}

}
111 changes: 111 additions & 0 deletions src/main/java/org/mobicents/ext/javax/sip/timers/MockSipTimer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Conditions Of Use
*
* This software was developed by employees of the National Institute of
* Standards and Technology (NIST), an agency of the Federal Government.
* Pursuant to title 15 Untied States Code Section 105, works of NIST
* employees are not subject to copyright protection in the United States
* and are considered to be in the public domain. As a result, a formal
* license is not needed to use the software.
*
* This software is provided by NIST as a service and is expressly
* provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY. NIST does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement
*
* .
*
*/
package org.mobicents.ext.javax.sip.timers;

import gov.nist.core.CommonLogger;
import gov.nist.core.StackLogger;
import gov.nist.javax.sip.SipStackImpl;
import gov.nist.javax.sip.stack.SIPStackTimerTask;
import gov.nist.javax.sip.stack.timers.SipTimer;

import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* https://github.com/Mobicents/jain-sip.ext/issues/4
* Mock SIP Timer implementation that does nothing
* this one should use
* the property called gov.nist.javax.sip.LINGER_TIMER=0 to go along with it
*
* @author [email protected]
*
*/
public class MockSipTimer implements SipTimer {
private static StackLogger logger = CommonLogger.getLogger(MockSipTimer.class);

protected AtomicBoolean started = new AtomicBoolean(false);
protected SipStackImpl sipStackImpl;

/*
* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#schedule(gov.nist.javax.sip.stack.SIPStackTimerTask, long)
*/
public boolean schedule(SIPStackTimerTask task, long delay) {
return true;
}

/*
* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#scheduleWithFixedDelay(gov.nist.javax.sip.stack.SIPStackTimerTask, long, long)
*/
public boolean scheduleWithFixedDelay(SIPStackTimerTask task, long delay,
long period) {
return true;
}

/*
* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#cancel(gov.nist.javax.sip.stack.SIPStackTimerTask)
*/
public boolean cancel(SIPStackTimerTask task) {
return true;
}

/*
* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#start(gov.nist.javax.sip.SipStackImpl, java.util.Properties)
*/
public void start(SipStackImpl sipStack, Properties configurationProperties) {
sipStackImpl= sipStack;
// don't need the properties so nothing to see here
started.set(true);
if(logger.isLoggingEnabled(StackLogger.TRACE_INFO)) {
logger.logInfo("the sip stack timer " + this.getClass().getName() + " has been started");
}
}

/*
* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#stop()
*/
public void stop() {
started.set(false);
logger.logStackTrace(StackLogger.TRACE_DEBUG);
if(logger.isLoggingEnabled(StackLogger.TRACE_INFO)) {
logger.logInfo("the sip stack timer " + this.getClass().getName() + " has been stopped");
}
}

/*
* (non-Javadoc)
* @see gov.nist.javax.sip.stack.timers.SipTimer#isStarted()
*/
public boolean isStarted() {
return started.get();
}


}

0 comments on commit c8e921a

Please sign in to comment.