org.jgroups.Message.getLength()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(207)

本文整理了Java中org.jgroups.Message.getLength()方法的一些代码示例,展示了Message.getLength()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.getLength()方法的具体详情如下:
包路径:org.jgroups.Message
类名称:Message
方法名:getLength

Message.getLength介绍

[英]Returns the number of bytes in the buffer
[中]返回缓冲区中的字节数

代码示例

代码示例来源:origin: wildfly/wildfly

  1. protected static String printPayload(Message msg, final StringBuilder sb) {
  2. byte[] payload=msg.getRawBuffer();
  3. int print_max=Math.min(msg.getLength(), 50);
  4. for(int i=msg.getOffset(); i < print_max; i++) {
  5. byte ch=payload[i];
  6. sb.append((char)ch);
  7. }
  8. return null;
  9. }
  10. }

代码示例来源:origin: org.jboss.cache/jbosscache-core

  1. /**
  2. * Message contains a Command. Execute it against *this* object and return result.
  3. */
  4. @Override
  5. public Object handle(Message req)
  6. {
  7. if (isValid(req))
  8. {
  9. try
  10. {
  11. ReplicableCommand command = (ReplicableCommand) req_marshaller.objectFromByteBuffer(req.getBuffer(), req.getOffset(), req.getLength());
  12. Object execResult = executeCommand(command, req);
  13. if (log.isTraceEnabled()) log.trace("Command : " + command + " executed, result is: " + execResult);
  14. return execResult;
  15. }
  16. catch (Throwable x)
  17. {
  18. if (trace) log.trace("Problems invoking command.", x);
  19. return x;
  20. }
  21. }
  22. else
  23. {
  24. return null;
  25. }
  26. }

代码示例来源:origin: wildfly/wildfly

  1. protected Message _decrypt(final Cipher cipher, Message msg) throws Exception {
  2. if(msg.getLength() == 0)
  3. return msg;
  4. byte[] decrypted_msg;
  5. if(cipher == null)
  6. decrypted_msg=code(msg.getRawBuffer(), msg.getOffset(), msg.getLength(), true);
  7. else
  8. try {
  9. decrypted_msg=cipher.doFinal(msg.getRawBuffer(), msg.getOffset(), msg.getLength());
  10. }
  11. catch(BadPaddingException | IllegalBlockSizeException e) {
  12. // if any exception is thrown, this cipher object may need to be reset before it can be used again.
  13. cipher.init(Cipher.DECRYPT_MODE, secret_key);
  14. throw e;
  15. }
  16. return msg.setBuffer(decrypted_msg);
  17. }

代码示例来源:origin: wildfly/wildfly

  1. public void receive(Message msg) {
  2. try {
  3. TotOrderRequest req=new TotOrderRequest();
  4. ByteBuffer buf=ByteBuffer.wrap(msg.getRawBuffer(), msg.getOffset(), msg.getLength());
  5. req.init(buf);
  6. processRequest(req);
  7. }
  8. catch(Exception e) {
  9. System.err.println(e);
  10. }
  11. }

代码示例来源:origin: wildfly/wildfly

  1. public void receive(Message msg) {
  2. byte[] buf=msg.getRawBuffer();
  3. byte type=buf[msg.getOffset()];
  4. switch(type) {
  5. case START:
  6. ByteBuffer tmp=ByteBuffer.wrap(buf, 1+msg.getOffset(), Global.LONG_SIZE);
  7. num_msgs=(int)tmp.getLong();
  8. print=num_msgs / 10;
  9. current_value.set(0);
  10. total_bytes.set(0);
  11. start=System.currentTimeMillis();
  12. break;
  13. case DATA:
  14. long new_val=current_value.incrementAndGet();
  15. total_bytes.addAndGet(msg.getLength() - Global.INT_SIZE);
  16. if(print > 0 && new_val % print == 0)
  17. System.out.println("received " + new_val);
  18. if(new_val >= num_msgs) {
  19. long time=System.currentTimeMillis() - start;
  20. double msgs_sec=(current_value.get() / (time / 1000.0));
  21. double throughput=total_bytes.get() / (time / 1000.0);
  22. System.out.println(String.format("\nreceived %d messages in %d ms (%.2f msgs/sec), throughput=%s",
  23. current_value.get(), time, msgs_sec, Util.printBytes(throughput)));
  24. break;
  25. }
  26. break;
  27. default:
  28. System.err.println("Type " + type + " is invalid");
  29. }
  30. }

