List of usage examples for io.vertx.core.buffer Buffer getInt
int getInt(int pos);
From source file:de.openflorian.alarm.AlarmFaxEventMessageCodec.java
License:Open Source License
@Override public AlarmFaxEvent decodeFromWire(int position, Buffer buffer) { // My custom message starting from this *position* of buffer int _pos = position; // Length of JSON int length = buffer.getInt(_pos); String jsonStr = buffer.getString(_pos += 4, _pos += length); JsonObject contentJson = new JsonObject(jsonStr); String fileName = contentJson.getString(JSON_PROPERTY_FILENAME); return new AlarmFaxEvent(new File(fileName)); }
From source file:de.openflorian.alarm.OperationMessageCodec.java
License:Open Source License
@Override public Operation decodeFromWire(int position, Buffer buffer) { // My custom message starting from this *position* of buffer int _pos = position; // Length of JSON int length = buffer.getInt(_pos); String jsonStr = buffer.getString(_pos += 4, _pos += length); Operation o;/*from w ww .j a v a 2 s . c o m*/ try { o = Operation.fromJson(jsonStr); return o; } catch (Exception e) { log.error(e.getMessage(), e); throw new RuntimeException(e.getMessage(), e); } }
From source file:examples.BufferExamples.java
License:Open Source License
public void example8() { Buffer buff = Buffer.buffer(); for (int i = 0; i < buff.length(); i += 4) { System.out.println("int value at " + i + " is " + buff.getInt(i)); }//from www. j a v a 2 s.c o m }
From source file:io.flowly.core.codecs.FlowInstanceCodec.java
License:Open Source License
@Override public FlowInstance decodeFromWire(int pos, Buffer buffer) { // Same as decoding a JsonObject. int length = buffer.getInt(pos); pos += 4;/*from ww w . j av a 2 s . c o m*/ byte[] encoded = buffer.getBytes(pos, pos + length); String str = new String(encoded, CharsetUtil.UTF_8); return new FlowInstance(str); }
From source file:io.flowly.core.codecs.FlowMetadataCodec.java
License:Open Source License
@Override public FlowMetadata decodeFromWire(int pos, Buffer buffer) { // Same as decoding a JsonObject. int length = buffer.getInt(pos); pos += 4;//from ww w . j a va2 s .c om byte[] encoded = buffer.getBytes(pos, pos + length); String str = new String(encoded, CharsetUtil.UTF_8); return new FlowMetadata(str); }
From source file:microservicerx.dht.routing.SerializableCodec.java
@Override public KLASS decodeFromWire(int position, Buffer buffer) { int _pos = position; // get length of byte[] int length = buffer.getInt(_pos); // Get JSON string by it`s length // Jump 4 because getInt() == 4 bytes byte[] data = buffer.getBytes(_pos += 4, _pos += length); return Serializer.deserialize(data); }
From source file:microservicerx.rx.DistributedObservableCodec.java
@Override public DistributedObservable decodeFromWire(int position, Buffer buffer) { // My custom message starting from this *position* of buffer int _pos = position; // Length of JSON int length = buffer.getInt(_pos); // Get JSON string by it`s length // Jump 4 because getInt() == 4 bytes String jsonStr = buffer.getString(_pos += 4, _pos += length); JsonObject contentJson = new JsonObject(jsonStr); // Get fields String address = contentJson.getString("address"); // We can finally create custom message object return new DistributedObservable(address); }
From source file:net.kuujo.copycat.vertx.VertxTcpProtocolClient.java
License:Apache License
@Override public CompletableFuture<Void> connect() { final CompletableFuture<Void> future = new CompletableFuture<>(); if (vertx == null) vertx = protocol.getVertx();//from w w w. ja v a 2 s. c o m if (vertx == null) vertx = Vertx.vertx(); if (client == null) { NetClientOptions options = new NetClientOptions().setTcpKeepAlive(true).setTcpNoDelay(true) .setSendBufferSize(protocol.getSendBufferSize()) .setReceiveBufferSize(protocol.getReceiveBufferSize()).setSsl(protocol.isSsl()) .setTrustAll(protocol.isClientTrustAll()).setUsePooledBuffers(true); client = vertx.createNetClient(options); client.connect(port, host, result -> { if (result.failed()) { future.completeExceptionally(result.cause()); } else { socket = result.result(); RecordParser parser = RecordParser.newFixed(4, null); Handler<Buffer> handler = new Handler<Buffer>() { int length = -1; @Override public void handle(Buffer buffer) { if (length == -1) { length = buffer.getInt(0); parser.fixedSizeMode(length + 8); } else { handleResponse(buffer.getLong(0), buffer.getBuffer(8, length + 8).getByteBuf().nioBuffer()); length = -1; parser.fixedSizeMode(4); } } }; parser.setOutput(handler); socket.handler(parser); future.complete(null); } }); } else { future.complete(null); } return future; }
From source file:net.kuujo.copycat.vertx.VertxTcpProtocolServer.java
License:Apache License
@Override public CompletableFuture<Void> listen() { final CompletableFuture<Void> future = new CompletableFuture<>(); if (vertx == null) vertx = protocol.getVertx();//from ww w . j a v a 2 s .c o m if (vertx == null) vertx = Vertx.vertx(); if (server == null) { NetServerOptions options = new NetServerOptions().setTcpKeepAlive(true).setTcpNoDelay(true) .setReuseAddress(true).setAcceptBacklog(protocol.getAcceptBacklog()) .setSendBufferSize(protocol.getSendBufferSize()) .setReceiveBufferSize(protocol.getReceiveBufferSize()).setSsl(protocol.isSsl()) .setClientAuthRequired(protocol.isClientAuthRequired()).setUsePooledBuffers(true); server = vertx.createNetServer(options); server.connectHandler(socket -> { RecordParser parser = RecordParser.newFixed(4, null); Handler<Buffer> handler = new Handler<Buffer>() { int length = -1; @Override public void handle(Buffer buffer) { if (length == -1) { length = buffer.getInt(0); parser.fixedSizeMode(length + 8); } else { handleRequest(buffer.getLong(0), socket, buffer.getBuffer(8, length + 8).getByteBuf().nioBuffer()); length = -1; parser.fixedSizeMode(4); } } }; parser.setOutput(handler); socket.handler(parser); }).listen(port, host, result -> { if (result.failed()) { future.completeExceptionally(result.cause()); } else { future.complete(null); } }); } else { future.complete(null); } return future; }
From source file:org.matrixlab.pisces.metastore.core.CustomMessageCodec.java
@Override public CustomMessage decodeFromWire(int position, Buffer buffer) { // My custom message starting from this *position* of buffer int _pos = position; // Length of JSON int length = buffer.getInt(_pos); // Get JSON string by it`s length // Jump 4 because getInt() == 4 bytes String jsonStr = buffer.getString(_pos += 4, _pos += length); JsonObject contentJson = new JsonObject(jsonStr); // Get fields int statusCode = contentJson.getInteger("statusCode"); String resultCode = contentJson.getString("resultCode"); String summary = contentJson.getString("summary"); // We can finally create custom message object return new CustomMessage(statusCode, resultCode, summary); }