Package tahrir.tools.ByteArraySegment

Examples of tahrir.tools.ByteArraySegment.ByteArraySegmentBuilder


        rng.nextBytes(iv);
      }
      final Cipher cipher = Cipher.getInstance(CIPHER_NAME);
      cipher.init(Cipher.ENCRYPT_MODE, skey, new IvParameterSpec(iv));
      final byte[] ciphertext = cipher.doFinal(toEncrypt.array, toEncrypt.offset, toEncrypt.length);
      final ByteArraySegmentBuilder basb = new ByteArraySegmentBuilder();
      basb.write(iv);
      basb.write(ciphertext);
      return basb.build();
    } catch (final Exception e) {
      throw new RuntimeException(e);
    }
  }
View Full Code Here


    ackReceived = new Called();
  }

  @Test
  public void simpleReliableMessageSend() throws Exception {
    final ByteArraySegmentBuilder msgBuilder = ByteArraySegment.builder();

    for (int x = 0; x < 100; x++) {
      msgBuilder.writeByte(33);
    }

    sentMessage = msgBuilder.build();

    one2two.send(sentMessage, 1, new TrSentReceivedListenerBasicImpl());

    for (int x = 0; x < 10; x++) {
      if (ackReceived.called && receivedSuccessfully.called) {
View Full Code Here

    Assert.assertTrue(receivedSuccessfully.called);
  }

  @Test
  public void simpleReliableUnilateralMessageSend() throws Exception {
    final ByteArraySegmentBuilder msgBuilder = ByteArraySegment.builder();

    for (int x = 0; x < 100; x++) {
      msgBuilder.writeByte(33);
    }

    sentMessage = msgBuilder.build();

    i2.allowUnsolicitedInbound(listener);

    one2two.send(sentMessage, 1, new TrSentReceivedListenerBasicImpl());
View Full Code Here

    Assert.assertTrue(receivedSuccessfully.called);
  }

  @Test
  public void longReliableMessageSend() throws Exception {
    final ByteArraySegmentBuilder msgBuilder = ByteArraySegment.builder();

    for (int x = 0; x < 2000; x++) {
      msgBuilder.writeByte(33);
    }

    sentMessage = msgBuilder.build();

    //    for (int x = 0; x < 100; x++) {
    //      if (connected1.called && connected2.called) {
    //        break;
    //      }
View Full Code Here

    //TODO: This seems to be working fine on some systems, but not on travis. Fix it.
  @Test(enabled = false)
  public void unreliableSimpleMessageSend() throws Exception {
    i1.setSimPercentageLoss(.2);

    final ByteArraySegmentBuilder msgBuilder = ByteArraySegment.builder();

    for (int x = 0; x < 10; x++) {
      msgBuilder.writeByte(33);
    }

    sentMessage = msgBuilder.build();

    one2two.send(sentMessage, 1, new TrSentReceivedListenerBasicImpl());

    for (int x = 0; x < 10; x++) {
      if (ackReceived.called && receivedSuccessfully.called) {
View Full Code Here

  @Test
  public void longUnreliableMessageSend() throws Exception {
    i1.setSimPercentageLoss(.2);

    final ByteArraySegmentBuilder msgBuilder = ByteArraySegment.builder();

    for (int x = 0; x < 2000; x++) {
      msgBuilder.writeByte(33);
    }

    sentMessage = msgBuilder.build();

    one2two.send(sentMessage, 1, new TrSentReceivedListenerBasicImpl());

    for (int x = 0; x < 1000; x++) {
      if (ackReceived.called && receivedSuccessfully.called) {
View Full Code Here

    estimatedPacketSize += message.length;
    if (estimatedPacketSize > TrConstants.MAX_UDP_PACKET_SIZE) {
      sendLongMessage(message, priority, sentListener);
    } else {
      // logger.debug("Sending short message");
      final ByteArraySegmentBuilder builder = ByteArraySegment.builder();
      PrimitiveMessageType.SHORT.write(builder);
      final int messageId = TrUtils.rand.nextInt();
      builder.writeInt(messageId);
      ShortMessageType.SIMPLE.write(builder);
      builder.write(message);
            ByteArraySegment basMessage = encryptOutbound(builder.build());
            final Resender resender = new Resender(messageId, TrConstants.UDP_SHORT_MESSAGE_RETRY_ATTEMPTS, sentListener,
                    basMessage, this, priority);
      resenders.put(messageId, resender);
      resender.run();
    }
View Full Code Here

      resender.run();
    }
  }

  private ByteArraySegment encryptOutbound(final ByteArraySegment rawMessage) {
    final ByteArraySegmentBuilder toSend = ByteArraySegment.builder();
    if (!remoteHasCachedOurOutboundSymKey) {
      logger.debug("Remote hasn't yet cached our outboundSymKey, prepend it");
      toSend.write(TrCrypto.encryptRaw(outboundSymKey.toByteArraySegment(), remotePubKey));
    }
    toSend.write(outboundSymKey.encrypt(rawMessage));
        ByteArraySegment bas = toSend.build();
        return bas;
  }
View Full Code Here

  private void handleShortMessage(final DataInputStream dis, final int maxLength) throws IOException,
  TrSerializableException {
    final int messageId = dis.readInt();
    {
      // Construct and send an ack message
      final ByteArraySegmentBuilder ackMessage = ByteArraySegment.builder();
      PrimitiveMessageType.ACK.write(ackMessage);
      ackMessage.writeInt(messageId);
      logger.debug("Sending ACK");
      iface.sendTo(remoteAddress, encryptOutbound(ackMessage.build()),
          TrNetworkInterface.CONNECTION_MAINTAINANCE_PRIORITY);
    }
    final ShortMessageType type = ShortMessageType.forBytes.get(dis.readByte());
    if (recentlyReceivedShortMessages.contains(messageId))
      // Seen this message before, disregard
      return;
    recentlyReceivedShortMessages.add(messageId);
    switch (type) {
    case SIMPLE:
      listener.received(iface, remoteAddress, ByteArraySegment.from(dis, maxLength));
      break;
    case LONG_PART:
      final LongPart lh = TrSerializer.deserializeFrom(LongPart.class, dis);
      // logger.debug("Received " + lh);
      PendingLongMessage plm = pendingReceivedLongMessages.get(lh.longMessageId);
      if (plm == null) {
        plm = new PendingLongMessage(lh.totalParts);
        pendingReceivedLongMessages.put(lh.longMessageId, plm);
      }
      plm.parts[lh.partNumber] = lh.data;
      if (plm.isComplete()) {
        // logger.debug("LongPart " + lh.longMessageId +
        // " received in its entirity");
        pendingReceivedLongMessages.remove(lh.longMessageId);
        final ByteArraySegmentBuilder longMessage = ByteArraySegment.builder();
        for (final ByteArraySegment bas : plm.parts) {
          longMessage.write(bas);
        }
        listener.received(iface, remoteAddress, longMessage.build());
      }
    }
  }
View Full Code Here

  public static <T> TrPPKEncrypted<T> encrypt(final T plainText, final RSAPublicKey pubKey)
      throws TrSerializableException {
    // TODO: Lots of reading from and writing to byte arrays, inefficient
    final ByteArrayOutputStream serializedPlaintext = new ByteArrayOutputStream(TrConstants.DEFAULT_BAOS_SIZE);
    final ByteArraySegmentBuilder dos = ByteArraySegment.builder();
    try {
      TrSerializer.serializeTo(plainText, dos);
      dos.flush();
      final TrSymKey aesKey = createAesKey();
      final ByteArraySegment aesEncrypted = aesKey.encrypt(dos.build());
      final Cipher cipher = getRSACipher();
      cipher.init(Cipher.ENCRYPT_MODE, pubKey);
      final byte[] rsaEncryptedAesKey = cipher.doFinal(aesKey.toBytes());
      return new TrPPKEncrypted<T>(rsaEncryptedAesKey, aesEncrypted);
    } catch (final Exception e) {
View Full Code Here

TOP

Related Classes of tahrir.tools.ByteArraySegment.ByteArraySegmentBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.