public static <T extends Streamable> T streamableFromByteBuffer(Class<? extends Streamable> cl,byte[] buffer,int offset,int length) throws Exception { if(buffer == null) return null; DataInput in=new ByteArrayDataInputStream(buffer,offset,length); T retval=(T)cl.newInstance(); retval.readFrom(in); return retval; }
@Deprecated public static <T extends Streamable> T streamableFromBuffer(Class<T> clazz,byte[] buffer,int offset,int length) throws Exception { DataInput in=new ByteArrayDataInputStream(buffer,offset,length); return Util.readStreamable(clazz, in); }
public static <T extends Streamable> T streamableFromByteBuffer(Supplier<T> factory, byte[] buffer, int offset, int length) throws Exception { if(buffer == null) return null; DataInput in=new ByteArrayDataInputStream(buffer,offset,length); T retval=factory.get(); retval.readFrom(in); return retval; }
public static Throwable exceptionFromBuffer(byte[] buf, int offset, int length) throws Exception { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset,length); return exceptionFromStream(in); }
public static <T extends Streamable> T streamableFromBuffer(Supplier<T> factory, byte[] buffer, int offset, int length) throws Exception { DataInput in=new ByteArrayDataInputStream(buffer,offset,length); return Util.readStreamable(factory, in); }
public static Message byteBufferToMessage(byte[] buffer,int offset,int length) throws Exception { DataInput in=new ByteArrayDataInputStream(buffer,offset,length); if(!in.readBoolean()) return null; Message msg=new Message(false); // don't create headers, readFrom() will do this msg.readFrom(in); return msg; }
protected Collection<? extends Address> readMembers(byte[] buffer, int offset, int length) { if(buffer == null) return null; try { DataInput in=new ByteArrayDataInputStream(buffer, offset, length); return Util.readAddresses(in, ArrayList::new); } catch(Exception ex) { log.error("%s: failed reading members from message: %s", local_addr, ex); return null; } }
protected ViewId readViewId(byte[] buffer, int offset, int length) { if(buffer == null) return null; try { DataInput in=new ByteArrayDataInputStream(buffer, offset, length); return Util.readViewId(in); } catch(Exception ex) { log.error("%s: failed reading ViewId from message: %s", local_addr, ex); return null; } }
protected static MethodCall methodCallFromBuffer(final byte[] buf, int offset, int length, Marshaller marshaller) throws Exception { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); MethodCall call=new MethodCall(); call.readFrom(in, marshaller); return call; }
protected static Object replyFromBuffer(final byte[] buf, int offset, int length, Marshaller marshaller) throws Exception { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); return marshaller != null? marshaller.objectFromStream(in) : Util.objectFromStream(in); }
protected Map<Address,IpAddress> unmarshal(byte[] buffer, int offset, int length) { if(buffer == null) return null; DataInput in=new ByteArrayDataInputStream(buffer, offset, length); HashMap<Address,IpAddress> addrs=null; try { int size=in.readInt(); if(size > 0) { addrs=new HashMap<>(size); for(int i=0; i < size; i++) { Address key=Util.readAddress(in); IpAddress val=Util.readStreamable(IpAddress::new, in); addrs.put(key, val); } } return addrs; } catch(Exception ex) { log.error("%s: failed reading addresses from message: %s", local_addr, ex); return null; } }
public void run() { // System.out.printf("[%s] reading from sock, conn: %s\n", Thread.currentThread().getName(), this); try { int len=in.readInt(); if(buffer == null || buffer.length < len) buffer=new byte[len]; in.readFully(buffer, 0, len); ByteArrayDataInputStream input=new ByteArrayDataInputStream(buffer, 0, len); Message msg=new Message(false); msg.readFrom(input); thread_pool.execute(() -> up_prot.up(msg)); } catch(IOException io_ex) { runner.stop(); throw new RuntimeException(io_ex); } catch(Exception ex) { if(sock.isClosed()) runner.stop(); throw new RuntimeException(ex); } }
protected static Message readMessage(byte[] buf, int offset, int length) throws Exception { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); short ver=in.readShort(); byte flags=in.readByte(); // final boolean multicast=(flags & (byte)2) == (byte)2; Message msg=new Message(false); // don't create headers, readFrom() will do this msg.readFrom(in); return msg; }
/** * Subclasses must call this method when a unicast or multicast message has been received. */ public void receive(Address sender, byte[] data, int offset, int length) { if(data == null) return; // drop message from self; it has already been looped back up (https://issues.jboss.org/browse/JGRP-1765) if(Objects.equals(local_physical_addr, sender)) return; // the length of a message needs to be at least 3 bytes: version (2) and flags (1) // JGRP-2210 if(length < Global.SHORT_SIZE + Global.BYTE_SIZE) return; short version=Bits.readShort(data, offset); if(!versionMatch(version, sender)) return; offset+=Global.SHORT_SIZE; byte flags=data[offset]; offset+=Global.BYTE_SIZE; boolean is_message_list=(flags & LIST) == LIST, multicast=(flags & MULTICAST) == MULTICAST; ByteArrayDataInputStream in=new ByteArrayDataInputStream(data, offset, length); if(is_message_list) // used if message bundling is enabled handleMessageBatch(in, multicast); else handleSingleMessage(in, multicast); }
protected Tuple<Collection<? extends Address>,Digest> readParticipantsAndDigest(byte[] buffer, int offset, int length) { if(buffer == null) return null; try { DataInput in=new ByteArrayDataInputStream(buffer, offset, length); Collection<Address> participants=Util.readAddresses(in, ArrayList::new); Digest digest=Util.readStreamable(Digest::new, in); return new Tuple<>(participants, digest); } catch(Exception ex) { log.error("%s: failed reading particpants and digest from message: %s", localAddress, ex); return null; } }
public void run() { final byte[] receive_buf=new byte[65535]; DatagramPacket packet=new DatagramPacket(receive_buf, receive_buf.length); DataInput inp; while(sock != null && receiver != null && Thread.currentThread().equals(receiver)) { packet.setData(receive_buf, 0, receive_buf.length); try { sock.receive(packet); inp=new ByteArrayDataInputStream(packet.getData(), packet.getOffset(), packet.getLength()); Message msg=new Message(); msg.readFrom(inp); up(msg); } catch(SocketException socketEx) { break; } catch(Throwable ex) { log.error(Util.getMessage("FailedReceivingPacketFrom"), packet.getSocketAddress(), ex); } } if(log.isTraceEnabled()) log.trace("receiver thread terminated"); }
public void run() { final byte[] receive_buf=new byte[65535]; DatagramPacket packet=new DatagramPacket(receive_buf, receive_buf.length); while(mcast_sock != null && receiver != null && Thread.currentThread().equals(receiver)) { packet.setData(receive_buf, 0, receive_buf.length); try { mcast_sock.receive(packet); DataInput inp=new ByteArrayDataInputStream(packet.getData(), packet.getOffset(), packet.getLength()); Message msg=new Message(); msg.readFrom(inp); if(!Objects.equals(local_addr,msg.getSrc())) // discard discovery request from self up(msg); } catch(SocketException socketEx) { break; } catch(Throwable ex) { log.error(Util.getMessage("FailedReceivingPacketFrom"), packet.getSocketAddress(), ex); } } log.debug("receiver thread terminated"); }
@Test public void testSeriazilabeToAndFromByteArray() throws IOException { Map<String, String> attributesMap = new HashMap<>(); attributesMap.put(REGION_NAME, "myRegion"); attributesMap.put(TABLE_NAME, "myTable"); attributesMap.put(PDX_NAME, "myPdx"); attributesMap.put(DATA_SOURCE_NAME, "myDatasource"); attributesMap.put(SYNCHRONOUS_NAME, "false"); attributesMap.put(ID_NAME, "myId"); DescribeMappingResult result = new DescribeMappingResult(attributesMap); DescribeMappingResult newResult = new DescribeMappingResult(); ByteArrayDataOutputStream output = new ByteArrayDataOutputStream(); ByteArrayDataInputStream input; result.toData(output); input = new ByteArrayDataInputStream(output.buffer()); newResult.fromData(input); Map<String, String> newAttributeMap = newResult.getAttributeMap(); assertThat(attributesMap).isEqualTo(newAttributeMap); }
public static Tuple<View,Digest> _readViewAndDigest(byte[] buffer, int offset, int length) throws Exception { if(buffer == null) return null; DataInput in=new ByteArrayDataInputStream(buffer, offset, length); View tmp_view=null; Digest digest=null; short flags=in.readShort(); if((flags & VIEW_PRESENT) == VIEW_PRESENT) { tmp_view=(flags & MERGE_VIEW) == MERGE_VIEW? new MergeView() : (flags & DELTA_VIEW) == DELTA_VIEW? new DeltaView() : new View(); tmp_view.readFrom(in); } if((flags & DIGEST_PRESENT) == DIGEST_PRESENT) { if((flags & READ_ADDRS) == READ_ADDRS) { digest=new Digest(); digest.readFrom(in); } else { digest=new Digest(tmp_view.getMembersRaw()); digest.readFrom(in,false); } } return new Tuple<>(tmp_view, digest); }
@Override public void receive(Address sender, byte[] buf, int offset, int length) { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); GossipData data=new GossipData(); try { data.readFrom(in); switch(data.getType()) { case MESSAGE: case SUSPECT: if(receiver != null) receiver.receive(data); break; case GET_MBRS_RSP: notifyResponse(data.getGroup(), data.getPingData()); break; } } catch(Exception ex) { log.error(Util.getMessage("FailedReadingData"), ex); } }