代码示例来源:origin: wildfly/wildfly

  1. /**
  2. * Assembles all the fragments into one buffer. Takes all Messages, and combines their buffers into one
  3. * buffer.
  4. * This method does not check if the fragmentation is complete (use {@link #isComplete()} to verify
  5. * before calling this method)
  6. * @return the complete message in one buffer
  7. *
  8. */
  9. protected Message assembleMessage() {
  10. Message retval;
  11. byte[] combined_buffer, tmp;
  12. int combined_length=0, length, offset;
  13. int index=0;
  14. for(Message fragment: fragments)
  15. combined_length+=fragment.getLength();
  16. combined_buffer=new byte[combined_length];
  17. retval=fragments[0].copy(false); // doesn't copy the payload, but copies the headers
  18. for(int i=0; i < fragments.length; i++) {
  19. Message fragment=fragments[i];
  20. fragments[i]=null; // help garbage collection a bit
  21. tmp=fragment.getRawBuffer();
  22. length=fragment.getLength();
  23. offset=fragment.getOffset();
  24. System.arraycopy(tmp, offset, combined_buffer, index, length);
  25. index+=length;
  26. }
  27. retval.setBuffer(combined_buffer);
  28. return retval;
  29. }

代码示例来源:origin: wildfly/wildfly

  1. public void receive(Message msg) {
  2. byte[] buf=msg.getRawBuffer();
  3. byte type=buf[msg.getOffset()];
  4. switch(type) {
  5. case START:
  6. ByteBuffer tmp=ByteBuffer.wrap(buf, 1+msg.getOffset(), Global.LONG_SIZE);
  7. num_msgs=(int)tmp.getLong();
  8. print=num_msgs / 10;
  9. current_value.set(0);
  10. total_bytes.set(0);
  11. start=System.currentTimeMillis();
  12. break;
  13. case DATA:
  14. long new_val=current_value.incrementAndGet();
  15. total_bytes.addAndGet(msg.getLength() - Global.INT_SIZE);
  16. if(print > 0 && new_val % print == 0)
  17. System.out.println("received " + new_val);
  18. if(new_val >= num_msgs) {
  19. long time=System.currentTimeMillis() - start;
  20. double msgs_sec=(current_value.get() / (time / 1000.0));
  21. double throughput=total_bytes.get() / (time / 1000.0);
  22. System.out.println(String.format("\nreceived %d messages in %d ms (%.2f msgs/sec), throughput=%s",
  23. current_value.get(), time, msgs_sec, Util.printBytes(throughput)));
  24. break;
  25. }
  26. break;
  27. default:
  28. System.err.println("Type " + type + " is invalid");
  29. }
  30. }

代码示例来源:origin: wildfly/wildfly

  1. public void receive(Message msg) {
  2. byte[] buf=msg.getRawBuffer();
  3. byte type=buf[msg.getOffset()];
  4. switch(type) {
  5. case START:
  6. ByteBuffer tmp=ByteBuffer.wrap(buf, 1+msg.getOffset(), Global.LONG_SIZE);
  7. num_msgs=(int)tmp.getLong();
  8. print=num_msgs / 10;
  9. current_value.set(0);
  10. total_bytes.set(0);
  11. start=System.currentTimeMillis();
  12. break;
  13. case DATA:
  14. long new_val=current_value.incrementAndGet();
  15. total_bytes.addAndGet(msg.getLength() - Global.INT_SIZE);
  16. if(print > 0 && new_val % print == 0)
  17. System.out.println("received " + new_val);
  18. if(new_val >= num_msgs) {
  19. long time=System.currentTimeMillis() - start;
  20. double msgs_sec=(current_value.get() / (time / 1000.0));
  21. double throughput=total_bytes.get() / (time / 1000.0);
  22. System.out.println(String.format("\nreceived %d messages in %d ms (%.2f msgs/sec), throughput=%s",
  23. current_value.get(), time, msgs_sec, Util.printBytes(throughput)));
  24. break;
  25. }
  26. break;
  27. default:
  28. System.err.println("Type " + type + " is invalid");
  29. }
  30. }

代码示例来源:origin: wildfly/wildfly

  1. public void receive(Message msg) {
  2. if(receiver == null)
  3. return;
  4. try {
  5. receiver.receive(msg.src(), msg.getRawBuffer(), msg.getOffset(), msg.getLength());
  6. }
  7. catch(Throwable t) {
  8. log.error("failed delivering message", t);
  9. }
  10. }

