Revision: 8f2cb70e9834
Author:   oifa yulian <[email protected]>
Date:     Wed May  2 06:03:48 2012
Log:      scheduler taken from ms

http://code.google.com/p/jss7/source/detail?r=8f2cb70e9834

Added:
 /scheduler/pom.xml
 /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/Clock.java
/scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/ConcurrentLinkedList.java /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/DefaultClock.java /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/IntConcurrentHashMap.java /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/IntConcurrentLinkedList.java /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/OrderedTaskQueue.java /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/Scheduler.java
 /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/Task.java

=======================================
--- /dev/null
+++ /scheduler/pom.xml  Wed May  2 06:03:48 2012
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd";>
+       <modelVersion>4.0.0</modelVersion>
+       <packaging>jar</packaging>
+
+       <parent>
+               <artifactId>ss7-parent</artifactId>
+               <groupId>org.mobicents.protocols.ss7</groupId>
+               <version>1.0.0-SNAPSHOT</version>
+       </parent>
+
+       <groupId>org.mobicents.protocols.ss7.scheduler</groupId>
+       <artifactId>scheduler</artifactId>
+       <name>Mobicents SS7 :: Scheduler :: ${pom.artifactId}</name>
+
+       <dependencies>
+                <!-- non mobicents -->
+                <dependency>
+                        <groupId>log4j</groupId>
+                        <artifactId>log4j</artifactId>
+                        <scope>provided</scope>
+                </dependency>
+       </dependencies>
+
+       <!--  -->
+       <build>
+               <plugins>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-compiler-plugin</artifactId>
+                               <configuration>
+                                       <source>1.5</source>
+                                       <target>1.5</target>
+                               </configuration>
+                       </plugin>
+                       <plugin>
+                               <artifactId>maven-eclipse-plugin</artifactId>
+                               <groupId>org.mobicents.tools</groupId>
+                               <version>
+                                       
${mobicents.tools.mavenplugin.eclipse.version}
+                               </version>
+                               <inherited>false</inherited>
+                               <executions />
+                               <configuration>
+                                       <classpathExcludes>
+                                               
<exclude>xml-apis:xml-apis</exclude>
+                                               <exclude>jtidy:jtidy</exclude>
+                                       </classpathExcludes>
+                                       <resolveTransitiveDependencies>
+                                               true
+                                       </resolveTransitiveDependencies>
+                                       <eclipseProjectName>
+                                               mobicents-scheduler
+                                       </eclipseProjectName>
+                               </configuration>
+                       </plugin>
+               </plugins>
+               <finalName>mobicents-scheduler-${pom.version}</finalName>
+       </build>
+
+</project>
=======================================
--- /dev/null
+++ /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/Clock.java Wed May 2 06:03:48 2012
@@ -0,0 +1,55 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.mobicents.protocols.ss7.scheduler;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Media server internal clock.
+ *
+ * @author kulikov
+ */
+public interface Clock {
+
+    /**
+     * Gets the current time.
+     *
+     * @return current time expressed in nanoseconds.
+     */
+    public long getTime();
+
+    /**
+     * Gets the current time.
+     *
+     * @param timeUnit the time measurement units.
+     * @return the value in specified units.
+     */
+    public long getTime(TimeUnit timeUnit);
+
+    /**
+     * Gets the time measurement units.
+     *
+     * @return the time measurement units.
+     */
+    public TimeUnit getTimeUnit();
+}
=======================================
--- /dev/null
+++ /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/ConcurrentLinkedList.java Wed May 2 06:03:48 2012
@@ -0,0 +1,826 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.mobicents.protocols.ss7.scheduler;
+
+import java.util.Iterator;
+import java.util.Queue;
+import java.util.Collection;
+import java.lang.UnsupportedOperationException;
+import java.util.NoSuchElementException;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.BlockingQueue;
+/**
+ *
+ * @author oifa yulian
+ */
+public class ConcurrentLinkedList<E> implements BlockingQueue<E> {
+       //main list
+       private Node<E> head=new Node<E>(null);
+       private Node<E> tail=new Node<E>(null);
+
+       //cached garbage items
+       private Node<E> cacheHead=new Node<E>(null);
+       private Node<E> cacheTail=new Node<E>(null);
+
+       private Node<E> tempNode;
+       private Object searchNode;
+
+       private Integer size=0;
+       private Integer cacheSize=0;
+
+       //notifiers
+       private Notifier notifierHead=new Notifier();
+       private Notifier notifierTail=new Notifier();
+
+       //cached garbage items
+       private Notifier notifierCacheHead=new Notifier();
+       private Notifier notifierCacheTail=new Notifier();
+
+       private Integer notifierSize=0;
+       private Integer cacheNotifierSize=0;
+
+       private ListIterator iterator=new ListIterator();
+
+       private Lock lock=new Lock();
+
+       public ConcurrentLinkedList()
+       {
+               head.next=tail;
+               tail.previous=head;
+
+               cacheHead.next=cacheTail;
+               cacheTail.previous=cacheHead;
+
+               notifierHead.next=notifierTail;
+               notifierTail.previous=notifierHead;
+
+               notifierCacheHead.next=notifierCacheTail;
+               notifierCacheTail.previous=notifierCacheHead;
+       }
+
+       public boolean contains(Object o)
+       {
+               if(o==null)
+                       return false;
+
+               aquireAccess();
+
+               tempNode=head.next;
+               while(tempNode.element!=null && !o.equals(tempNode.element))
+                       tempNode=tempNode.next;
+
+               if(tempNode.element==null)
+               {
+                       releaseAccess();
+                       return true;
+               }
+
+               releaseAccess();
+               return false;
+       }
+
+       public boolean containsAll(Collection<?> c)
+       {
+               aquireAccess();
+
+               Iterator<? extends Object> e = c.iterator();
+               while (e.hasNext())
+               {
+                       searchNode=e.next();
+                       if(searchNode==null)
+                       {
+                               releaseAccess();
+                               return false;
+                       }
+
+                       tempNode=head.next;
+                       while(tempNode.element!=null && 
!searchNode.equals(tempNode.element))
+                               tempNode=tempNode.next;
+
+                       if(tempNode.element==null)
+                       {
+                               releaseAccess();
+                               return false;
+                       }
+               }
+
+               releaseAccess();
+               return true;
+       }
+
+       public boolean add(E value)
+       {
+               if(value==null)
+                       return false;
+
+               aquireAccess();
+
+               tempNode=head.next;
+               while(tempNode.element!=null && !value.equals(tempNode.element))
+                       tempNode=tempNode.next;
+
+               if(tempNode.element!=null)
+               {
+                       releaseAccess();
+                       return false;
+               }
+
+               if(cacheSize==0)
+                       //need new node
+                       tempNode=new Node(value);
+               else
+               {
+                       //obtain node from cache decrease cache size
+                       tempNode=cacheHead.next;
+                       cacheHead.next=tempNode.next;
+                       cacheHead.next.previous=cacheHead;
+
+                       tempNode.element=value;
+                       cacheSize--;
+               }
+
+               //connect to main queue
+               tail.previous.next=tempNode;
+               tempNode.previous=tail.previous;
+
+               tempNode.next=tail;
+               tail.previous=tempNode;
+               size++;
+
+               if(notifierSize>0)
+                       updateNotifier(value);
+
+               releaseAccess();
+               return true;
+       }
+
+       public boolean addAll(Collection<? extends E> c)
+    {
+               Boolean result=false;
+               for(E item:c)
+                       result|=add(item);
+
+               return result;
+    }
+
+       public void put(E o)
+       {
+               offer(o);
+       }
+
+       public boolean offer(E o, long timeout, TimeUnit unit)
+       {
+               return offer(o);
+       }
+
+       public boolean offer(E value)
+       {
+               if(value==null)
+                       return false;
+
+               aquireAccess();
+
+               if(cacheSize==0)
+                       //need new node
+                       tempNode=new Node(value);
+               else
+               {
+                       //obtain node from cache decrease cache size
+                       tempNode=cacheHead.next;
+                       cacheHead.next=tempNode.next;
+                       cacheHead.next.previous=cacheHead;
+
+                       tempNode.element=value;
+                       cacheSize--;
+               }
+
+               //connect to main queue
+               tail.previous.next=tempNode;
+               tempNode.previous=tail.previous;
+
+               tempNode.next=tail;
+               tail.previous=tempNode;
+               size++;
+
+               if(notifierSize>0)
+                       updateNotifier(value);
+
+               releaseAccess();
+               return true;
+       }
+
+       public E peek()
+       {
+               aquireAccess();
+
+               E currValue=head.next.element;
+
+               releaseAccess();
+               return currValue;
+       }
+
+       public E element() throws NoSuchElementException
+       {
+               aquireAccess();
+
+               if(size==0)
+               {
+                       releaseAccess();
+                       throw new NoSuchElementException();
+               }
+
+               E currValue=head.next.element;
+
+               releaseAccess();
+               return currValue;
+       }
+
+       public <T> T[] toArray(T[] a) {
+               if (a.length < size)
+ a = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
+
+               aquireAccess();
+
+               tempNode=head.next;
+               int i=0;
+               Object[] result = a;
+
+               while(tempNode.element!=null && i<a.length)
+               {
+                       result[i++]=tempNode.element;
+                       tempNode=tempNode.next;
+               }
+
+               releaseAccess();
+
+               while (i<a.length)
+                       result[i] = null;
+
+               return a;
+       }
+
+       public Object[] toArray()
+       {
+               aquireAccess();
+
+               Object[] result=new Object[size];
+               tempNode=head.next;
+               int i=0;
+
+               while(i<size)
+               {
+                       result[i++]=tempNode.element;
+                       tempNode=tempNode.next;
+               }
+
+               releaseAccess();
+               return result;
+       }
+
+       public E poll()
+       {
+               aquireAccess();
+
+               if(size==0)
+               {
+                       releaseAccess();
+                       return null;
+               }
+
+               //disconnect from main list
+               tempNode=head.next;
+               head.next=tempNode.next;
+               head.next.previous=head;
+
+               //connect to cache list
+               tempNode.next=cacheHead.next;
+               cacheHead.next=tempNode;
+
+               tempNode.previous=cacheHead;
+               tempNode.next.previous=tempNode;
+
+               //update counters
+               E currValue=tempNode.element;
+               cacheSize++;
+               size--;
+
+               releaseAccess();
+               return currValue;
+       }
+
+       public E take()
+       {
+               aquireAccess();
+
+               if(size==0)
+               {
+                       Notifier<E> tempNotifier;
+                       if(cacheNotifierSize==0)
+                               //need new node
+                               tempNotifier=new Notifier();
+                       else
+                       {
+                               //obtain node from cache decrease cache size
+                               tempNotifier=notifierCacheHead.next;
+                               notifierCacheHead.next=tempNotifier.next;
+                               
notifierCacheHead.next.previous=notifierCacheHead;
+
+                               cacheNotifierSize--;
+                       }
+
+                       //store it in notifiers list
+                       //connect to main queue
+                       notifierTail.previous.next=tempNotifier;
+                       tempNotifier.previous=notifierTail.previous;
+
+                       tempNotifier.next=notifierTail;
+                       notifierTail.previous=tempNotifier;
+                       notifierSize++;
+
+                       while(size==0)
+                       {
+                               //release lock so others will be able to add 
nodes and wait
+                               releaseAccess();
+
+                               synchronized(tempNotifier.lock)
+                       {
+                                       try
+                                       {
+                                               tempNotifier.lock.wait();
+                                       }
+                                       catch(InterruptedException e)
+                                       {
+
+                                       }
+                       }
+
+                               aquireAccess();
+
+                               if(size!=0)
+                               {
+                                       //take back notifier
+                                       
tempNotifier.previous.next=tempNotifier.next;
+                                       
tempNotifier.next.previous=tempNotifier.previous;
+
+                                       
tempNotifier.next=notifierCacheHead.next;
+                                       
notifierCacheHead.next.previous=tempNotifier;
+                                       notifierCacheHead.next=tempNotifier;
+                                       tempNotifier.previous=notifierCacheHead;
+                                       cacheNotifierSize++;
+                               }
+                       }
+               }
+
+               //disconnect from main list
+               tempNode=head.next;
+               head.next=tempNode.next;
+               head.next.previous=head;
+
+               //connect to cache list
+               tempNode.next=cacheHead.next;
+               cacheHead.next=tempNode;
+
+               tempNode.previous=cacheHead;
+               tempNode.next.previous=tempNode;
+
+               //update counters
+               E currValue=tempNode.element;
+               cacheSize++;
+               size--;
+
+               releaseAccess();
+               return currValue;
+       }
+
+       public E poll(long timeout,TimeUnit unit) throws InterruptedException
+       {
+               aquireAccess();
+
+               if(size==0)
+               {
+                       Notifier<E> tempNotifier;
+                       if(cacheNotifierSize==0)
+                               //need new node
+                               tempNotifier=new Notifier();
+                       else
+                       {
+                               //obtain node from cache decrease cache size
+                               tempNotifier=notifierCacheHead.next;
+                               notifierCacheHead.next=tempNotifier.next;
+                               
notifierCacheHead.next.previous=notifierCacheHead;
+
+                               cacheNotifierSize--;
+                       }
+
+                       //store it in notifiers list
+                       //connect to main queue
+                       notifierTail.previous.next=tempNotifier;
+                       tempNotifier.previous=notifierTail.previous;
+
+                       tempNotifier.next=notifierTail;
+                       notifierTail.previous=tempNotifier;
+                       notifierSize++;
+
+                       while(size==0)
+                       {
+                               //release lock so others will be able to add 
nodes and wait
+                               releaseAccess();
+
+                               synchronized(tempNotifier.lock)
+                       {
+                                       try {
+                                               switch(unit)
+                                               {
+                                                       case MICROSECONDS:
+ tempNotifier.lock.wait(timeout/1000L,(int)((timeout*1000L)%1000000L));
+                                                               break;
+                                                       case MILLISECONDS:
+                                                               
tempNotifier.lock.wait(timeout);
+                                                               break;
+                                                       case NANOSECONDS:
+                                                               
tempNotifier.lock.wait(timeout/1000000L,(int)(timeout%1000000L));
+                                                               break;
+                                                       case SECONDS:
+                                                               
tempNotifier.lock.wait(timeout*1000L);
+                                                               break;
+                                               }
+                                       }
+                                       catch(InterruptedException e)
+                                       {
+
+                                       }
+                       }
+
+                               aquireAccess();
+
+                               if(size==0)
+                               {
+                                       releaseAccess();
+                                       throw new InterruptedException();
+                               }
+                       }
+
+                       //take back notifier
+                       tempNotifier.previous.next=tempNotifier.next;
+                       tempNotifier.next.previous=tempNotifier.previous;
+
+                       tempNotifier.next=notifierCacheHead.next;
+                       notifierCacheHead.next.previous=tempNotifier;
+                       notifierCacheHead.next=tempNotifier;
+                       tempNotifier.previous=notifierCacheHead;
+                       cacheNotifierSize++;
+               }
+
+               //disconnect from main list
+               tempNode=head.next;
+               head.next=tempNode.next;
+               head.next.previous=head;
+
+               //connect to cache list
+               tempNode.next=cacheHead.next;
+               cacheHead.next=tempNode;
+
+               tempNode.previous=cacheHead;
+               tempNode.next.previous=tempNode;
+
+               //update counters
+               E currValue=tempNode.element;
+               cacheSize++;
+               size--;
+
+               releaseAccess();
+               return currValue;
+       }
+
+       public E remove() throws NoSuchElementException
+       {
+               aquireAccess();
+
+               if(size==0)
+               {
+                       releaseAccess();
+                       throw new NoSuchElementException();
+               }
+
+               //disconnect from main list
+               tempNode=head.next;
+               head.next=tempNode.next;
+               head.next.previous=head;
+
+               //connect to cache list
+               tempNode.next=cacheHead.next;
+               cacheHead.next=tempNode;
+
+               tempNode.previous=cacheHead;
+               tempNode.next.previous=tempNode;
+
+               //update counters
+               E currValue=tempNode.element;
+               cacheSize++;
+               size--;
+
+               releaseAccess();
+               return currValue;
+       }
+
+       public boolean remove(Object o)
+       {
+               aquireAccess();
+
+               tempNode=head.next;
+               while(tempNode.element!=null && !o.equals(tempNode.element))
+                       tempNode=tempNode.next;
+
+               if(tempNode.element==null)
+               {
+                       releaseAccess();
+                       return false;
+               }
+
+               tempNode.previous.next=tempNode.next;
+               tempNode.next.previous=tempNode.previous;
+               size--;
+
+               //return to cache
+               tempNode.next=cacheHead.next;
+               cacheHead.next=tempNode;
+
+               tempNode.previous=cacheHead;
+               tempNode.next.previous=tempNode;
+
+               releaseAccess();
+               return true;
+       }
+
+       public void clear()
+       {
+               aquireAccess();
+
+               if(size>0)
+               {
+                       cacheSize+=size;
+                       size=0;
+
+                       tail.previous.next=cacheHead.next;
+                       cacheHead.next.previous=tail.previous;
+
+                       cacheHead.next=head.next;
+                       head.next.previous=cacheHead;
+
+                       head.next=tail;
+                       tail.previous=head;
+               }
+
+               releaseAccess();
+       }
+
+       public int drainTo(Collection<? super E> c)
+       {
+               if(size==0)
+                       return 0;
+
+               if(c==null)
+                       throw new NullPointerException();
+
+               if(c.equals(this))
+                       throw new IllegalArgumentException();
+
+               aquireAccess();
+
+               Node<E> tempFirst=head.next;
+               Node<E> tempLast=tail.previous;
+               int tempCount=size;
+
+               head.next=tail;
+               tail.previous=head;
+               size=0;
+
+               releaseAccess();
+
+               Node<E> tempNode=tempFirst;
+               for(int i=tempCount;i>0;i--,tempNode=tempNode.next)
+                       c.add(tempNode.element);
+
+               aquireAccess();
+
+               if(tempCount>0)
+               {
+                       cacheSize+=tempCount;
+
+                       tempLast.next=cacheHead.next;
+                       cacheHead.next.previous=tempLast;
+
+                       cacheHead.next=tempFirst;
+                       tempFirst.previous=cacheHead;
+               }
+
+               releaseAccess();
+               return tempCount;
+       }
+
+       public int drainTo(Collection<? super E> c,int maxElements)
+       {
+               if(size==0)
+                       return 0;
+
+               if(c==null)
+                       throw new NullPointerException();
+
+               if(c.equals(this))
+                       throw new IllegalArgumentException();
+
+               aquireAccess();
+
+               tempNode=head.next;
+               int count=0;
+               while(tempNode.element!=null && count<maxElements)
+               {
+                       c.add(tempNode.element);
+                       count++;
+                       tempNode=tempNode.next;
+               }
+
+               size-=count;
+               if(count>0)
+               {
+                       cacheSize+=count;
+
+                       Node<E> firstNode=head.next;
+                       head.next=tempNode.next;
+                       head.next.previous=head;
+
+                       tempNode.next=cacheHead.next;
+                       cacheHead.next.previous=tempNode;
+
+                       cacheHead.next=firstNode;
+                       firstNode.previous=cacheHead;
+               }
+
+               releaseAccess();
+               return count;
+       }
+
+       public boolean isEmpty()
+       {
+               return size==0;
+       }
+
+       public int size()
+       {
+               return size;
+       }
+
+       public Iterator<E> iterator()
+       {
+               iterator.currNode=head;
+               return iterator;
+       }
+
+       public int remainingCapacity()
+       {
+               return Integer.MAX_VALUE;
+       }
+
+       public boolean retainAll(Collection<?> c)
+    {
+       throw new UnsupportedOperationException();
+    }
+
+    public boolean removeAll(Collection<?> c)
+    {
+       throw new UnsupportedOperationException();
+    }
+
+
+    private void updateNotifier(E newElement)
+    {
+       if(notifierSize==0)
+               return;
+
+       notifierSize--;
+       Notifier tempNotifier=notifierHead.next;
+       synchronized(tempNotifier.lock)
+       {
+               tempNotifier.lock.notify();
+       }
+    }
+
+    private void aquireAccess()
+    {
+       try
+       {
+               lock.lock();
+       }
+       catch(InterruptedException e)
+       {
+
+       }
+    }
+
+    private void releaseAccess()
+    {
+       lock.unlock();
+    }
+
+    private class Notifier<E> {
+       private Object lock=new Object();
+       Notifier next;
+       Notifier previous;
+    }
+
+    private class Node<E> {
+       E element;
+       Node<E> next;
+       Node<E> previous;
+
+       Node(E element)
+       {
+               this.element=element;
+       }
+    }
+
+    private class ListIterator implements Iterator<E>
+    {
+       private Node<E> currNode;
+
+       public ListIterator()
+       {
+               this.currNode=head;
+       }
+
+       public boolean hasNext()
+       {
+               return currNode.next.element!=null;
+       }
+
+       public E next()
+       {
+               aquireAccess();
+
+               E  result=currNode.next.element;
+               currNode=currNode.next;
+
+               releaseAccess();
+               return result;
+       }
+
+       public void remove()
+       {
+               aquireAccess();
+
+               tempNode=currNode;
+               currNode=currNode.next;
+
+               tempNode.previous.next=tempNode.next;
+               tempNode.next.previous=tempNode.previous;
+               size--;
+
+               //return to cache
+               tempNode.next=cacheHead.next;
+               cacheHead.next=tempNode;
+
+               tempNode.previous=cacheHead;
+               tempNode.next.previous=tempNode;
+
+               releaseAccess();
+       }
+    }
+
+    private class Lock {
+        protected boolean locked;
+        public Lock() {
+            locked=false;
+        }
+
+        public synchronized void lock() throws InterruptedException {
+            while (locked) wait();
+            locked=true;
+        }
+
+        public synchronized void unlock() {
+            locked=false;
+            notify();
+        }
+    }
+}
=======================================
--- /dev/null
+++ /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/DefaultClock.java Wed May 2 06:03:48 2012
@@ -0,0 +1,66 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.mobicents.protocols.ss7.scheduler;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * Clock implementation wich uses the default OS clock.
+ *
+ * @author kulikov
+ */
+public class DefaultClock implements Clock {
+
+    /**
+     * The default time unit: nanoseconds.
+     */
+    private TimeUnit timeUnit = TimeUnit.NANOSECONDS;
+
+    /**
+     * (Non Java-doc.)
+     *
+     * @see org.mobicents.media.server.scheduler.Clock.getTime().
+     */
+    public long getTime() {
+        return System.nanoTime();
+    }
+
+    /**
+     * (Non Java-doc.)
+     *
+     * @see org.mobicents.media.server.scheduler.Clock.getTimeUnit().
+     */
+    public TimeUnit getTimeUnit() {
+        return timeUnit;
+    }
+
+    /**
+     * (Non Java-doc.)
+     *
+     * @see org.mobicents.media.server.scheduler.Clock.getTime().
+     */
+    public long getTime(TimeUnit timeUnit) {
+        return timeUnit.convert(System.nanoTime(), this.timeUnit);
+    }
+}
=======================================
--- /dev/null
+++ /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/IntConcurrentHashMap.java Wed May 2 06:03:48 2012
@@ -0,0 +1,86 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.mobicents.protocols.ss7.scheduler;
+
+import java.util.Iterator;
+import java.util.Queue;
+import java.util.Collection;
+import java.lang.UnsupportedOperationException;
+import java.util.NoSuchElementException;
+
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.BlockingQueue;
+/**
+ *
+ * @author oifa yulian
+ */
+public class IntConcurrentHashMap<E> {
+ private IntConcurrentLinkedList<E>[] lists=new IntConcurrentLinkedList[16];
+
+       public IntConcurrentHashMap()
+       {
+               for(int i=0;i<16;i++)
+                       lists[i]=new IntConcurrentLinkedList<E>();
+       }
+
+       public boolean contains(int key)
+       {
+               return lists[getHash(key)].contains(key);
+       }
+
+       public boolean add(E value,int key)
+       {
+               if(value==null)
+                       return false;
+
+               lists[getHash(key)].add(value,key);
+               return true;
+       }
+
+       public boolean offer(E value,int key)
+       {
+               if(value==null)
+                       return false;
+
+               lists[getHash(key)].offer(value,key);
+               return true;
+       }
+
+       public E get(int key)
+       {
+               return lists[getHash(key)].get(key);
+       }
+
+       public E remove(int key)
+       {
+               return lists[getHash(key)].remove(key);
+       }
+
+       private int getHash(int key)
+       {
+               //4 bits allows 16 values
+               //use bits 1,2 and 6,7
+               return key&0x3 + (key>>3)&0xC;
+       }
+}
=======================================
--- /dev/null
+++ /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/IntConcurrentLinkedList.java Wed May 2 06:03:48 2012
@@ -0,0 +1,335 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.mobicents.protocols.ss7.scheduler;
+
+import java.util.Iterator;
+import java.util.Queue;
+import java.util.Collection;
+import java.lang.UnsupportedOperationException;
+import java.util.NoSuchElementException;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.BlockingQueue;
+/**
+ *
+ * @author oifa yulian
+ */
+public class IntConcurrentLinkedList<E> {
+       //main list
+       private Node<E> head=new Node<E>(-1,null);
+       private Node<E> tail=new Node<E>(-1,null);
+
+       //cached garbage items
+       private Node<E> cacheHead=new Node<E>(-1,null);
+       private Node<E> cacheTail=new Node<E>(-1,null);
+
+       private Node<E> tempNode;
+       private Object searchNode;
+
+       private Integer size=0;
+       private Integer cacheSize=0;
+
+       private ListIterator iterator=new ListIterator();
+
+       private Lock lock=new Lock();
+
+       public IntConcurrentLinkedList()
+       {
+               head.next=tail;
+               tail.previous=head;
+
+               cacheHead.next=cacheTail;
+               cacheTail.previous=cacheHead;
+       }
+
+       public boolean contains(int key)
+       {
+               aquireAccess();
+
+               tempNode=head.next;
+               while(tempNode.element!=null && key!=tempNode.key)
+                       tempNode=tempNode.next;
+
+               if(tempNode.element==null)
+               {
+                       releaseAccess();
+                       return true;
+               }
+
+               releaseAccess();
+               return false;
+       }
+
+       public boolean add(E value,int key)
+       {
+               if(value==null)
+                       return false;
+
+               aquireAccess();
+
+               tempNode=head.next;
+               while(tempNode.element!=null && key!=tempNode.key)
+                       tempNode=tempNode.next;
+
+               if(tempNode.element!=null)
+               {
+                       releaseAccess();
+                       return false;
+               }
+
+               if(cacheSize==0)
+                       //need new node
+                       tempNode=new Node(key,value);
+               else
+               {
+                       //obtain node from cache decrease cache size
+                       tempNode=cacheHead.next;
+                       cacheHead.next=tempNode.next;
+                       cacheHead.next.previous=cacheHead;
+
+                       tempNode.key=key;
+                       tempNode.element=value;
+                       cacheSize--;
+               }
+
+               //connect to main queue
+               tail.previous.next=tempNode;
+               tempNode.previous=tail.previous;
+
+               tempNode.next=tail;
+               tail.previous=tempNode;
+               size++;
+
+               releaseAccess();
+               return true;
+       }
+
+       public boolean offer(E value,int key)
+       {
+               if(value==null)
+                       return false;
+
+               aquireAccess();
+
+               if(cacheSize==0)
+                       //need new node
+                       tempNode=new Node(key,value);
+               else
+               {
+                       //obtain node from cache decrease cache size
+                       tempNode=cacheHead.next;
+                       cacheHead.next=tempNode.next;
+                       cacheHead.next.previous=cacheHead;
+
+                       tempNode.key=key;
+                       tempNode.element=value;
+                       cacheSize--;
+               }
+
+               //connect to main queue
+               tail.previous.next=tempNode;
+               tempNode.previous=tail.previous;
+
+               tempNode.next=tail;
+               tail.previous=tempNode;
+               size++;
+
+               releaseAccess();
+               return true;
+       }
+
+       public E get(int key)
+       {
+               aquireAccess();
+
+               tempNode=head.next;
+               while(tempNode.element!=null && key!=tempNode.key)
+                       tempNode=tempNode.next;
+
+               E result=tempNode.element;
+               releaseAccess();
+               return result;
+       }
+
+       public E remove(int key)
+       {
+               aquireAccess();
+
+               tempNode=head.next;
+               while(tempNode.element!=null && key!=tempNode.key)
+                       tempNode=tempNode.next;
+
+               if(tempNode.element==null)
+               {
+                       releaseAccess();
+                       return null;
+               }
+
+               tempNode.previous.next=tempNode.next;
+               tempNode.next.previous=tempNode.previous;
+               size--;
+
+               //return to cache
+               tempNode.next=cacheHead.next;
+               cacheHead.next=tempNode;
+
+               tempNode.previous=cacheHead;
+               tempNode.next.previous=tempNode;
+
+               E result=tempNode.element;
+               releaseAccess();
+               return result;
+       }
+
+       public void clear()
+       {
+               aquireAccess();
+
+               if(size>0)
+               {
+                       cacheSize+=size;
+                       size=0;
+
+                       tail.previous.next=cacheHead.next;
+                       cacheHead.next.previous=tail.previous;
+
+                       cacheHead.next=head.next;
+                       head.next.previous=cacheHead;
+
+                       head.next=tail;
+                       tail.previous=head;
+               }
+
+               releaseAccess();
+       }
+
+       public boolean isEmpty()
+       {
+               return size==0;
+       }
+
+       public int size()
+       {
+               return size;
+       }
+
+       public Iterator<E> iterator()
+       {
+               iterator.currNode=head;
+               return iterator;
+       }
+
+       private void aquireAccess()
+    {
+       try
+       {
+               lock.lock();
+       }
+       catch(InterruptedException e)
+       {
+
+       }
+    }
+
+    private void releaseAccess()
+    {
+       lock.unlock();
+    }
+
+       private class Node<E> {
+               int key;
+       E element;
+       Node<E> next;
+       Node<E> previous;
+
+       Node(int key,E element)
+       {
+               this.key=key;
+               this.element=element;
+       }
+    }
+
+    private class ListIterator implements Iterator<E>
+    {
+       private Node<E> currNode;
+
+       public ListIterator()
+       {
+               this.currNode=head;
+       }
+
+       public boolean hasNext()
+       {
+               return currNode.next.element!=null;
+       }
+
+       public E next()
+       {
+               aquireAccess();
+
+               E  result=currNode.next.element;
+               currNode=currNode.next;
+
+               releaseAccess();
+               return result;
+       }
+
+       public void remove()
+       {
+               aquireAccess();
+
+               tempNode=currNode;
+               currNode=currNode.next;
+
+               tempNode.previous.next=tempNode.next;
+               tempNode.next.previous=tempNode.previous;
+               size--;
+
+               //return to cache
+               tempNode.next=cacheHead.next;
+               cacheHead.next=tempNode;
+
+               tempNode.previous=cacheHead;
+               tempNode.next.previous=tempNode;
+
+               releaseAccess();
+       }
+    }
+
+    private class Lock {
+        protected boolean locked;
+        public Lock() {
+            locked=false;
+        }
+
+        public synchronized void lock() throws InterruptedException {
+            while (locked) wait();
+            locked=true;
+        }
+
+        public synchronized void unlock() {
+            locked=false;
+            notify();
+        }
+    }
+}
=======================================
--- /dev/null
+++ /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/OrderedTaskQueue.java Wed May 2 06:03:48 2012
@@ -0,0 +1,149 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.mobicents.protocols.ss7.scheduler;
+
+import java.util.Collection;
+
+/**
+ * Implements queue of tasks.
+ *
+ *
+ * @author kulikov
+ */
+public class OrderedTaskQueue {
+       //inner holder for tasks
+ private ConcurrentLinkedList<Task>[] taskList=new ConcurrentLinkedList[2];
+
+    private Integer activeIndex=0;
+
+    public OrderedTaskQueue() {
+        //intitalize task list
+       taskList[0] = new ConcurrentLinkedList<Task>();
+       taskList[1] = new ConcurrentLinkedList<Task>();
+    }
+
+    /**
+     * Shows if this queue is empty.
+     *
+     * @return true if queue is currently empty
+     */
+    public boolean isEmpty() {
+               return taskList[activeIndex].isEmpty();
+    }
+
+    /**
+     * Queues specified task using tasks dead line time.
+     *
+     * @param task the task to be queued.
+     * @return TaskExecutor for the scheduled task.
+     */
+    public void accept(Task task) {
+       if((activeIndex+1)%2==0)
+       {
+               if(!task.isInQueue0())
+               {
+                       taskList[0].offer(task);
+                       task.storedInQueue0();
+               }
+       }
+       else
+       {
+               if(!task.isInQueue1())
+               {
+                       taskList[1].offer(task);
+                       task.storedInQueue1();
+               }
+       }
+    }
+
+    /**
+ * Retrieves the task with earliest dead line and removes it from queue.
+     *
+     * @return task which has earliest dead line
+     */
+    public Task poll() {
+       Task result=taskList[activeIndex].poll();
+       if(result!=null)
+       {
+               if(activeIndex==0)
+                       result.removeFromQueue0();
+               else
+                       result.removeFromQueue1();
+       }
+
+       return result;
+    }
+
+    public void changePool()
+    {
+       activeIndex=(activeIndex+1)%2;
+    }
+
+    /**
+     * Clean the queue.
+     */
+    public void clear() {
+       taskList[0].clear();
+       taskList[1].clear();
+    }
+
+    /**
+     * Gets the size of this queue.
+     *
+     * @return the size of the queue.
+     */
+    public int size() {
+       return taskList[activeIndex].size();
+    }
+
+    protected void remove(Task task) {
+       if(task.isInQueue0())
+       {
+               taskList[0].remove(task);
+               task.removeFromQueue0();
+       }
+
+       if(task.isInQueue1())
+       {
+               taskList[1].remove(task);
+               task.removeFromQueue1();
+       }
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        sb.append("Queue[");
+
+        int len = Math.min(30, taskList[activeIndex].size());
+        for (int i = 0; i < len -1; i++) {
+               //sb.append(taskList[activeIndex].get(i).getPriority());
+            sb.append(",");
+        }
+
+        //if(!taskList[activeIndex].isEmpty())
+        //     sb.append(taskList[activeIndex].get(len - 1).getPriority());
+        sb.append("]");
+        return sb.toString();
+    }
+}
=======================================
--- /dev/null
+++ /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/Scheduler.java Wed May 2 06:03:48 2012
@@ -0,0 +1,306 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.mobicents.protocols.ss7.scheduler;
+
+import java.lang.InterruptedException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.CompletionService;
+import java.util.concurrent.ExecutorCompletionService;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.LockSupport;
+
+import org.apache.log4j.Logger;
+
+/**
+ * Implements scheduler with multi-level priority queue.
+ *
+ * This scheduler implementation follows to uniprocessor model with "super" thread. + * The "super" thread includes IO bound thread and one or more CPU bound threads
+ * with equal priorities.
+ *
+ * The actual priority is assigned to task instead of process and can be
+ * changed dynamically at runtime using the initial priority level, feedback
+ * and other parameters.
+ *
+ *
+ * @author kulikov
+ */
+public class Scheduler  {
+       //MANAGEMENT QUEUE SHOULD CONTAIN ONLY TASKS THAT ARE NOT TIME DEPENDENT
+       public static final Integer MANAGEMENT_QUEUE=0;
+       public static final Integer L2READ_QUEUE=1;
+       public static final Integer L3READ_QUEUE=2;
+       public static final Integer INTERNETWORKING_QUEUE=3;
+       public static final Integer L3WRITE_QUEUE=4;
+       public static final Integer L2WRITE_QUEUE=5;
+
+       public static final Integer HEARTBEAT_QUEUE=-1;
+
+    //The clock for time measurement
+    private Clock clock;
+
+    //priority queue
+    protected OrderedTaskQueue[] taskQueues = new OrderedTaskQueue[6];
+
+    protected OrderedTaskQueue heartBeatQueue;
+    //CPU bound threads
+    private CpuThread cpuThread;
+
+    //flag indicating state of the scheduler
+    private boolean isActive;
+
+    private Logger logger = Logger.getLogger(Scheduler.class) ;
+
+    /**
+     * Creates new instance of scheduler.
+     */
+    public Scheduler() {
+       for(int i=0;i<taskQueues.length;i++)
+               taskQueues[i]=new OrderedTaskQueue();
+
+       heartBeatQueue=new OrderedTaskQueue();
+
+        cpuThread = new CpuThread(String.format("Scheduler"));
+    }
+
+    /**
+     * Sets clock.
+     *
+     * @param clock the clock used for time measurement.
+     */
+    public void setClock(Clock clock) {
+        this.clock = clock;
+    }
+
+    /**
+     * Gets the clock used by this scheduler.
+     *
+     * @return the clock object.
+     */
+    public Clock getClock() {
+        return clock;
+    }
+
+    /**
+     * Queues task for execution according to its priority.
+     *
+     * @param task the task to be executed.
+     */
+    public void submit(Task task,Integer index) {
+        task.activate(false);
+        taskQueues[index].accept(task);
+    }
+
+    /**
+     * Queues task for execution according to its priority.
+     *
+     * @param task the task to be executed.
+     */
+    public void submitHeatbeat(Task task) {
+        task.activate(true);
+        heartBeatQueue.accept(task);
+    }
+
+    /**
+     * Starts scheduler.
+     */
+    public void start() {
+       if(this.isActive)
+               return;
+
+        if (clock == null) {
+            throw new IllegalStateException("Clock is not set");
+        }
+
+        this.isActive = true;
+
+        logger.info("Starting ");
+
+        cpuThread.activate();
+
+        logger.info("Started ");
+    }
+
+    /**
+     * Stops scheduler.
+     */
+    public void stop() {
+        if (!this.isActive) {
+            return;
+        }
+
+        cpuThread.shutdown();
+
+        try
+        {
+               Thread.sleep(40);
+        }
+        catch(InterruptedException e)
+               {
+               }
+
+        for(int i=0;i<taskQueues.length;i++)
+               taskQueues[i].clear();
+
+        heartBeatQueue.clear();
+    }
+
+    //removed statistics to increase perfomance
+    /**
+     * Shows the miss rate.
+     *
+     * @return the miss rate value;
+     */
+    public double getMissRate() {
+        return 0;
+    }
+
+    public long getWorstExecutionTime() {
+        return 0;
+    }
+
+    public void notifyCompletion()
+    {
+       cpuThread.notifyCompletion();
+    }
+
+    /**
+     * Executor thread.
+     */
+    private class CpuThread extends Thread {
+        private volatile boolean active;
+        private int currQueue=MANAGEMENT_QUEUE;
+        private AtomicInteger activeTasksCount=new AtomicInteger();
+        private long cycleStart=0;
+        private int runIndex=0;
+        private ExecutorService eservice;
+        private Object LOCK=new Object();
+
+        public CpuThread(String name) {
+            super(name);
+            int size=Runtime.getRuntime().availableProcessors()*2;
+ eservice =new ThreadPoolExecutor(size, size,0L, TimeUnit.MILLISECONDS,new ConcurrentLinkedList<Runnable>());
+        }
+
+        public void activate() {
+               this.active = true;
+               this.start();
+        }
+
+        public void notifyCompletion() {
+               int newValue=activeTasksCount.decrementAndGet();
+               if(newValue==0 && this.active)
+                       synchronized(LOCK) {
+                               LOCK.notify();
+                       }
+        }
+
+        @Override
+        public void run() {
+               long cycleDuration,cycleDuration2;
+               cycleStart = System.nanoTime();
+
+               while(active)
+               {
+                       while(currQueue<=L2WRITE_QUEUE)
+                       {
+                               synchronized(LOCK) {
+                                       if(executeQueue(taskQueues[currQueue]))
+                                               try {
+                                                       LOCK.wait();
+                                               }
+                                               catch(InterruptedException e)  {
+                                                       //lets continue
+                                               }
+                               }
+
+                               currQueue++;
+                       }
+
+                       runIndex=(runIndex+1)%25;
+                       if(runIndex==0)
+                       {
+                               synchronized(LOCK) {
+                                       if(executeQueue(heartBeatQueue))
+                                               try  {
+                                                       LOCK.wait();
+                                               }
+                                                       
catch(InterruptedException e)  {
+                                                       //lets continue
+                                                       }
+                               }
+                       }
+
+                       //sleep till next cycle
+                       cycleDuration=System.nanoTime() - cycleStart;
+                       if(cycleDuration<4000000L)
+                       {
+                               try  {
+ sleep(4L-cycleDuration/1000000L,(int)((4000000L-cycleDuration)%1000000L));
+                               }
+                               catch(InterruptedException e)  {
+                                       //lets continue
+                               }
+                       }
+
+                       //new cycle starts , updating cycle start time by 4ms
+                       //cycleDuration2=System.nanoTime() - cycleStart;
+                       cycleStart = cycleStart + 4000000L;
+                currQueue=MANAGEMENT_QUEUE;
+
+                //if(cycleDuration2>4100000L)
+ // System.out.println("TIME LONGER THEN 4.1MS,DURATION:" + cycleDuration);
+                //else if(cycleDuration2<3900000L)
+ // System.out.println("TIME SHORTER THEN 3.9MS,DURATION:" + cycleDuration);
+               }
+        }
+
+        private boolean executeQueue(OrderedTaskQueue currQueue)
+        {
+               Task t;
+               currQueue.changePool();
+            int currQueueSize=currQueue.size();
+            activeTasksCount.set(currQueueSize);
+            t = currQueue.poll();
+            //submit all tasks in current queue
+            while(t!=null)
+            {
+               eservice.execute(t);
+               t = currQueue.poll();
+            }
+
+            return currQueueSize!=0;
+        }
+
+        /**
+         * Terminates thread.
+         */
+        private void shutdown() {
+            this.active = false;
+        }
+    }
+}
=======================================
--- /dev/null
+++ /scheduler/src/main/java/org/mobicents/protocols/ss7/scheduler/Task.java Wed May 2 06:03:48 2012
@@ -0,0 +1,139 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.mobicents.protocols.ss7.scheduler;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.atomic.AtomicBoolean;
+/**
+ * Scheduling task.
+ *
+ * @author kulikov
+ */
+public abstract class Task implements Runnable {
+    protected Scheduler scheduler;
+
+    private volatile boolean isActive = true;
+    private volatile boolean isHeartbeat = true;
+
+    private final Object LOCK = new Object();
+
+    private AtomicBoolean inQueue0=new AtomicBoolean(false);
+    private AtomicBoolean inQueue1=new AtomicBoolean(false);
+
+    public Task(Scheduler scheduler) {
+        this.scheduler = scheduler;
+    }
+
+    public Scheduler scheduler() {
+        return this.scheduler;
+    }
+
+    public void storedInQueue0()
+    {
+       inQueue0.set(true);
+    }
+
+    public void storedInQueue1()
+    {
+       inQueue1.set(true);
+    }
+
+    public void removeFromQueue0()
+    {
+       inQueue0.set(false);
+    }
+
+    public void removeFromQueue1()
+    {
+       inQueue1.set(false);
+    }
+
+    public Boolean isInQueue0()
+    {
+       return inQueue0.get();
+    }
+
+    public Boolean isInQueue1()
+    {
+       return inQueue1.get();
+    }
+
+    /**
+     * Current queue of this task.
+     *
+     * @return the value of queue
+     */
+    public abstract int getQueueNumber();
+
+
+    /**
+     * Executes task.
+     *
+     * @return dead line of next execution
+     */
+    public abstract long perform();
+
+    /**
+     * Cancels task execution
+     */
+    public void cancel() {
+       synchronized(LOCK) {
+               this.isActive = false;
+               //no needs to remove , since it will not run anyway.
+               //otherwise search on 2 queues is required
+
+               /*if(isHeartbeat)
+                       scheduler.heartBeatQueue.remove(this);
+               else
+                       scheduler.taskQueues[getQueueNumber()].remove(this);*/
+       }
+    }
+
+ //call should not be synchronized since can run only once in queue cycle
+    public void run() {
+               if (this.isActive)  {
+                       try {
+                               perform();
+
+                               //submit next partition
+//                     if (chain != null) chain.continueExecution();
+                       } catch (Exception e) {
+
+                       }
+               }
+
+               scheduler.notifyCompletion();
+    }
+
+    protected void activate(Boolean isHeartbeat) {
+       synchronized(LOCK) {
+               this.isActive = true;
+               this.isHeartbeat=isHeartbeat;
+       }
+    }
+
+    public boolean isActive()
+    {
+       return this.isActive;
+    }
+}

Reply via email to