/*
 * JORAM: Java(TM) Open Reliable Asynchronous Messaging
 * Copyright (C) 2004 - 2025 ScalAgent Distributed Technologies
 * Copyright (C) 2004 France Telecom R&D
 *
 * This library 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 any later version.
 * 
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA.
 *
 * Initial developer(s): ScalAgent Distributed Technologies
 */
package org.objectweb.joram.mom.proxies;

import java.util.Vector;

import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;

import fr.dyade.aaa.common.Debug;

public class AckedQueue implements java.io.Serializable {
  /** define serialVersionUID for interoperability */
  private static final long serialVersionUID = 1L;

  /** logger */
  private static final Logger logger = Debug.getLogger(AckedQueue.class.getName());
  private static final boolean DEBUG = logger.isLoggable(BasicLevel.DEBUG);

  private Vector<ProxyMessage> list;
  
  private int current;

  public AckedQueue() {
    list = new Vector<ProxyMessage>();
    current = 0;
  }

  public void push(ProxyMessage msg) {
    if (DEBUG)
      logger.log(BasicLevel.DEBUG, "AckedQueue.push(" + msg + ')');

    synchronized (list) {
      list.addElement(msg);
      list.notify();
    }
  }

  public ProxyMessage get() throws InterruptedException {
    if (DEBUG)
      logger.log(BasicLevel.DEBUG, "AckedQueue.get()");

    synchronized (list) { 
      while ((list.size() - current) == 0) {
        list.wait();
      }      
      ProxyMessage msg = list.elementAt(current);
      current++;
      return msg;
    }
  }

  public void ack(long ackId) {
    if (DEBUG)
      logger.log(BasicLevel.DEBUG, "AckedQueue.ack(" + ackId + ')');
    
    synchronized (list) {
      while (list.size() > 0) {
        ProxyMessage m = list.elementAt(0);
        if (ackId < m.getId())  return;

        // acked
        if (DEBUG)
          logger.log(BasicLevel.DEBUG, "AckedQueue acked " + m.getId());
        
        list.removeElementAt(0);
        if (current > 0) current--;
      }
    }
  }

  public void reset() {
    if (DEBUG)
      logger.log(BasicLevel.DEBUG, "AckedQueue.reset()");
    
    current = 0;
  }
  
  public int size() {
    return list.size();
  }
}