代码示例来源:origin: wildfly/wildfly

  1. protected void encryptAndSend(Message msg) throws Exception {
  2. EncryptHeader hdr=new EncryptHeader(EncryptHeader.ENCRYPT, symVersion());
  3. // copy neeeded because same message (object) may be retransmitted -> prevent double encryption
  4. Message msgEncrypted=msg.copy(false).putHeader(this.id, hdr);
  5. if(msg.getLength() > 0)
  6. msgEncrypted.setBuffer(code(msg.getRawBuffer(),msg.getOffset(),msg.getLength(),false));
  7. else { // length is 0
  8. byte[] payload=msg.getRawBuffer();
  9. if(payload != null) // we don't encrypt empty buffers (https://issues.jboss.org/browse/JGRP-2153)
  10. msgEncrypted.setBuffer(payload, msg.getOffset(), msg.getLength());
  11. }
  12. down_prot.down(msgEncrypted);
  13. }

代码示例来源:origin: wildfly/wildfly

  1. public void receive(Message msg) {
  2. if(msg == null || msg.getLength() == 0) {
  3. log.error("DrawApplet.run(): msg or msg.buffer is null !");
  4. return;
  5. }
  6. instream=new DataInputStream(new ByteArrayInputStream(msg.getRawBuffer(), msg.getOffset(), msg.getLength()));
  7. int r=0;
  8. try {
  9. r=instream.readInt();
  10. if(r == -13) {
  11. clearPanel();
  12. return;
  13. }
  14. int g=instream.readInt();
  15. int b=instream.readInt();
  16. int my_x=instream.readInt();
  17. int my_y=instream.readInt();
  18. if(graphics != null) {
  19. graphics.setColor(new Color(r, g, b));
  20. graphics.fillOval(my_x, my_y, 10, 10);
  21. graphics.setColor(default_color);
  22. }
  23. }
  24. catch(Exception ex) {
  25. ex.printStackTrace();
  26. }
  27. }
  28. });

代码示例来源:origin: wildfly/wildfly

  1. public void receive(MessageBatch batch) {
  2. if(receiver == null)
  3. return;
  4. for(Message msg: batch) {
  5. try {
  6. receiver.receive(msg.src(), msg.getRawBuffer(), msg.getOffset(), msg.getLength());
  7. }
  8. catch(Throwable t) {
  9. log.error("failed delivering message from batch", t);
  10. }
  11. }
  12. }

代码示例来源:origin: wildfly/wildfly

  1. protected Object handleUpEvent(Message msg, RelayHeader hdr) {
  2. switch(hdr.type) {
  3. case DISSEMINATE:
  4. Message copy=msg.copy();
  5. if(hdr.original_sender != null)
  6. copy.setSrc(hdr.original_sender);
  7. return up_prot.up(copy);
  8. case FORWARD:
  9. if(is_coord)
  10. forward(msg.getRawBuffer(), msg.getOffset(), msg.getLength());
  11. break;
  12. case VIEW:
  13. return installView(msg.getRawBuffer(), msg.getOffset(), msg.getLength());
  14. case BROADCAST_VIEW:
  15. break;
  16. default:
  17. throw new IllegalArgumentException(hdr.type + " is not a valid type");
  18. }
  19. return null;
  20. }

代码示例来源:origin: wildfly/wildfly

  1. private Callable<Object> read(Message message) throws Exception {
  2. try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(message.getRawBuffer(), message.getOffset(), message.getLength()))) {
  3. int version = IndexSerializer.VARIABLE.readInt(input);
  4. try (Unmarshaller unmarshaller = this.marshallingContext.createUnmarshaller(version)) {
  5. unmarshaller.start(Marshalling.createByteInput(input));
  6. Object clientId = unmarshaller.readObject();
  7. Optional<Object> context = this.contexts.get(clientId);
  8. if (context == null) return () -> NoSuchService.INSTANCE;
  9. @SuppressWarnings("unchecked")
  10. Command<Object, Object> command = (Command<Object, Object>) unmarshaller.readObject();
  11. // Wrap execution result in an Optional, since command execution might return null
  12. ExceptionSupplier<Optional<Object>, Exception> task = () -> Optional.ofNullable(command.execute(context.orElse(null)));
  13. return () -> this.executor.execute(task).orElse(Optional.of(NoSuchService.INSTANCE)).orElse(null);
  14. }
  15. }
  16. }

