Skip to content

Commit

Permalink
ns
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Sep 18, 2023
1 parent ec755d7 commit 92aa86e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
16 changes: 12 additions & 4 deletions src/org/jgroups/protocols/Frag3Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,18 @@ public Frag3Header(int id, int frag_id, int num_frags, int original_length, int
this.offset=offset;
}

public short getMagicId() {return 91;}
public Supplier<? extends Header> create() {return Frag3Header::new;}
public boolean needsDeserialization() {return needs_deserialization;}
public Frag3Header needsDeserialization(boolean flag) {needs_deserialization=flag; return this;}
public short getMagicId() {return 91;}
public Supplier<? extends Header> create() {return Frag3Header::new;}
public int getFragId() {return frag_id;}
public Frag3Header setFragId(int frag_id) {this.frag_id=frag_id; return this;}
public int getNumFrags() {return num_frags;}
public Frag3Header setNumFrags(int n) {this.num_frags=n; return this;}
public int getOriginalLength() {return original_length;}
public Frag3Header setOriginalLength(int l) {this.original_length=l; return this;}
public int getOffset() {return offset;}
public Frag3Header setOffset(int offset) {this.offset=offset; return this;}
public boolean needsDeserialization() {return needs_deserialization;}
public Frag3Header needsDeserialization(boolean f) {needs_deserialization=f; return this;}

public String toString() {
return String.format("[id=%d, frag-id=%d, num_frags=%d orig-length=%d, offset=%d]",
Expand Down
43 changes: 38 additions & 5 deletions src/org/jgroups/util/Headers.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package org.jgroups.util;

import org.jgroups.BaseMessage;
import org.jgroups.Global;
import org.jgroups.Header;
import org.jgroups.conf.ClassConfigurator;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* Helper class providing functions to manipulate the {@link BaseMessage#headers} array. The headers are stored
* in the array as follows:
* Helper class providing functions to manipulate the headers array in {@link org.jgroups.BaseMessage}.
* The headers are stored in the array as follows:
* <pre>
* Headers: hdr-1 | hdr-2 | hdr-3 | ... | hdr-n |
* </pre>
Expand All @@ -22,7 +22,7 @@
* putting a new key/header are operations with O(n) cost, so this implementation is <em>not</em> recommended for
* a large number of elements.
* <br/>
* This class is synchronized for writes (put(), resize()), but not for reads (size(), get())
* This class is unsynchronized.
* @author Bela Ban
*/
public final class Headers {
Expand Down Expand Up @@ -95,7 +95,7 @@ public static String printHeaders(final Header[] hdrs) {
first=false;
else
sb.append(", ");
Class clazz=ClassConfigurator.getProtocol(id);
Class<?> clazz=ClassConfigurator.getProtocol(id);
String name=clazz != null? clazz.getSimpleName() : Short.toString(id);
sb.append(name).append(": ").append(hdr);
}
Expand Down Expand Up @@ -136,6 +136,39 @@ public static Header[] putHeader(final Header[] headers, short id, Header hdr, b
throw new IllegalStateException("unable to add element " + id + ", index=" + i); // we should never come here
}

public static ByteArray writeHeaders(Header[] hdrs) throws IOException {
int size=Headers.size(hdrs);
ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(marshalledSize(hdrs));
out.writeShort(size);
if(size > 0) {
for(Header hdr : hdrs) {
if(hdr == null)
break;
short id=hdr.getProtId();
out.writeShort(id);
short magic_number=hdr.getMagicId();
out.writeShort(magic_number);
hdr.writeTo(out);
}
}
return out.getBuffer();
}

public static Header[] readHeaders(ByteArray buf) throws IOException, ClassNotFoundException {
ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf);
int len=in.readShort();
Header[] headers=new Header[len];
for(int i=0; i < len; i++) {
short id=in.readShort();
short magic_number=in.readShort();
Header hdr=ClassConfigurator.create(magic_number);
hdr.readFrom(in);
hdr.setProtId(id);
headers[i]=hdr;
}
return headers;
}

/**
* Increases the capacity of the array and copies the contents of the old into the new array
*/
Expand Down
18 changes: 13 additions & 5 deletions tests/junit-functional/org/jgroups/tests/SizeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.*;
import java.util.*;

import static org.jgroups.protocols.relay.RelayHeader.DATA;
Expand Down Expand Up @@ -197,7 +194,18 @@ public void testFdSockHeaders() throws Exception {
_testSize(hdr) ;
}


public void testHeaderMarshalling() throws IOException, ClassNotFoundException {
Header[] headers={
NakAckHeader2.createMessageHeader(322649),
UnicastHeader3.createDataHeader(1024, (short)22, true),
new Frag3Header(22, 2, 3)
};
ByteArray buf=Headers.writeHeaders(headers);
Header[] hdrs=Headers.readHeaders(buf);
assert hdrs[0].getClass().equals(NakAckHeader2.class);
assert ((UnicastHeader3)hdrs[1]).first();
assert ((Frag3Header)hdrs[2]).getFragId() == 2;
}

public void testUnicast3Header() throws Exception {
UnicastHeader3 hdr=UnicastHeader3.createDataHeader(322649, (short)127, false);
Expand Down

0 comments on commit 92aa86e

Please sign in to comment.