示例#1
0
 protected static void writeMessage(Message msg, DataOutput dos, boolean multicast)
     throws Exception {
   byte flags = 0;
   dos.writeShort(Version.version); // write the version
   if (multicast) flags += MULTICAST;
   dos.writeByte(flags);
   msg.writeTo(dos);
 }
示例#2
0
 protected Buffer writeMessage(final Message msg) throws Exception {
   out_stream.reset();
   dos.reset();
   byte flags = 0;
   dos.writeShort(Version.version); // write the version
   if (msg.getDest() == null) flags += (byte) 2;
   dos.writeByte(flags);
   msg.writeTo(dos);
   return new Buffer(out_stream.getRawBuffer(), 0, out_stream.size());
 }
示例#3
0
  void sendMessages() throws Exception {
    if (num_threads > 1 && num_msgs % num_threads != 0) {
      System.err.println(
          "num_msgs (" + num_msgs + " ) has to be divisible by num_threads (" + num_threads + ")");
      return;
    }

    System.out.println(
        "sending "
            + num_msgs
            + " messages ("
            + Util.printBytes(msg_size)
            + ") to "
            + remote
            + ": oob="
            + oob
            + ", "
            + num_threads
            + " sender thread(s)");
    ByteBuffer buf =
        ByteBuffer.allocate(Global.BYTE_SIZE + Global.LONG_SIZE).put(START).putLong(num_msgs);
    Message msg = new Message(null, buf.array());
    // msg.writeTo(output);

    ExposedByteArrayOutputStream out_stream = new ExposedByteArrayOutputStream((int) (msg.size()));
    ExposedDataOutputStream dos = new ExposedDataOutputStream(out_stream);
    byte flags = 0;
    dos.writeShort(Version.version); // write the version
    if (msg.getDest() == null) flags += (byte) 2;
    dos.writeByte(flags);
    msg.writeTo(dos);
    Buffer buffer = new Buffer(out_stream.getRawBuffer(), 0, out_stream.size());

    output_lock.lock(); // need to sync if we have more than 1 sender
    try {
      // msg.writeTo(output);
      output.writeInt(buffer.getLength());
      output.write(buffer.getBuf(), buffer.getOffset(), buffer.getLength());
    } finally {
      output_lock.unlock();
    }

    int msgs_per_sender = num_msgs / num_threads;
    Sender[] senders = new Sender[num_threads];
    for (int i = 0; i < senders.length; i++)
      senders[i] = new Sender(msgs_per_sender, msg_size, num_msgs / 10);
    for (Sender sender : senders) sender.start();
    for (Sender sender : senders) sender.join();
    output.flush();
    System.out.println("done sending " + num_msgs + " to " + remote);
  }
示例#4
0
 @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);
   }
 }
示例#5
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);
    }
  }
示例#6
0
  void sendMcastDiscoveryRequest(Message msg) {
    Buffer buf;
    DatagramPacket packet;
    DataOutputStream out = null;

    try {
      if (msg.getSrc() == null) msg.setSrc(local_addr);
      ExposedByteArrayOutputStream out_stream = new ExposedByteArrayOutputStream(128);
      out = new DataOutputStream(out_stream);
      msg.writeTo(out);
      out.flush(); // flushes contents to out_stream
      buf = new Buffer(out_stream.getRawBuffer(), 0, out_stream.size());
      packet =
          new DatagramPacket(
              buf.getBuf(), buf.getOffset(), buf.getLength(), mcast_addr, mcast_port);
      discovery_reception.reset();
      if (mcast_send_sockets != null) {
        MulticastSocket s;
        for (int i = 0; i < mcast_send_sockets.length; i++) {
          s = mcast_send_sockets[i];
          try {
            s.send(packet);
          } catch (Exception e) {
            log.error("failed sending packet on socket " + s);
          }
        }
      } else { // DEFAULT path
        if (mcast_sock != null) mcast_sock.send(packet);
      }
      waitForDiscoveryRequestReception();
    } catch (IOException ex) {
      log.error("failed sending discovery request", ex);
    } finally {
      Util.close(out);
    }
  }