代码示例来源:origin: wildfly/wildfly

  1. /** Returns a new message as a result of uncompressing msg, or null if msg couldn't be uncompressed */
  2. protected Message uncompress(Message msg, int original_size) {
  3. byte[] compressed_payload=msg.getRawBuffer();
  4. if(compressed_payload != null && compressed_payload.length > 0) {
  5. byte[] uncompressed_payload=new byte[original_size];
  6. Inflater inflater=null;
  7. try {
  8. inflater=inflater_pool.take();
  9. inflater.reset();
  10. inflater.setInput(compressed_payload, msg.getOffset(), msg.getLength());
  11. try {
  12. inflater.inflate(uncompressed_payload);
  13. // we need to copy: https://jira.jboss.org/jira/browse/JGRP-867
  14. return msg.copy(false).setBuffer(uncompressed_payload);
  15. }
  16. catch(DataFormatException e) {
  17. log.error(Util.getMessage("CompressionFailure"), e);
  18. }
  19. }
  20. catch(InterruptedException e) {
  21. Thread.currentThread().interrupt(); // set the interrupt bit again, so caller can handle it
  22. }
  23. finally {
  24. if(inflater != null)
  25. inflater_pool.offer(inflater);
  26. }
  27. }
  28. return null;
  29. }

代码示例来源:origin: wildfly/wildfly

  1. /** Adds a fragment to the full message */
  2. public Message set(Message frag_msg, Frag3Header hdr) {
  3. lock.lock();
  4. try {
  5. if(buffer == null)
  6. buffer=new byte[hdr.original_length];
  7. if(hdr.frag_id == 0) {
  8. // the first fragment creates the message, copy the headers but not the buffer
  9. msg=frag_msg.copy(false);
  10. }
  11. if(received.set(hdr.frag_id)) {
  12. // if not yet added: copy the fragment's buffer into msg.buffer at the correct offset
  13. int frag_length=frag_msg.getLength();
  14. int offset=hdr.offset;
  15. System.arraycopy(frag_msg.getRawBuffer(), frag_msg.getOffset(), buffer, offset, frag_length);
  16. if(isComplete())
  17. return assembleMessage();
  18. }
  19. return null;
  20. }
  21. finally {
  22. lock.unlock();
  23. }
  24. }

代码示例来源:origin: wildfly/wildfly

  1. protected void dispatch(final Message msg, final Header hdr) {
  2. switch(hdr.type) {
  3. case Header.REQ:
  4. handleRequest(msg, hdr);
  5. break;
  6. case Header.RSP:
  7. case Header.EXC_RSP:
  8. Request req=requests.get(hdr.req_id);
  9. if(req != null)
  10. handleResponse(req, msg.src(), msg.getRawBuffer(), msg.getOffset(), msg.getLength(), hdr.type == Header.EXC_RSP);
  11. break;
  12. default:
  13. log.error(Util.getMessage("HeaderSTypeIsNeitherREQNorRSP"));
  14. break;
  15. }
  16. }

代码示例来源:origin: wildfly/wildfly

  1. HashMap<String, String> hdrs=new HashMap<>();
  2. hdrs.put("sender", msg.getSrc().toString());
  3. sendToClients(hdrs, msg.getRawBuffer(), msg.getOffset(), msg.getLength());
  4. sendToClients(hdr.headers, msg.getRawBuffer(), msg.getOffset(), msg.getLength());
  5. break;
  6. case ENDPOINT:

代码示例来源:origin: wildfly/wildfly

  1. public Object up(Message msg) {
  2. StableHeader hdr=msg.getHeader(this.id);
  3. if(hdr == null) {
  4. handleRegularMessage(msg);
  5. return up_prot.up(msg);
  6. }
  7. handleUpEvent(hdr, msg.getSrc(), readDigest(msg.getRawBuffer(), msg.getOffset(), msg.getLength()));
  8. return null; // don't pass STABLE or STABILITY messages up the stack
  9. }

代码示例来源:origin: wildfly/wildfly

  1. public void receive(Message msg) {
  2. byte[] buf=msg.getRawBuffer();
  3. if(buf == null) {
  4. System.err.printf("%s: received null buffer from %s, headers: %s\n", channel.getAddress(), msg.src(), msg.printHeaders());
  5. return;
  6. }
  7. try {
  8. DrawCommand comm=Util.streamableFromByteBuffer(DrawCommand::new, buf, msg.getOffset(), msg.getLength());
  9. switch(comm.mode) {
  10. case DrawCommand.DRAW:
  11. if(panel != null)
  12. panel.drawPoint(comm);
  13. break;
  14. case DrawCommand.CLEAR:
  15. clearPanel();
  16. break;
  17. default:
  18. System.err.println("***** received invalid draw command " + comm.mode);
  19. break;
  20. }
  21. }
  22. catch(Exception e) {
  23. e.printStackTrace();
  24. }
  25. }

相关文章