/** * <em> Note that the byte[] buffer passed as argument must not be modified. Reason: if we * retransmit the message, it would still have a ref to the original byte[] buffer passed in as * argument, and so we would retransmit a changed byte[] buffer ! </em> */ public final void setBuffer(Buffer buf) { if (buf != null) { this.buf = buf.getBuf(); this.offset = buf.getOffset(); this.length = buf.getLength(); } }
private static void _testMessage(Message msg) throws Exception { Buffer buf = Util.messageToByteBuffer(msg); Message msg2 = Util.byteBufferToMessage(buf.getBuf(), buf.getOffset(), buf.getLength()); Assert.assertEquals(msg.getSrc(), msg2.getSrc()); Assert.assertEquals(msg.getDest(), msg2.getDest()); Assert.assertEquals(msg.getLength(), msg2.getLength()); }
public void run() { Throwable t = null; while (canRun()) { Buffer data = null; try { data = send_queue.take(); if (data.hashCode() == termination.hashCode()) break; } catch (InterruptedException e) { t = e; break; } if (data != null) { try { _send(data.getBuf(), 0, data.getLength(), false, send_queue.isEmpty()); } catch (Throwable ignored) { t = ignored; } } } server.notifyConnectionClosed( TcpConnection.this, String.format( "%s: %s", getClass().getSimpleName(), t != null ? t.toString() : "normal stop")); }
private void encryptAndSend(Message msg) throws Exception { EncryptHeader hdr = new EncryptHeader(EncryptHeader.ENCRYPT, getSymVersion()); if (this.encrypt_entire_message) hdr.type |= EncryptHeader.ENCRYPT_ENTIRE_MSG; if (encrypt_entire_message) { if (msg.getSrc() == null) msg.setSrc(local_addr); Buffer serialized_msg = Util.streamableToBuffer(msg); byte[] encrypted_msg = code( serialized_msg.getBuf(), serialized_msg.getOffset(), serialized_msg.getLength(), false); // exclude existing headers, they will be seen again when we decrypt and unmarshal the msg at // the receiver Message tmp = msg.copy(false, false).setBuffer(encrypted_msg).putHeader(this.id, hdr); down_prot.down(new Event(Event.MSG, tmp)); return; } // copy neeeded because same message (object) may be retransmitted -> no double encryption Message msgEncrypted = msg.copy(false) .putHeader(this.id, hdr) .setBuffer(code(msg.getRawBuffer(), msg.getOffset(), msg.getLength(), false)); down_prot.down(new Event(Event.MSG, msgEncrypted)); }
public void run() { for (int i = 1; i <= number_of_msgs; i++) { try { Message msg = new Message(null, buf); if (oob) msg.setFlag(Message.Flag.OOB); if (dont_bundle) msg.setFlag(Message.Flag.DONT_BUNDLE); if (i > 0 && do_print > 0 && i % do_print == 0) System.out.println("-- sent " + i); Buffer buffer = writeMessage(msg); 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()); // output.flush(); } finally { output_lock.unlock(); } } catch (Exception e) { e.printStackTrace(); } } }
/** * <em> Note that the byte[] buffer passed as argument must not be modified. Reason: if we * retransmit the message, it would still have a ref to the original byte[] buffer passed in as * argument, and so we would retransmit a changed byte[] buffer ! </em> */ public final Message setBuffer(Buffer buf) { if (buf != null) { this.buf = buf.getBuf(); this.offset = buf.getOffset(); this.length = buf.getLength(); } return this; }
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); }
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); } }