private static Buffer marshal(Message msg) throws Exception { ByteArrayDataOutputStream dos = new ByteArrayDataOutputStream((int) (msg.size() + 50)); Address dest = msg.getDest(); boolean multicast = dest == null; writeMessage(msg, dos, multicast); return dos.getBuffer(); }
@Override protected void sendMcastDiscoveryRequest(Message msg) { try { if (msg.getSrc() == null) msg.setSrc(local_addr); ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(128); msg.writeTo(out); for (int i = bind_port; i <= bind_port + port_range; i++) { DatagramPacket packet = new DatagramPacket(out.buffer(), 0, out.position(), dest_addr, i); sock.send(packet); } } catch (Exception ex) { log.error(Util.getMessage("FailedSendingDiscoveryRequest"), ex); } }
protected void getStateFrom( JChannel channel, Protocol prot, String stack, String ch, DataOutputStream out) throws Exception { ByteArrayDataOutputStream output = new ByteArrayDataOutputStream(1024); OutputStreamAdapter out_ad = new OutputStreamAdapter(output); Event evt = new Event(Event.STATE_TRANSFER_OUTPUTSTREAM, out_ad); if (channel != null) channel.up(evt); else prot.up(evt); int len = output.position(); if (len > 0) { Bits.writeString(stack, out); Bits.writeString(ch, out); out.writeInt(len); out.write(output.buffer(), 0, len); log.trace("%s: fetched %d bytes from %s:%s", local_addr, len, stack, ch); } }
/** * Send all fragments as separate messages (with same ID !). Example: * * <pre> * Given the generated ID is 2344, number of fragments=3, message {dst,src,buf} * would be fragmented into: * <p/> * [2344,3,0]{dst,src,buf1}, * [2344,3,1]{dst,src,buf2} and * [2344,3,2]{dst,src,buf3} * </pre> */ private void fragment(Message msg, long size) { Address dest = msg.getDest(), src = msg.getSrc(); long frag_id = curr_id.getAndIncrement(); // used as seqnos int num_frags; try { // write message into a byte buffer and fragment it ByteArrayDataOutputStream dos = new ByteArrayDataOutputStream((int) (size + 50)); msg.writeTo(dos); byte[] buffer = dos.buffer(); byte[][] fragments = Util.fragmentBuffer(buffer, frag_size, dos.position()); num_frags = fragments.length; num_sent_frags += num_frags; if (log.isTraceEnabled()) { StringBuilder sb = new StringBuilder(); sb.append("fragmenting packet to ") .append(dest != null ? dest.toString() : "<all members>") .append(" (size=") .append(buffer.length) .append(") into ") .append(num_frags) .append(" fragment(s) [frag_size=") .append(frag_size) .append(']'); log.trace(sb.toString()); } for (int i = 0; i < num_frags; i++) { Message frag_msg = new Message(dest, src, fragments[i]); FragHeader hdr = new FragHeader(frag_id, i, num_frags); frag_msg.putHeader(this.id, hdr); Event evt = new Event(Event.MSG, frag_msg); down_prot.down(evt); } } catch (Exception e) { log.error(Util.getMessage("ExceptionOccurredTryingToFragmentMessage"), e); } }
public static void testWriteAndReadStreamableArray() throws Exception { Message[] msgs = { new Message(null, "hello world").setFlag(Message.Flag.OOB, Message.Flag.NO_RELIABILITY), new Message(Util.createRandomAddress("dest"), "bela ban"), new Message( Util.createRandomAddress("dest"), Util.createRandomAddress("src"), "hello world again") .setTransientFlag(Message.TransientFlag.DONT_LOOPBACK) }; ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(256); Util.write(msgs, out); ByteArrayDataInputStream in = new ByteArrayDataInputStream(out.buffer(), 0, out.position()); Message[] tmp = Util.read(Message.class, in); for (int i = 0; i < msgs.length; i++) { if (msgs[i].dest() == null) assert tmp[i].dest() == null; else assert (msgs[i].dest().equals(tmp[i].dest())); assert msgs[i].getLength() == tmp[i].getLength(); assert msgs[i].getObject().equals(tmp[i].getObject()); } }