Beispiel #1
0
  /**
   * 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);
    }
  }
Beispiel #2
0
  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());
    }
  }