Skip to content

Commit

Permalink
Most Streamable classes now implement sub-interface SizeStreamable
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Nov 3, 2016
1 parent 73a9811 commit 3e8d56d
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/org/jgroups/View.java
Expand Up @@ -4,7 +4,7 @@

import org.jgroups.annotations.Immutable;
import org.jgroups.util.ArrayIterator;
import org.jgroups.util.Streamable;
import org.jgroups.util.SizeStreamable;
import org.jgroups.util.Util;

import java.io.DataInput;
Expand All @@ -24,7 +24,7 @@
* @author Bela Ban
*/
@Immutable
public class View implements Comparable<View>, Streamable, Iterable<Address>, Constructable<View> {
public class View implements Comparable<View>, SizeStreamable, Iterable<Address>, Constructable<View> {

/**
* A view is uniquely identified by its ViewID. The view id contains the creator address and a
Expand Down
4 changes: 2 additions & 2 deletions src/org/jgroups/ViewId.java
Expand Up @@ -2,7 +2,7 @@
package org.jgroups;

import org.jgroups.util.Bits;
import org.jgroups.util.Streamable;
import org.jgroups.util.SizeStreamable;
import org.jgroups.util.Util;

import java.io.DataInput;
Expand All @@ -15,7 +15,7 @@
* Ordering between views is important for example in a virtual synchrony protocol where
* all views seen by a member have to be ordered.
*/
public class ViewId implements Comparable<ViewId>, Streamable, Constructable<ViewId> {
public class ViewId implements Comparable<ViewId>, SizeStreamable, Constructable<ViewId> {
protected Address creator; // Address of the creator of this view
protected long id; // Lamport time of the view

Expand Down
2 changes: 1 addition & 1 deletion src/org/jgroups/protocols/PingData.java
Expand Up @@ -122,7 +122,7 @@ else if(isServer())
return sb.toString();
}

public int size() {
public int serializedSize() {
int retval=Global.BYTE_SIZE; // for is_server
retval+=Util.size(sender);
retval+=Global.BYTE_SIZE; // presence byte for logical_name
Expand Down
4 changes: 2 additions & 2 deletions src/org/jgroups/protocols/pbcast/JoinRsp.java
Expand Up @@ -6,7 +6,7 @@
import org.jgroups.Global;
import org.jgroups.View;
import org.jgroups.util.Digest;
import org.jgroups.util.Streamable;
import org.jgroups.util.SizeStreamable;

import java.io.DataInput;
import java.io.DataOutput;
Expand All @@ -16,7 +16,7 @@
/**
* Result of a JOIN request (sent by the GMS client). Instances of this class are immutable.
*/
public class JoinRsp implements Streamable, Constructable<JoinRsp> {
public class JoinRsp implements SizeStreamable, Constructable<JoinRsp> {
protected View view;
protected Digest digest;
protected String fail_reason; /** only set if JOIN failed, e.g. in AUTH */
Expand Down
7 changes: 4 additions & 3 deletions src/org/jgroups/protocols/tom/MessageID.java
Expand Up @@ -2,10 +2,11 @@

import org.jgroups.Address;
import org.jgroups.util.Bits;
import org.jgroups.util.Streamable;
import org.jgroups.util.SizeStreamable;
import org.jgroups.util.Util;

import java.io.*;
import java.io.DataInput;
import java.io.DataOutput;


/**
Expand All @@ -16,7 +17,7 @@
* @author Pedro Ruivo
* @since 3.1
*/
public class MessageID implements Comparable<MessageID>, Cloneable, Streamable {
public class MessageID implements Comparable<MessageID>, Cloneable, SizeStreamable {
private Address address = null;
private long id = -1;

Expand Down
4 changes: 2 additions & 2 deletions src/org/jgroups/stack/GossipData.java
Expand Up @@ -112,7 +112,7 @@ public String toString() {
return sb.toString();
}

public int size() {
public int serializedSize() {
int retval=Global.BYTE_SIZE; // type
if(group != null)
retval+=group.length() +2; // group
Expand All @@ -128,7 +128,7 @@ public int size() {
retval+=Global.SHORT_SIZE; // ping_data
if(ping_data != null)
for(PingData data: ping_data)
retval+=data.size();
retval+=data.serializedSize();

retval+=Util.size(physical_addr); // physical_addr
}
Expand Down
6 changes: 3 additions & 3 deletions src/org/jgroups/stack/GossipRouter.java
Expand Up @@ -256,7 +256,7 @@ public void receive(Address sender, byte[] buf, int offset, int length) {
rsp.addPingData(data);
}
}
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(rsp.size());
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(rsp.serializedSize());
try {
rsp.writeTo(out);
server.send(sender, out.buffer(), 0, out.position());
Expand Down Expand Up @@ -374,7 +374,7 @@ protected void route(String group, Address dest, byte[] msg, int offset, int len


protected void sendToAllMembersInGroup(Set<Map.Entry<Address,Entry>> dests, GossipData request) {
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(request.size());
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(request.serializedSize());
try {
request.writeTo(out);
}
Expand Down Expand Up @@ -415,7 +415,7 @@ protected void sendToAllMembersInGroup(Set<Map.Entry<Address,Entry>> dests, byte


protected void sendToMember(Address dest, GossipData request) {
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(request.size());
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(request.serializedSize());
try {
request.writeTo(out);
server.send(dest, out.buffer(), 0, out.position());
Expand Down
4 changes: 2 additions & 2 deletions src/org/jgroups/stack/RouterStub.java
Expand Up @@ -119,7 +119,7 @@ public void connect(String group, Address addr, String logical_name, PhysicalAdd
_doConnect();
}
GossipData request=new GossipData(GossipType.REGISTER, group, addr, logical_name, phys_addr);
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(request.size()+10);
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(request.serializedSize()+10);
request.writeTo(out);
client.send(remote, out.buffer(), 0, out.position());
}
Expand Down Expand Up @@ -240,7 +240,7 @@ public String toString() {


protected synchronized void writeRequest(GossipData req) throws Exception {
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(req.size());
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(req.serializedSize());
req.writeTo(out);
client.send(remote, out.buffer(), 0, out.position());
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/jgroups/util/Range.java
Expand Up @@ -6,7 +6,7 @@
import java.io.DataOutput;


public class Range implements Streamable, Comparable<Range> {
public class Range implements SizeStreamable, Comparable<Range> {
public long low=-1; // first msg to be retransmitted
public long high=-1; // last msg to be retransmitted

Expand Down
2 changes: 1 addition & 1 deletion src/org/jgroups/util/SeqnoList.java
Expand Up @@ -14,7 +14,7 @@
* @author Bela Ban
* @since 3.1
*/
public class SeqnoList extends FixedSizeBitSet implements Streamable, Iterable<Long>, Constructable<SeqnoList> {
public class SeqnoList extends FixedSizeBitSet implements SizeStreamable, Iterable<Long>, Constructable<SeqnoList> {
protected long offset; // first seqno

/** Only to be used by serialization */
Expand Down
2 changes: 1 addition & 1 deletion src/org/jgroups/util/SizeStreamable.java
Expand Up @@ -8,5 +8,5 @@
*/
public interface SizeStreamable extends Streamable {
/** Returns the size (in bytes) of the marshalled object */
int size();
int serializedSize();
}
6 changes: 4 additions & 2 deletions src/org/jgroups/util/Util.java
Expand Up @@ -963,13 +963,15 @@ public static <T extends Streamable> T streamableFromBuffer(Class<T> clazz,byte[


public static byte[] streamableToByteBuffer(Streamable obj) throws Exception {
final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(512);
int expected_size=obj instanceof SizeStreamable? ((SizeStreamable)obj).serializedSize() : 512;
final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size);
obj.writeTo(out);
return Arrays.copyOf(out.buffer(), out.position());
}

public static Buffer streamableToBuffer(Streamable obj) {
final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(512);
int expected_size=obj instanceof SizeStreamable? ((SizeStreamable)obj).serializedSize() +1 : 512;
final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size);
try {
Util.writeStreamable(obj,out);
return out.getBuffer();
Expand Down
2 changes: 1 addition & 1 deletion tests/junit-functional/org/jgroups/tests/MessageTest.java
Expand Up @@ -410,7 +410,7 @@ public void testSizeMessageWithDestAndSrcAndHeaders() throws Exception {
_testSize(msg);
}

public static void testReadFromSkipPayload() throws Exception {
public void testReadFromSkipPayload() throws Exception {
Message msg=new Message(Util.createRandomAddress("A"), "bela".getBytes()).src(Util.createRandomAddress("B"));
addHeaders(msg);
byte[] buf=Util.streamableToByteBuffer(msg);
Expand Down
Expand Up @@ -113,7 +113,7 @@ public void testSerialization() throws Exception {
System.out.println("list.size()=" + list.size() + "\nlist = " + list);
int expected_size=list.serializedSize();
byte[] buf=Util.streamableToByteBuffer(list);
SeqnoList list2=(SeqnoList)Util.streamableFromByteBuffer(SeqnoList.class,buf);
SeqnoList list2=Util.streamableFromByteBuffer(SeqnoList.class, buf);
System.out.println("list2.size()=" + list2.size() + "\nlist2 = " + list2);
assert list.size() == list2.size();

Expand Down
2 changes: 1 addition & 1 deletion tests/junit-functional/org/jgroups/tests/SizeTest.java
Expand Up @@ -895,7 +895,7 @@ private static void _testSize(JoinRsp rsp) throws Exception {

private static void _testSize(SizeStreamable data) throws Exception {
System.out.println("\ndata: " + data);
long size=data.size();
long size=data.serializedSize();
byte[] serialized_form=Util.streamableToByteBuffer(data);
System.out.println("size=" + size + ", serialized size=" + serialized_form.length);
assert serialized_form.length == size : "serialized length=" + serialized_form.length + ", size=" + size;
Expand Down

0 comments on commit 3e8d56d

Please sign in to comment.