Package net.tomp2p.storage

Examples of net.tomp2p.storage.AlternativeCompositeByteBuf$Component


      InvalidKeyException, SignatureException, NoSuchAlgorithmException,
      InvalidKeySpecException {
    Message m1 = Utils2.createDummyMessage();
    m1.buffer(new Buffer(Unpooled.buffer()));
    Encoder e = new Encoder(null);
    AlternativeCompositeByteBuf buf = AlternativeCompositeByteBuf.compBuffer();
    e.write(buf, m1, null);
    Decoder d = new Decoder(null);
    boolean header = d.decodeHeader(buf, new InetSocketAddress(0),
        new InetSocketAddress(0));
    boolean payload = d.decodePayload(buf);
View Full Code Here


   * @return The message that was decoded.
   * @throws Exception .
   */
  private Message encodeDecode(final Message m1) throws Exception {
    AtomicReference<Message> m2 = new AtomicReference<Message>();
    final AlternativeCompositeByteBuf buf = AlternativeCompositeByteBuf.compBuffer();
    TomP2POutbound encoder = new TomP2POutbound(true,
        new DSASignatureFactory(), new CompByteBufAllocator() {
          @Override
          public AlternativeCompositeByteBuf compBuffer() {
            return buf;
          }
          @Override
          public AlternativeCompositeByteBuf compDirectBuffer() {
            return buf;
          }
        });
   
    buf.retain();
    ChannelHandlerContext ctx = mockChannelHandlerContext(buf, m2);
    encoder.write(ctx, m1, null);
    Decoder decoder = new Decoder(new DSASignatureFactory());
    decoder.decode(ctx, buf, m1.recipient().createSocketTCP(), m1
        .sender().createSocketTCP());
View Full Code Here

    Message m1 = Utils2.createDummyMessage();
    m1.intValue(intVal);
   
    Encoder encoder = new Encoder(null); // TODO signaturefactory?

    AlternativeCompositeByteBuf buf = AlternativeCompositeByteBuf.compBuffer(); // TODO what buffer to use?
   
    encoder.write(buf, m1, null);
   
    byte[] bytes = buf.array();
    writeToFile(bytes);
   
  }
View Full Code Here

                    LOG.debug("no sync required");
                } else {
                    // get the checksums
                  // TODO: don't copy data, toBytes does a copy!
                    List<Checksum> checksums = RSync.checksums(data.toBytes(), blockSize);
                    AlternativeCompositeByteBuf abuf = AlternativeCompositeByteBuf.compBuffer();
                    DataBuffer dataBuffer = SyncUtils.encodeChecksum(checksums, entry.getKey().versionKey(), data.hash(), abuf);
                    retVal.put(entry.getKey(), new Data(dataBuffer));
                    LOG.debug("sync required hash = {}", data.hash());
                }
            } else {
              if(isSyncFromOldVersion) {
                //TODO: the client could send us his history to figure out what the latest version in this history is
                Entry<Number640, Data> latest = storageLayer.
                    get(entry.getKey().minVersionKey(), entry.getKey().maxVersionKey(), 1, false).lastEntry();
                // TODO: don't copy data, toBytes does a copy!
                List<Checksum> checksums = RSync.checksums(latest.getValue().toBytes(), blockSize);
                AlternativeCompositeByteBuf abuf = AlternativeCompositeByteBuf.compBuffer();
                    DataBuffer dataBuffer = SyncUtils.encodeChecksum(checksums, latest.getKey().versionKey(),
                        latest.getValue().hash(), abuf);
                    retVal.put(entry.getKey(), new Data(dataBuffer));
                    LOG.debug("sync required for version");
              } else {
View Full Code Here

  /**
   * Encodes a message into a buffer, such that it can be used as a message payload (piggybacked), stored, etc.
   */
  public static Buffer encodeMessage(Message message, SignatureFactory signatureFactory) throws InvalidKeyException, SignatureException, IOException {
    Encoder e = new Encoder(signatureFactory);
    AlternativeCompositeByteBuf buf = AlternativeCompositeByteBuf.compBuffer();
    e.write(buf, message, message.receivedSignature());
    return new Buffer(buf);
  }
View Full Code Here

                            List<Checksum> checksums = SyncUtils.decodeChecksums(buffer);
                            // TODO: don't copy data, toBytes does a copy!
                            List<Instruction> instructions = RSync.instructions(
                                         data2.toBytes(), checksums, blockSize);
                           
                            AlternativeCompositeByteBuf abuf = AlternativeCompositeByteBuf.compBuffer();
                           
                            dataCopy += SyncUtils.encodeInstructions(instructions, versionKey, hash, abuf);
                            DataBuffer dataBuffer = new DataBuffer(abuf);
                            //diff
                            Data data1 = new Data(dataBuffer).flag1();
View Full Code Here

    private static final Random RND = new Random(SEED);
    private static final DSASignatureFactory factory = new DSASignatureFactory();

  @Test
  public void compositeBufferTest1() {
    AlternativeCompositeByteBuf cbuf = AlternativeCompositeByteBuf.compBuffer();
    cbuf.writeInt(1);
    ByteBuf buf = Unpooled.buffer();
    buf.writeInt(2);
    cbuf.capacity(4);
    cbuf.addComponent(buf);
    cbuf.writerIndex(8);

    Assert.assertEquals(1, cbuf.readInt());
    Assert.assertEquals(2, cbuf.readInt());
  }
View Full Code Here

    Assert.assertEquals(2, cbuf.readInt());
  }

  @Test
  public void compositeBufferTest2() {
    AlternativeCompositeByteBuf cbuf = AlternativeCompositeByteBuf.compBuffer();
    int len = 8 * 4;
    for (int i = 0; i < len; i += 4) {
      ByteBuf buf = Unpooled.buffer().writeInt(i);
      cbuf.capacity(cbuf.writerIndex()).addComponent(buf)
          .writerIndex(i + 4);
    }
    cbuf.writeByte(1);

    byte[] me = new byte[len];
    cbuf.readBytes(me);
    cbuf.readByte();

    System.err.println("reader: " + cbuf.readerIndex());
    System.err.println("writer: " + cbuf.writerIndex());
    System.err.println("capacity: " + cbuf.capacity());
    // see https://github.com/netty/netty/issues/1976
    cbuf.discardSomeReadBytes();
  }
View Full Code Here

    }

    @Override
    public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise)
            throws Exception {
        AlternativeCompositeByteBuf buf = null;
        if (!(msg instanceof Message)) {
        ctx.write(msg, promise);
            return;
      }
        try {
          boolean done = false;
               
            if (preferDirect) {
                buf = alloc.compDirectBuffer();
            } else {
                buf = alloc.compBuffer();
            }
            //null, means create signature
            done = encoder.write(buf, (Message) msg, null);
           
            final Message message = encoder.message();

            if (buf.isReadable()) {
                // this will release the buffer
                if (ctx.channel() instanceof DatagramChannel) {
                 
                  final InetSocketAddress recipientUnreflected;
                  final InetSocketAddress recipient;
                  final InetSocketAddress sender;
                    if (message.senderSocket() == null) {
                      //in case of a request
                      if(message.recipientRelay()!=null) {
                        //in case of sending to a relay (the relayed flag is already set)
                        recipientUnreflected = message.recipientRelay().createSocketUDP();
                      } else {
                        recipientUnreflected = message.recipient().createSocketUDP();
                      }
                      recipient = Utils.natReflection(recipientUnreflected, true, message.sender());
                      sender = message.sender().createSocketUDP(0);
                    } else {
                      //in case of a reply
                      recipient = message.senderSocket();
                      sender = message.recipientSocket();
                    }
                    DatagramPacket d = new DatagramPacket(buf, recipient, sender);
                    LOG.debug("Send UPD message {}, datagram: {}", message, d);
                    ctx.writeAndFlush(d, promise);
                   
                } else {
                    LOG.debug("Send TCP message {} to {}", message, message.senderSocket());
                    ctx.writeAndFlush(buf, promise);
                }
                if (done) {
                    message.setDone(true);
                    // we wrote the complete message, reset state
                    encoder.reset();
                }
            } else {
                buf.release();
                ctx.write(Unpooled.EMPTY_BUFFER, promise);
            }
            buf = null;

        } catch (Throwable t) {
            ctx.fireExceptionCaught(t);
        }
        finally {
            if (buf != null) {
                buf.release();
            }
        }
    }
