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);
}
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());
}
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;
}
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;
}
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);
});
}
Aggregations