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

org.objectweb.dream.protocol.atomicity.AtomicReactor 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): Matthieu Leclercq
 * Contributor(s): Romain Lenglet
 */

package org.objectweb.dream.protocol.atomicity;

import org.objectweb.dream.Pull;
import org.objectweb.dream.PullException;
import org.objectweb.dream.Push;
import org.objectweb.dream.PushException;
import org.objectweb.dream.dreamannotation.DreamComponent;
import org.objectweb.dream.dreamannotation.DreamMonolog;
import org.objectweb.dream.message.Message;
import org.objectweb.dream.message.MessageManagerType;
import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.fraclet.annotation.annotations.Interface;
import org.objectweb.fractal.fraclet.annotation.annotations.Provides;
import org.objectweb.fractal.fraclet.annotation.annotations.Requires;
import org.objectweb.fractal.fraclet.annotation.annotations.Service;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;

/**
 * Transient implementation of the Reactor component. This implementation
 * guarantees that notifications emitted by the reacting agent are effectively
 * sent if the reaction doesn't throw an exception.
 */
@DreamComponent(controllerDesc = "dreamPrimitive")
@Provides(interfaces = { @Interface(name = "incoming-in-push", signature = Push.class) })
public class AtomicReactor implements Push {

    // ------------------------------------------------------------------------
    // ---
    // Client Interfaces
    // ------------------------------------------------------------------------
    // ---

    /**
     * The name of the client interface used to pass message received on the
     * incoming-in-push interface.
     */
    @Requires(name = "incoming-out-push")
    private Push incomingOutPushItf;

    /**
     * The name of the client interface used to send message pulled from the
     * waiting list.
     */
    @Requires(name = "outgoing-out-push")
    private Push outgoingOutPushItf;

    /**
     * The name of the client interface used to retrieve notifications emitted
     * during reaction. This interface MUST be in non blocking mode. 
     */
    public static final String WAITING_PULL_ITF_NAME = "waitingPull";

    @Requires(name = WAITING_PULL_ITF_NAME)
    private Pull waitingPullItf;

    @Requires(name = SetReactorThread.ITF_NAME)
    private SetReactorThread setReactorThreadItf;

    @Requires(name = "message-manager")
    private MessageManagerType messageManagerItf;

    // ------------------------------------------------------------------------
    // --
    // Services interfaces
    // ------------------------------------------------------------------------
    // --

    /**
     * Component reference
     */
    @Service
    Component weaveableC;

    /**
     * Logger of the component
     */
    @DreamMonolog()
    protected Logger logger;

    /** The previous thread that was executing a reaction. */
    private Thread previousReactorThread = null;

    /**
     * @see Push#push(Message)
     */
    public synchronized void push(Message message) throws PushException {

        Thread currentThread = Thread.currentThread();
        if (currentThread != previousReactorThread) {
            previousReactorThread = currentThread;
            setReactorThreadItf.setReactorThread(currentThread);
        }

        try {
            try {
                if (logger.isLoggable(BasicLevel.DEBUG)) {
                    logger.log(BasicLevel.DEBUG, "Forward incoming message : " + message);
                }
                incomingOutPushItf.push(message);
            } catch (PushException e) {
                logger.log(BasicLevel.INFO,
                        "Exception catched during reaction, emited messages are dropped. (message="
                                + message + ")", e.getCause());

                Message msg = waitingPullItf.pull();
                while (msg != null) {
                    messageManagerItf.deleteMessage(msg);
                    msg = waitingPullItf.pull();
                }
            }

            Message msg = waitingPullItf.pull();
            while (msg != null) {
                if (logger.isLoggable(BasicLevel.DEBUG)) {
                    logger.log(BasicLevel.DEBUG, "Push outgoing message : " + message);
                }
                outgoingOutPushItf.push(msg);
                msg = waitingPullItf.pull();
            }
        } catch (PullException e) {
            throw new PushException("An error occurs while pulling waiting messages", e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy