
org.objectweb.dream.protocol.messagePassing.BufferedMessagePassingProtocolImpl 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):
*/
package org.objectweb.dream.protocol.messagePassing;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.objectweb.dream.IOPushException;
import org.objectweb.dream.PushException;
import org.objectweb.dream.control.activity.Util;
import org.objectweb.dream.control.activity.manager.TaskManager;
import org.objectweb.dream.control.activity.task.AbstractTask;
import org.objectweb.dream.control.activity.task.TaskController;
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.dream.protocol.ExportException;
import org.objectweb.dream.protocol.ExportIdentifier;
import org.objectweb.dream.protocol.IncomingPush;
import org.objectweb.dream.protocol.InvalidExportIdentifierException;
import org.objectweb.dream.protocol.channel.ChannelProtocol;
import org.objectweb.dream.util.Dream;
import org.objectweb.dream.util.Error;
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;
/**
* Implementation of a decoupling bus protocol.
*/
@DreamComponent(controllerDesc = "activeDreamUnstoppablePrimitive")
@Provides(interfaces = { @Interface(name = MessagePassingProtocol.ITF_NAME, signature = MessagePassingProtocol.class) })
public class BufferedMessagePassingProtocolImpl implements MessagePassingProtocol {
private static final boolean DEFENSIVE_CHECKS = true;
// ---------------------------------------------------------------------------
// Client interfaces
// ---------------------------------------------------------------------------
@Requires(name = "lower-protocol")
protected MessagePassingProtocol lowerLevelProtocolItf;
@Requires(name = "message-manager")
protected MessageManagerType messageManagerItf;
@Requires(name = "task-manager")
protected TaskManager taskManagerItf;
// --------------------------------------------------------------------------
// Services interfaces
// --------------------------------------------------------------------------
/**
* Component reference
*/
@Service
Component weaveableC;
/**
* Logger of the component
*/
@DreamMonolog()
protected Logger logger;
// ---------------------------------------------------------------------------
// Implementation of the MessagePassingProtocol interface
// ---------------------------------------------------------------------------
/**
* @see MessagePassingProtocol#export(IncomingPush, Map)
*/
public MessagePassingOutgoingPush export(IncomingPush incomingPushItf, Map hints)
throws ExportException {
Session session = new Session(incomingPushItf);
try {
Util.addTask(weaveableC, session.sendTask, new HashMap());
} catch (Exception e) {
throw new ExportException("Unable to register send task.", e);
}
MessagePassingOutgoingPush lowerPush = lowerLevelProtocolItf.export(session, hints);
session.initialize(lowerPush);
return session;
}
/**
* @see ChannelProtocol#createExportIdentifier(Map, ExportIdentifier[])
*/
public ExportIdentifier createExportIdentifier(Map info, ExportIdentifier[] next)
throws InvalidExportIdentifierException {
return lowerLevelProtocolItf.createExportIdentifier(info, next);
}
// ---------------------------------------------------------------------------
// Inner classes
// ---------------------------------------------------------------------------
protected class Session implements MessagePassingOutgoingPush, IncomingPush {
protected IncomingPush upperIncomingPush;
protected MessagePassingOutgoingPush lowerOutgoingPush;
protected SendTask sendTask = new SendTask();
protected boolean closed = false;
protected ListElem first;
protected ListElem last;
protected Session(IncomingPush upperIncomingPush) {
this.upperIncomingPush = upperIncomingPush;
}
protected void initialize(MessagePassingOutgoingPush lowerPush) {
this.lowerOutgoingPush = lowerPush;
}
// -------------------------------------------------------------------------
// Implementation of the MessagePassingOutgoingPush interface
// -------------------------------------------------------------------------
/**
* @see MessagePassingOutgoingPush#getLocalExportIdentifier()
*/
public ExportIdentifier getLocalExportIdentifier() {
return lowerOutgoingPush.getLocalExportIdentifier();
}
/**
* @see MessagePassingOutgoingPush#outgoingPush(Message,
* ExportIdentifier)
*/
public synchronized void outgoingPush(Message message, ExportIdentifier to)
throws InvalidExportIdentifierException, IOPushException {
if (closed) {
throw new IOPushException("Session is closed");
}
ListElem elem = new ListElem(to, message);
if (last == null) {
if (DEFENSIVE_CHECKS && first != null) {
Error.bug(logger);
}
first = elem;
last = elem;
notify();
} else {
if (DEFENSIVE_CHECKS && first == null) {
Error.bug(logger);
}
last.next = elem;
last = elem;
}
}
/**
* @see MessagePassingOutgoingPush#outgoingClose(IncomingPush)
*/
public void outgoingClose(IncomingPush incomingPush) throws IOException {
synchronized (this) {
lowerOutgoingPush.outgoingClose(this);
closed = true;
while (first != null) {
messageManagerItf.deleteMessage(first.message);
first = first.next;
}
notify();
}
try {
TaskController taskController = Dream.getTaskController(weaveableC);
taskController.removeTask(sendTask);
} catch (Exception e) {
Error.bug(logger, e);
}
}
// -------------------------------------------------------------------------
// Implementation of the IncomingPush interface
// -------------------------------------------------------------------------
/**
* @see IncomingPush#incomingPush(Message)
*/
public void incomingPush(Message message) throws PushException {
// just forward the message.
upperIncomingPush.incomingPush(message);
}
/**
* @see IncomingPush#incomingClosed(Object, Exception)
*/
public synchronized void incomingClosed(Object outgoingPush, Exception exception) {
// never called on message passing protocol
Error.bug(logger);
}
class ListElem {
ExportIdentifier to;
Message message;
ListElem next;
ListElem(ExportIdentifier to, Message message) {
this.to = to;
this.message = message;
}
}
protected class SendTask extends AbstractTask {
public SendTask() {
super("Send task");
}
public Object execute(Object hints) throws InterruptedException {
ListElem elem;
synchronized (Session.this) {
if (closed) {
return STOP_EXECUTING;
}
while (first == null) {
Session.this.wait();
}
if (closed) {
return STOP_EXECUTING;
}
elem = first;
first = elem.next;
if (first == null) {
last = null;
}
}
try {
lowerOutgoingPush.outgoingPush(elem.message, elem.to);
} catch (Exception e) {
logger.log(BasicLevel.ERROR, "A push exception occurs while sending message.",
e);
messageManagerItf.deleteMessage(elem.message);
}
return EXECUTE_AGAIN;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy