From cd731b745c9b8f4afb78147591626a2df119bfbf Mon Sep 17 00:00:00 2001 From: Bela Ban Date: Thu, 14 Nov 2024 16:07:04 +0100 Subject: [PATCH] Changed to CompositeMessage (https://issues.redhat.com/browse/JGRP-2848) --- src/org/jgroups/CompositeMessage.java | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/org/jgroups/CompositeMessage.java b/src/org/jgroups/CompositeMessage.java index 795d55bb65..1402162f3f 100644 --- a/src/org/jgroups/CompositeMessage.java +++ b/src/org/jgroups/CompositeMessage.java @@ -7,10 +7,7 @@ import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; -import java.util.Arrays; -import java.util.Iterator; -import java.util.NoSuchElementException; -import java.util.Objects; +import java.util.*; import java.util.function.Supplier; /** @@ -44,6 +41,11 @@ public CompositeMessage(Address dest, Message ... messages) { add(messages); } + public CompositeMessage(Address dest, Collection messages) { + super(dest); + add(messages); + } + public Supplier create() {return CompositeMessage::new;} public short getType() {return collapse? Message.BYTES_MSG : Message.COMPOSITE_MSG;} @@ -84,6 +86,12 @@ public CompositeMessage add(Message ... messages) { return this; } + public CompositeMessage add(Collection messages) { + ensureCapacity(index + messages.size()); + for(Message msg: messages) + msgs[index++]=Objects.requireNonNull(ensureSameDest(msg)); + return this; + } public T get(int index) { return (T)msgs[index]; @@ -134,7 +142,7 @@ public void writePayload(DataOutput out) throws IOException { for(int i=0; i < index; i++) { Message msg=msgs[i]; out.writeShort(msg.getType()); - msg.writeTo(out); + msg.writeToNoAddrs(src(), out); } } } @@ -147,6 +155,11 @@ public void readPayload(DataInput in) throws IOException, ClassNotFoundException short type=in.readShort(); msgs[i]=MessageFactory.create(type); msgs[i].readFrom(in); + Message msg=MessageFactory.create(type).setDest(getDest()); + if(msg.getSrc() == null) + msg.setSrc(src()); + msg.readFrom(in); + msgs[i]=msg; } } }