All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objectweb.dream.queue.keyed.buffer.BufferAscendingSequenceNumberKeyedAddKeyedRemove Maven / Gradle / Ivy

/**
 * DREAM
 * Copyright (C) 2003-2004 INRIA Rhone-Alpes
 *
 * 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 of the License, or (at your option) 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
 *
 * Contact : [email protected]
 *
 * Initial developer(s): Vivien Quema
 * Contributor(s): Romain Lenglet
 */

package org.objectweb.dream.queue.keyed.buffer;

import java.util.HashMap;
import java.util.Map;

import org.objectweb.dream.message.Message;
import org.objectweb.dream.message.MessageManagerType;
import org.objectweb.dream.queue.SequenceNumberChunk;
import org.objectweb.dream.queue.keyed.MissingSequenceNumberKeyBased;
import org.objectweb.dream.util.Error;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.fraclet.annotation.annotations.Attribute;
import org.objectweb.fractal.fraclet.annotation.annotations.Service;
import org.objectweb.dream.dreamannotation.DreamComponent;
import org.objectweb.dream.dreamannotation.DreamMonolog;
import org.objectweb.fractal.fraclet.annotation.annotations.Requires;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;

/**
 * This buffer stores incoming messages according to
 * 
    *
  • a sequence number that is retrieved using a chunk that extends the * {@link SequenceNumberChunk}class. The name of this chunk can be specified * using the * {@link org.objectweb.dream.queue.buffer.BufferAttributeControllerAscendingSequenceNumber} *
  • *
  • a key that is explicitely passed as a parameter, when adding the message * using the {@link #add(Message, Object)}method.
  • *
* The {@link #doRemove(Object)}method removes the last message in sequence for * the specified key. *

* For each key, when there is a gap upon adding a message, missing sequence * numbers are made known using the {@link MissingSequenceNumberKeyBased} * interface. For instance, a component can use this interface to issue * retransmission requests. */ @DreamComponent(controllerDesc = "dreamUnstoppablePrimitive") public class BufferAscendingSequenceNumberKeyedAddKeyedRemove extends AbstractBufferKeyedAddKeyedRemove { // --------------------------------------------------------------------------- // client interface. // --------------------------------------------------------------------------- @Requires(name = MissingSequenceNumberKeyBased.ITF_NAME) protected MissingSequenceNumberKeyBased missingSequenceNumberKeyBasedItf; @Requires(name = "message-manager") protected MessageManagerType messageManagerItf; // -------------------------------------------------------------------------- // Services interfaces // -------------------------------------------------------------------------- /** * Component reference */ @Service Component weaveableC; /** * Logger of the component */ @DreamMonolog() protected Logger logger; // --------------------------------------------------------------------------- // Attributes // --------------------------------------------------------------------------- @Attribute(argument = "sortingChunkName") String sortingChunkName = SequenceNumberChunk.DEFAULT_NAME; // --------------------------------------------------------------------------- // Fields // --------------------------------------------------------------------------- /** Map that maintains messages keyed by keys. */ private Map queue = new HashMap(); // --------------------------------------------------------------------------- // Implemented methods for the Buffer interface. // --------------------------------------------------------------------------- /** * @see AbstractBufferKeyedAddKeyedRemove#doAdd(Message, Object) */ protected void doAdd(Message message, Object key) { SequenceNumberChunk sequenceNumberChunk = messageManagerItf .getChunk(message, sortingChunkName); long sn = sequenceNumberChunk.getSequenceNumber(); List msgsReceivedFromKey = queue.get(key); if (msgsReceivedFromKey == null) { // create a list for the specified key msgsReceivedFromKey = new List(); queue.put(key, msgsReceivedFromKey); } if (msgsReceivedFromKey.add(message, sn, key)) { incrementStoredMessagesCount(1, key); } } /** * @see AbstractBufferKeyedAddKeyedRemove#doRemove(Object) */ protected Message doRemove(Object key) { List msgsReceivedFromKey = queue.get(key); if (msgsReceivedFromKey == null) { return null; } // We don't remove msgsReceivedFromKey from the queue. Should be done by a // membership component Message m = msgsReceivedFromKey.remove(); if (m != null) { incrementStoredMessagesCount(-1, key); } return m; } /** * @see AbstractBufferKeyedAddKeyedRemove#doRemoveAll() */ protected Message doRemoveAll() { // TODO implement this method throw new UnsupportedOperationException(); } /** * @see AbstractBufferKeyedAddKeyedRemove#doGet(Object) */ protected Message doGet(Object key) { // TODO implement this method throw new UnsupportedOperationException(); } /** * @see AbstractBufferKeyedAddKeyedRemove#hasAvailableMessage(Object) */ protected boolean hasAvailableMessage(Object key) { List msgsReceivedFromKey = queue.get(key); if (msgsReceivedFromKey == null) { return false; } // We don't remove msgsReceivedFromKey from the queue. Should be done by a // membership component return msgsReceivedFromKey.removable > 0; } // --------------------------------------------------------------------------- // Implementation of BufferMonitoringKeyed interface. // --------------------------------------------------------------------------- /** * @see BufferMonitoringKeyed#getKeys() */ public Object[] getKeys() { synchronized (lock) { return queue.keySet().toArray(); } } // --------------------------------------------------------------------------- // Methods related to the class // --------------------------------------------------------------------------- /** * Get the last sequence number for messages linked to the key * * @param key key associated to the list of messages * @return *

    *
  • The last sequence number if there is a message associated to * the key given as parameter
  • *
  • -1 otherwise
  • *
* @throws InterruptedException */ public long getLastInSequence(Object key) throws InterruptedException { synchronized (lock) { List msgsReceivedFromKey = queue.get(key); if (msgsReceivedFromKey == null) { return -1; } return msgsReceivedFromKey.lastSequenceNumberInSequence; } } /** * Get the last sequence number for the list messages linked to specified the * key * * @param key key associated to the list of messages * @param lastInSequence * @throws InterruptedException */ public void setLastInSequence(Object key, long lastInSequence) throws InterruptedException { if (lastInSequence < 0) { Error.error("last in sequence number must be a positive value !", logger); } synchronized (lock) { List msgsReceivedFromKey = queue.get(key); if (msgsReceivedFromKey == null) { Error.error("No such key", logger); } msgsReceivedFromKey.lastSequenceNumberInSequence = lastInSequence; } } // --------------------------------------------------------------------------- // Inner class // --------------------------------------------------------------------------- private class List { /** The first element stored in this buffer. */ protected Element first = null; /** The last element stored in this buffer. */ protected Element last = null; /** The last sequence number in sequence. */ protected long lastSequenceNumberInSequence = -1; /** * The number of removable objects. */ protected int removable = 0; /** * The number of stored objects. */ protected int stored = 0; /** * This method adds a message in the list. * * @param message the object to be added. * @param sn the sequence number of the object to be added. * @param source the source of the message. * @return true if the message has been added. */ boolean add(Message message, long sn, Object source) { if (sn <= lastSequenceNumberInSequence) { if (logger.isLoggable(BasicLevel.DEBUG)) { logger.log(BasicLevel.DEBUG, "Message " + sn + " has already been delivered."); } messageManagerItf.deleteMessage(message); return false; } Element current = last; while (current != null && sn < current.sequenceNumber) { current = current.previous; } if (current != null && sn == current.sequenceNumber) { // The message is already stored in the list. if (logger.isLoggable(BasicLevel.DEBUG)) { logger.log(BasicLevel.DEBUG, "Message " + sn + " is already stored in the list."); } messageManagerItf.deleteMessage(message); return false; } Element element = new Element(); element.message = message; element.sequenceNumber = sn; if (current == null) { // insert at the beginning if (first != null) { // list not empty element.next = first; first.previous = element; first = element; if (logger.isLoggable(BasicLevel.DEBUG)) { logger.log(BasicLevel.DEBUG, "Message " + sn + " is no longer missing."); } missingSequenceNumberKeyBasedItf.noLongerMissing(sn, source); } else { // list is empty, first = last last = element; first = element; if (sn != lastSequenceNumberInSequence + 1) { // Some messages are missing if (missingSequenceNumberKeyBasedItf != null) { missingSequenceNumberKeyBasedItf.missingSequenceNumber( lastSequenceNumberInSequence + 1, sn - 1, source); } } } } else { // insert after current element.previous = current; if (current.next != null) { // new element is not the new last element.next = current.next; element.next.previous = element; if (logger.isLoggable(BasicLevel.DEBUG)) { logger.log(BasicLevel.DEBUG, "Message " + sn + " is no longer missing."); } missingSequenceNumberKeyBasedItf.noLongerMissing(sn, source); } else { // insert at the end last = element; if (sn > element.previous.sequenceNumber + 1) { // Some messages are missing if (missingSequenceNumberKeyBasedItf != null) { missingSequenceNumberKeyBasedItf.missingSequenceNumber( element.previous.sequenceNumber + 1, sn - 1, source); } } } element.previous.next = element; } // update last in sequence current = element; int counter = 0; while (current != null && current.sequenceNumber == lastSequenceNumberInSequence + 1) { counter++; lastSequenceNumberInSequence++; current = current.next; } if (counter > 0) { removable += counter; } stored++; return true; } /** * Removes an object from the list. * * @return an object with a sequence number that is 1 higher than the * previously removed object, null if no such message * exists. */ Message remove() { if (removable > 0) { removable--; stored--; Element newFirst = first.next; if (newFirst != null) { newFirst.previous = null; } else { last = null; } Message message = first.message; first = newFirst; return message; } return null; } /** * This class represents elements of a linked list. Each element contains a * reference to its predecessor (previous), its successor ( * next), a message, and a sequence number. */ public class Element { Element previous; Element next; Message message; long sequenceNumber; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy