/*
 * This file is part of the OpenNMS(R) Application.
 *
 * OpenNMS(R) is Copyright (C) 2009 The OpenNMS Group, Inc.  All rights reserved.
 * OpenNMS(R) is a derivative work, containing both original code, included code and modified
 * code that was published under the GNU General Public License. Copyrights for modified
 * and included code are below.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * For more information contact:
 * OpenNMS Licensing       <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 */
package org.opennms.sms.reflector.smsservice;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

import org.opennms.protocols.rt.Request;

/**
 * SmsRequest
 *
 * @author brozow
 */
public abstract class MobileMsgRequest implements Request<String, MobileMsgRequest, MobileMsgResponse> {
    
    private long m_timeout;
    private int m_retries;
    private MobileMsgResponseCallback m_cb;
    private MobileMsgResponseMatcher m_responseMatcher;
    
    private long m_expiration;
	private long m_sentTime;

    public MobileMsgRequest(long timeout, int retries, MobileMsgResponseCallback cb, MobileMsgResponseMatcher responseMatcher) {
        m_timeout = timeout;
        m_retries = retries;
        m_cb = cb;
        m_responseMatcher = responseMatcher;
    }
    
    public long getSentTime() {
    	return m_sentTime;
    }
    
    /**
     * @return the timeout
     */
    public long getTimeout() {
        return m_timeout;
    }

    /**
     * @param timeout the timeout to set
     */
    public void setTimeout(long timeout) {
        m_timeout = timeout;
    }

    /**
     * @return the retries
     */
    public int getRetries() {
        return m_retries;
    }

    /**
     * @param retries the retries to set
     */
    public void setRetries(int retries) {
        m_retries = retries;
    }

    /**
     * @return the cb
     */
    public MobileMsgResponseCallback getCb() {
        return m_cb;
    }

    /**
     * @param cb the cb to set
     */
    public void setCb(MobileMsgResponseCallback cb) {
        m_cb = cb;
    }

    /**
     * @return the responseMatcher
     */
    public MobileMsgResponseMatcher getResponseMatcher() {
        return m_responseMatcher;
    }

    /**
     * @param responseMatcher the responseMatcher to set
     */
    public void setResponseMatcher(MobileMsgResponseMatcher responseMatcher) {
        m_responseMatcher = responseMatcher;
    }

    public void setSendTimestamp(long timeInMillis) {
    	m_sentTime = timeInMillis;
        m_expiration = timeInMillis + m_timeout;
    }

    public long getDelay(TimeUnit unit) {
        return unit.convert(m_expiration - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    public abstract String getId();

    public void processError(Throwable t) {
        m_cb.handleError(this, t);
    }

    public boolean processResponse(MobileMsgResponse response) {
    	response.setRequest(this);
        return m_cb.handleResponse(this, response);
    }

    public MobileMsgRequest processTimeout() {
        MobileMsgRequest retry = createNextRetry();
        if (retry == null) {
            m_cb.handleTimeout(this);
        }
        return retry;
    }
    
    public abstract MobileMsgRequest createNextRetry();

    public int compareTo(Delayed o) {
        long thisVal = this.getDelay(TimeUnit.NANOSECONDS);
        long anotherVal = o.getDelay(TimeUnit.NANOSECONDS);
        return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
    }

    public boolean matches(MobileMsgResponse response) {
        return m_responseMatcher.matches(this, response);
    }


}

@@ -1904,16 +1904,65 @@
 entTime;
+%0A%09%0A%09private volatile boolean m_processed = false;
 %0A%0A    pu
@@ -3726,24 +3726,42 @@
 owable t) %7B%0A
+        try %7B%0A    
         m_cb
@@ -3783,16 +3783,78 @@
 is, t);%0A
+        %7D finally %7B%0A            m_processed = true;%0A        %7D%0A
     %7D%0A%0A 
@@ -3921,17 +3921,38 @@
 ) %7B%0A    
-%09
+    try %7B%0A            
 response
@@ -3962,32 +3962,36 @@
 tRequest(this);%0A
+    
         return m
@@ -4022,24 +4022,86 @@
  response);%0A
+        %7D finally %7B%0A            m_processed = true;%0A        %7D%0A
     %7D%0A%0A    p
@@ -4134,32 +4134,50 @@
 cessTimeout() %7B%0A
+        try %7B%0A    
         MobileMs
@@ -4212,16 +4212,20 @@
 etry();%0A
+    
         
@@ -4249,32 +4249,36 @@
 ) %7B%0A            
+    
 m_cb.handleTimeo
@@ -4279,32 +4279,36 @@
 eTimeout(this);%0A
+    
         %7D%0A      
@@ -4305,24 +4305,28 @@
   %7D%0A        
+    
 return retry
@@ -4327,16 +4327,78 @@
  retry;%0A
+        %7D finally %7B%0A            m_processed = true;%0A        %7D%0A
     %7D%0A  
@@ -4818,11 +4818,85 @@
 %0A    %7D%0A%0A
+    public boolean isProcessed() %7B%0A        return m_processed;%0A    %7D%0A%0A    
 %0A%7D%0A

converti sms-service to tracker 0.4