use of org.jgroups.Header in project JGroups by belaban.
the class Headers method printHeaders.
public static String printHeaders(final Header[] hdrs) {
if (hdrs == null)
return "";
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Header hdr : hdrs) {
if (hdr == null)
break;
short id = hdr.getProtId();
if (first)
first = false;
else
sb.append(", ");
Class clazz = ClassConfigurator.getProtocol(id);
String name = clazz != null ? clazz.getSimpleName() : Short.toString(id);
sb.append(name).append(": ").append(hdr);
}
return sb.toString();
}
use of org.jgroups.Header in project JGroups by belaban.
the class Headers method marshalledSize.
public static int marshalledSize(final Header[] hdrs) {
int retval = 0;
if (hdrs == null)
return retval;
for (Header hdr : hdrs) {
if (hdr == null)
break;
// for protocol ID and magic number
retval += Global.SHORT_SIZE * 2;
retval += hdr.serializedSize();
}
return retval;
}
use of org.jgroups.Header in project geode by apache.
the class StatRecorder method filter.
private void filter(Message msg, int direction) {
if (direction == INCOMING) {
Header h = msg.getHeader(frag2HeaderId);
boolean copyBuffer = false;
if (h != null && h instanceof FragHeader) {
copyBuffer = true;
// String str = direction == OUTGOING? "sending" : "receiving";
// logger.debug("{} fragment {} msg buffer hash {} offset {} msg size {} first bytes=\n{}",
// str, hdr,
// msg.getRawBuffer().hashCode(), msg.getOffset(), msg.getLength(),
// GMSUtil.formatBytes(msg.getRawBuffer(), msg.getOffset(),
// Math.min(200, msg.getLength())));
} else {
h = msg.getHeader(unicastHeaderId);
if (h instanceof UNICAST3.Header) {
copyBuffer = true;
} else {
h = msg.getHeader(nakackHeaderId);
if (h instanceof NakAckHeader2) {
copyBuffer = true;
}
}
}
if (copyBuffer) {
// JGroups doesn't copy its message buffer when thread pools are
// disabled. This causes Frag2 fragments to become corrupted
msg.setBuffer(msg.getBuffer(), 0, msg.getLength());
}
}
}
use of org.jgroups.Header in project JGroups by belaban.
the class ClassConfigurator method init.
protected static void init() throws Exception {
// make sure we have a class for DocumentBuilderFactory
Util.loadClass("javax.xml.parsers.DocumentBuilderFactory", ClassConfigurator.class);
String magic_number_file = null, protocol_id_file = null;
try {
// PropertyPermission not granted if running in an untrusted environment with JNLP
magic_number_file = Util.getProperty(new String[] { Global.MAGIC_NUMBER_FILE, "org.jgroups.conf.magicNumberFile" }, null, null, MAGIC_NUMBER_FILE);
protocol_id_file = Util.getProperty(new String[] { Global.PROTOCOL_ID_FILE, "org.jgroups.conf.protocolIDFile" }, null, null, PROTOCOL_ID_FILE);
} catch (SecurityException ex) {
}
// Read jg-magic-map.xml
List<Triple<Short, String, Boolean>> mapping = readMappings(magic_number_file);
for (Triple<Short, String, Boolean> tuple : mapping) {
short m = tuple.getVal1();
if (m >= MAX_MAGIC_VALUE)
throw new IllegalArgumentException("ID " + m + " is bigger than MAX_MAGIC_VALUE (" + MAX_MAGIC_VALUE + "); increase MAX_MAGIC_VALUE");
boolean external = tuple.getVal3();
if (external) {
if (magicMap[m] != null)
alreadyInMagicMap(m, tuple.getVal2());
continue;
}
Class clazz = Util.loadClass(tuple.getVal2(), ClassConfigurator.class);
if (magicMap[m] != null)
alreadyInMagicMap(m, clazz.getName());
if (Constructable.class.isAssignableFrom(clazz)) {
Constructable obj = (Constructable) clazz.newInstance();
magicMap[m] = obj.create();
} else {
Supplier<? extends Object> supplier = (Supplier<Object>) () -> {
try {
return clazz.newInstance();
} catch (Throwable throwable) {
return null;
}
};
magicMap[m] = supplier;
}
Object inst = magicMap[m].get();
if (inst == null)
continue;
// test to confirm that the Constructable impl returns an instance of the correct type
if (!inst.getClass().equals(clazz))
throw new IllegalStateException(String.format("%s.create() returned the wrong class: %s\n", clazz.getSimpleName(), inst.getClass().getSimpleName()));
// check that the IDs are the same
if (inst instanceof Header)
checkSameId((Header) inst, m);
classMap.put(clazz, m);
}
// Read jg-protocol-ids.xml
mapping = readMappings(protocol_id_file);
for (Triple<Short, String, Boolean> tuple : mapping) {
short m = tuple.getVal1();
boolean external = tuple.getVal3();
if (external) {
if (protocol_names.containsKey(m))
alreadyInProtocolsMap(m, tuple.getVal2());
continue;
}
Class clazz = Util.loadClass(tuple.getVal2(), ClassConfigurator.class);
if (protocol_ids.containsKey(clazz))
alreadyInProtocolsMap(m, clazz.getName());
protocol_ids.put(clazz, m);
protocol_names.put(m, clazz);
}
}
use of org.jgroups.Header in project JGroups by belaban.
the class MessageTest method testCopyHeaders.
public static void testCopyHeaders() {
Message m1 = new Message(null, "hello");
for (short id : new short[] { 1, 2, 10, Global.BLOCKS_START_ID, Global.BLOCKS_START_ID + 10 }) {
m1.putHeader(id, new DummyHeader(id));
}
System.out.println("Headers for m1: " + m1.printHeaders());
Message m2 = m1.copy(true, Global.BLOCKS_START_ID);
System.out.println("Headers for m2: " + m2.printHeaders());
Map<Short, Header> hdrs = m2.getHeaders();
assert hdrs.size() == 2;
assert hdrs.containsKey(Global.BLOCKS_START_ID);
short tmp = Global.BLOCKS_START_ID + 10;
assert hdrs.containsKey(tmp);
}
Aggregations