View Full Code Here

    db.prepareDbUnit(getClass(), "migrate_components.xml");

    migration.execute();
    session.commit();

    Component root = mapper.selectComponentByKey("org.struts:struts");
    assertThat(root.getUuid()).isNotNull();
    assertThat(root.getProjectUuid()).isEqualTo(root.getUuid());
    assertThat(root.getModuleUuid()).isNull();
    assertThat(root.getModuleUuidPath()).isNull();

    Component module = mapper.selectComponentByKey("org.struts:struts-core");
    assertThat(module.getUuid()).isNotNull();
    assertThat(module.getProjectUuid()).isEqualTo(root.getUuid());
    assertThat(module.getModuleUuid()).isEqualTo(root.getUuid());
    assertThat(module.getModuleUuidPath()).isEqualTo(root.getUuid());

    Component subModule = mapper.selectComponentByKey("org.struts:struts-db");
    assertThat(subModule.getUuid()).isNotNull();
    assertThat(subModule.getProjectUuid()).isEqualTo(root.getUuid());
    assertThat(subModule.getModuleUuid()).isEqualTo(module.getUuid());
    assertThat(subModule.getModuleUuidPath()).isEqualTo(root.getUuid() + "." + module.getUuid());

    Component directory = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts");
    assertThat(directory.getUuid()).isNotNull();
    assertThat(directory.getProjectUuid()).isEqualTo(root.getUuid());
    assertThat(directory.getModuleUuid()).isEqualTo(subModule.getUuid());
    assertThat(directory.getModuleUuidPath()).isEqualTo(root.getUuid() + "." + module.getUuid() + "." + subModule.getUuid());

    Component file = mapper.selectComponentByKey("org.struts:struts-core:src/org/struts/RequestContext.java");
    assertThat(file.getUuid()).isNotNull();
    assertThat(file.getProjectUuid()).isEqualTo(root.getUuid());
    assertThat(file.getModuleUuid()).isEqualTo(subModule.getUuid());
    assertThat(file.getModuleUuidPath()).isEqualTo(root.getUuid() + "." + module.getUuid() + "." + subModule.getUuid());

    // Verify that each generated uuid is unique
    assertThat(ImmutableSet.of(root.getUuid(), module.getUuid(), subModule.getUuid(), directory.getUuid(), file.getUuid())).hasSize(5);
  }
View Full Code Here

TOP

Related Classes of net.tomp2p.storage.AlternativeCompositeByteBuf$Component

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.