
org.objectweb.dream.protocol.messagePassing.SerializatorMessagePassingProtocolImpl 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.Map;
import org.objectweb.dream.IOPushException;
import org.objectweb.dream.PushException;
import org.objectweb.dream.message.ChunkFactoryReference;
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.serializator.ByteArrayChunk;
import org.objectweb.dream.serializator.DeSerializatorItf;
import org.objectweb.dream.serializator.SerializatorItf;
import org.objectweb.dream.util.Error;
import org.objectweb.fractal.api.Component;
import org.objectweb.dream.dreamannotation.DreamComponent;
import org.objectweb.dream.dreamannotation.DreamMonolog;
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.Logger;
/**
* Message passing protocol which serializes outgoing messages and de-serializes
* incoming messages.
*
* @see org.objectweb.dream.serializator
*/
@DreamComponent(controllerDesc = "activeDreamUnstoppablePrimitive")
@Provides(interfaces = { @Interface(name = MessagePassingProtocol.ITF_NAME, signature = MessagePassingProtocol.class) })
public class SerializatorMessagePassingProtocolImpl implements MessagePassingProtocol {
private ChunkFactoryReference byteArrayChunkFactory;
// ------------------------------------------------------------------------
// ---
// Client interfaces
// ------------------------------------------------------------------------
// ---
@Requires(name = "lower-protocol")
protected MessagePassingProtocol lowerLevelProtocolItf;
@Requires(name = "message-manager")
protected MessageManagerType messageManagerItf;
@Requires(name = SerializatorItf.ITF_NAME)
protected SerializatorItf serializatorItf;
@Requires(name = DeSerializatorItf.ITF_NAME)
protected DeSerializatorItf deSerializatorItf;
// ------------------------------------------------------------------------
// --
// 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);
session.initialize(lowerLevelProtocolItf.export(session, hints));
return session;
}
/**
* @see org.objectweb.dream.protocol.Protocol#createExportIdentifier(Map,
* ExportIdentifier[])
*/
public ExportIdentifier createExportIdentifier(Map info, ExportIdentifier[] next)
throws InvalidExportIdentifierException {
return lowerLevelProtocolItf.createExportIdentifier(info, next);
}
// ------------------------------------------------------------------------
// ---
// Utility method
// ------------------------------------------------------------------------
// ---
private ByteArrayChunk newByteArrayChunk() {
if (byteArrayChunkFactory == null) {
byteArrayChunkFactory = messageManagerItf.getChunkFactory(ByteArrayChunk.class);
}
return messageManagerItf.createChunk(byteArrayChunkFactory);
}
// ------------------------------------------------------------------------
// ---
// Inner Class
// ------------------------------------------------------------------------
// ---
protected class Session implements MessagePassingOutgoingPush, IncomingPush {
private IncomingPush upperIncomingPush;
private MessagePassingOutgoingPush lowerOutgoingPush;
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();
}
public void outgoingPush(Message message, ExportIdentifier to)
throws InvalidExportIdentifierException, IOPushException {
Message byteMessage = messageManagerItf.createMessage();
ByteArrayChunk chunk;
try {
chunk = newByteArrayChunk();
chunk.setByteArray(serializatorItf.serialize(message));
} catch (IOException e) {
throw new IOPushException("An exception occurs while serializing message.", e);
}
messageManagerItf.deleteMessage(message);
messageManagerItf.addChunk(byteMessage, ByteArrayChunk.DEFAULT_NAME, chunk);
lowerOutgoingPush.outgoingPush(byteMessage, to);
}
public void outgoingClose(IncomingPush incomingPush) throws IOException {
lowerOutgoingPush.outgoingClose(this);
}
// --------------------------------------------------------------------
// -----
// Implementation of the IncomingPush interface
// --------------------------------------------------------------------
// -----
/**
* @see IncomingPush#incomingPush(Message)
*/
public void incomingPush(Message byteMessage) throws PushException {
ByteArrayChunk byteArrayChunk = messageManagerItf.getChunk(byteMessage,
ByteArrayChunk.DEFAULT_NAME);
if (byteArrayChunk == null) {
throw new PushException("Cannot find byte array chunk in incoming message.");
}
Message message;
try {
message = deSerializatorItf.deserialize(byteArrayChunk.getByteArray());
} catch (IOException e) {
throw new IOPushException("An exception occurs while de-serializing message.", e);
}
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);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy