Search in sources :

Example 41 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project baseio by generallycloud.

the class TestProtobufClient method main.

public static void main(String[] args) throws Exception {
    ProtobufUtil protobufUtil = new ProtobufUtil();
    protobufUtil.regist(SearchRequest.getDefaultInstance());
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            ProtobaseFuture f = (ProtobaseFuture) future;
            SearchRequest res = (SearchRequest) protobufUtil.getMessage(f);
            System.out.println();
            System.out.println("________" + res);
            System.out.println();
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    // context.addSessionEventListener(new SessionActiveSEListener());
    // context.setBeatFutureFactory(new FLBeatFutureFactory());
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    SocketSession session = connector.connect();
    ProtobaseFuture f = new ProtobaseFutureImpl(context);
    ByteString byteString = ByteString.copyFrom("222".getBytes());
    SearchRequest request = SearchRequest.newBuilder().setCorpus(Corpus.IMAGES).setPageNumber(100).setQuery("test").setQueryBytes(byteString).setResultPerPage(-1).build();
    protobufUtil.writeProtobuf(request, f);
    session.flush(f);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : SearchRequest(com.generallycloud.test.io.protobuf.TestProtoBufBean.SearchRequest) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ByteString(com.google.protobuf.ByteString) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) ProtobufUtil(com.generallycloud.baseio.codec.protobuf.ProtobufUtil) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future)

Example 42 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project Terasology by MovingBlocks.

the class EntityDataJSONFormatTest method testPersistBytes.

@Test
public void testPersistBytes() throws Exception {
    ByteString bytes = ByteString.copyFrom(new byte[] { 1, 2, 3, 4 });
    nameValueBuilder.setName(VALUE_NAME);
    nameValueBuilder.setValue(EntityData.Value.newBuilder().setBytes(bytes));
    componentBuilder.addField(nameValueBuilder);
    entityBuilder.addComponent(componentBuilder.build());
    worldBuilder.addEntity(entityBuilder.build());
    EntityData.GlobalStore actual = persistAndRetrieve(worldBuilder.build());
    assertEquals(VALUE_NAME, actual.getEntity(0).getComponent(0).getField(0).getName());
    assertArrayEquals(bytes.toByteArray(), actual.getEntity(0).getComponent(0).getField(0).getValue().getBytes().toByteArray());
}
Also used : ByteString(com.google.protobuf.ByteString) EntityData(org.terasology.protobuf.EntityData) Test(org.junit.Test)

Example 43 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project java-tron by tronprotocol.

the class Manager method generateBlock.

/**
 * Generate a block.
 */
public synchronized BlockCapsule generateBlock(final WitnessCapsule witnessCapsule, final long when, final byte[] privateKey) throws ValidateSignatureException, ContractValidateException, ContractExeException, UnLinkedBlockException {
    final long timestamp = this.dynamicPropertiesStore.getLatestBlockHeaderTimestamp();
    final long number = this.dynamicPropertiesStore.getLatestBlockHeaderNumber();
    final ByteString preHash = this.dynamicPropertiesStore.getLatestBlockHeaderHash();
    // judge create block time
    if (when < timestamp) {
        throw new IllegalArgumentException("generate block timestamp is invalid.");
    }
    long currentTrxSize = 0;
    long postponedTrxCount = 0;
    final BlockCapsule blockCapsule = new BlockCapsule(number + 1, preHash, when, witnessCapsule.getAddress());
    dialog.reset();
    dialog = DialogOptional.of(revokingStore.buildDialog());
    Iterator iterator = pendingTransactions.iterator();
    while (iterator.hasNext()) {
        TransactionCapsule trx = (TransactionCapsule) iterator.next();
        currentTrxSize += RamUsageEstimator.sizeOf(trx);
        // judge block size
        if (currentTrxSize > TRXS_SIZE) {
            postponedTrxCount++;
            continue;
        }
        // apply transaction
        try (Dialog tmpDialog = revokingStore.buildDialog()) {
            processTransaction(trx);
            tmpDialog.merge();
            // push into block
            blockCapsule.addTransaction(trx);
            iterator.remove();
        } catch (ContractExeException e) {
            logger.info("contract not processed during execute");
            logger.debug(e.getMessage(), e);
        } catch (ContractValidateException e) {
            logger.info("contract not processed during validate");
            logger.debug(e.getMessage(), e);
        } catch (RevokingStoreIllegalStateException e) {
            logger.debug(e.getMessage(), e);
        }
    }
    dialog.reset();
    if (postponedTrxCount > 0) {
        logger.info("{} transactions over the block size limit", postponedTrxCount);
    }
    logger.info("postponedTrxCount[" + postponedTrxCount + "],TrxLeft[" + pendingTransactions.size() + "]");
    blockCapsule.setMerkleRoot();
    blockCapsule.sign(privateKey);
    blockCapsule.generatedByMyself = true;
    this.pushBlock(blockCapsule);
    return blockCapsule;
}
Also used : TransactionCapsule(org.tron.core.capsule.TransactionCapsule) ByteString(com.google.protobuf.ByteString) Dialog(org.tron.core.db.AbstractRevokingStore.Dialog) ContractValidateException(org.tron.core.exception.ContractValidateException) Iterator(java.util.Iterator) BlockCapsule(org.tron.core.capsule.BlockCapsule) ContractExeException(org.tron.core.exception.ContractExeException) RevokingStoreIllegalStateException(org.tron.core.exception.RevokingStoreIllegalStateException)

Example 44 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project java-tron by tronprotocol.

the class Manager method getScheduledWitness.

/**
 * get ScheduledWitness by slot.
 */
public ByteString getScheduledWitness(final long slot) {
    final long currentSlot = getHeadSlot() + slot;
    if (currentSlot < 0) {
        throw new RuntimeException("currentSlot should be positive.");
    }
    final List<WitnessCapsule> currentShuffledWitnesses = this.getShuffledWitnessStates();
    if (CollectionUtils.isEmpty(currentShuffledWitnesses)) {
        throw new RuntimeException("ShuffledWitnesses is null.");
    }
    final int witnessIndex = (int) currentSlot % currentShuffledWitnesses.size();
    final ByteString scheduledWitness = currentShuffledWitnesses.get(witnessIndex).getAddress();
    return scheduledWitness;
}
Also used : WitnessCapsule(org.tron.core.capsule.WitnessCapsule) ByteString(com.google.protobuf.ByteString)

Example 45 with ByteString

use of org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString in project java-tron by tronprotocol.

the class Manager method initWitness.

/**
 * save witnesses into database.
 */
private void initWitness() {
    final Args args = Args.getInstance();
    final GenesisBlock genesisBlockArg = args.getGenesisBlock();
    genesisBlockArg.getWitnesses().forEach(key -> {
        byte[] keyAddress = ByteArray.fromHexString(key.getAddress());
        ByteString address = ByteString.copyFrom(keyAddress);
        if (!this.accountStore.has(keyAddress)) {
            final AccountCapsule accountCapsule = new AccountCapsule(ByteString.EMPTY, address, AccountType.AssetIssue, 0L);
            this.accountStore.put(keyAddress, accountCapsule);
        }
        final WitnessCapsule witnessCapsule = new WitnessCapsule(address, key.getVoteCount(), key.getUrl());
        witnessCapsule.setIsJobs(true);
        this.witnessStore.put(keyAddress, witnessCapsule);
        this.wits.add(witnessCapsule);
    });
}
Also used : GenesisBlock(org.tron.core.config.args.GenesisBlock) AccountCapsule(org.tron.core.capsule.AccountCapsule) Args(org.tron.core.config.args.Args) WitnessCapsule(org.tron.core.capsule.WitnessCapsule) ByteString(com.google.protobuf.ByteString)

Aggregations

ByteString (com.google.protobuf.ByteString)406 Test (org.junit.Test)143 ArrayList (java.util.ArrayList)65 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)63 HashMap (java.util.HashMap)41 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)40 IOException (java.io.IOException)37 List (java.util.List)33 Map (java.util.Map)33 ServerRequest (com.pokegoapi.main.ServerRequest)17 ExecutionException (java.util.concurrent.ExecutionException)16 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)14 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)14 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)14 Feature (com.google.cloud.vision.v1.Feature)14 Image (com.google.cloud.vision.v1.Image)14 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)14 FileInputStream (java.io.FileInputStream)13 ByteBuffer (java.nio.ByteBuffer)13 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)12