Java Code Examples for io.vertx.core.parsetools.RecordParser#handler()

The following examples show how to use io.vertx.core.parsetools.RecordParser#handler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtocolGateway.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
void handleConnect(final NetSocket socket) {

        final Map<String, Object> dict = new HashMap<>();
        final RecordParser commandParser = RecordParser.newDelimited("\n", socket);
        commandParser.endHandler(end -> socket.close());
        commandParser.exceptionHandler(t -> {
            LOG.debug("error processing data from device", t);
            socket.close();
        });
        commandParser.handler(data -> handleData(socket, dict, data));
        socket.closeHandler(remoteClose -> {
            LOG.debug("device closed connection");
            Optional.ofNullable((CommandConsumer) dict.get(KEY_COMMAND_CONSUMER))
                .ifPresent(c -> {
                    c.close(res -> {
                        LOG.debug("closed device's command consumer");
                    });
                });
            socket.close();
        });

        socket.write(String.format("Welcome to the Protocol Gateway for devices of tenant [%s], please enter a command\n", tenant));
        LOG.debug("connection with client established");
    }
 
Example 2
Source File: DatabaseReader.java    From vertx-in-action with MIT License 5 votes vote down vote up
public static void main(String[] args) {
  Vertx vertx = Vertx.vertx();

  AsyncFile file = vertx.fileSystem().openBlocking("sample.db",
    new OpenOptions().setRead(true));

  RecordParser parser = RecordParser.newFixed(4, file);
  parser.handler(header -> readMagicNumber(header, parser));
  parser.endHandler(v -> vertx.close());
}
 
Example 3
Source File: DatabaseReader.java    From vertx-in-action with MIT License 5 votes vote down vote up
private static void finishEntry(String key, Buffer valueLength, RecordParser parser) {
  parser.fixedSizeMode(valueLength.getInt(0));
  parser.handler(value -> {
    logger.info("Key: {} / Value: {}", key, value);
    parser.fixedSizeMode(4);
    parser.handler(keyLength -> readKey(keyLength, parser));
  });
}
 
Example 4
Source File: FetchDatabaseReader.java    From vertx-in-action with MIT License 5 votes vote down vote up
public static void main(String[] args) {
  Vertx vertx = Vertx.vertx();

  AsyncFile file = vertx.fileSystem().openBlocking("sample.db",
    new OpenOptions().setRead(true));

  RecordParser parser = RecordParser.newFixed(4, file);
  parser.pause();
  parser.fetch(1);
  parser.handler(header -> readMagicNumber(header, parser));
  parser.endHandler(v -> vertx.close());
}
 
Example 5
Source File: FetchDatabaseReader.java    From vertx-in-action with MIT License 5 votes vote down vote up
private static void finishEntry(String key, Buffer valueLength, RecordParser parser) {
  parser.fixedSizeMode(valueLength.getInt(0));
  parser.handler(value -> {
    logger.info("Key: {} / Value: {}", key, value);
    parser.fixedSizeMode(4);
    parser.handler(keyLength -> readKey(keyLength, parser));
    parser.fetch(1);
  });
  parser.fetch(1);
}
 
Example 6
Source File: DatabaseReader.java    From vertx-in-action with MIT License 4 votes vote down vote up
private static void readMagicNumber(Buffer header, RecordParser parser) {
  logger.info("Magic number: {}:{}:{}:{}", header.getByte(0), header.getByte(1), header.getByte(2), header.getByte(3));
  parser.handler(version -> readVersion(version, parser));
}
 
Example 7
Source File: DatabaseReader.java    From vertx-in-action with MIT License 4 votes vote down vote up
private static void readVersion(Buffer header, RecordParser parser) {
  logger.info("Version: {}", header.getInt(0));
  parser.delimitedMode("\n");
  parser.handler(name -> readName(name, parser));
}
 
Example 8
Source File: DatabaseReader.java    From vertx-in-action with MIT License 4 votes vote down vote up
private static void readName(Buffer name, RecordParser parser) {
  logger.info("Name: {}", name.toString());
  parser.fixedSizeMode(4);
  parser.handler(keyLength -> readKey(keyLength, parser));
}
 
Example 9
Source File: DatabaseReader.java    From vertx-in-action with MIT License 4 votes vote down vote up
private static void readKey(Buffer keyLength, RecordParser parser) {
  parser.fixedSizeMode(keyLength.getInt(0));
  parser.handler(key -> readValue(key.toString(), parser));
}
 
Example 10
Source File: DatabaseReader.java    From vertx-in-action with MIT License 4 votes vote down vote up
private static void readValue(String key, RecordParser parser) {
  parser.fixedSizeMode(4);
  parser.handler(valueLength -> finishEntry(key, valueLength, parser));
}
 
Example 11
Source File: FetchDatabaseReader.java    From vertx-in-action with MIT License 4 votes vote down vote up
private static void readMagicNumber(Buffer header, RecordParser parser) {
  logger.info("Magic number: {}:{}:{}:{}", header.getByte(0), header.getByte(1), header.getByte(2), header.getByte(3));
  parser.handler(version -> readVersion(version, parser));
  parser.fetch(1);
}
 
Example 12
Source File: FetchDatabaseReader.java    From vertx-in-action with MIT License 4 votes vote down vote up
private static void readVersion(Buffer header, RecordParser parser) {
  logger.info("Version: {}", header.getInt(0));
  parser.delimitedMode("\n");
  parser.handler(name -> readName(name, parser));
  parser.fetch(1);
}
 
Example 13
Source File: FetchDatabaseReader.java    From vertx-in-action with MIT License 4 votes vote down vote up
private static void readName(Buffer name, RecordParser parser) {
  logger.info("Name: {}", name.toString());
  parser.fixedSizeMode(4);
  parser.handler(keyLength -> readKey(keyLength, parser));
  parser.fetch(1);
}
 
Example 14
Source File: FetchDatabaseReader.java    From vertx-in-action with MIT License 4 votes vote down vote up
private static void readKey(Buffer keyLength, RecordParser parser) {
  parser.fixedSizeMode(keyLength.getInt(0));
  parser.handler(key -> readValue(key.toString(), parser));
  parser.fetch(1);
}
 
Example 15
Source File: FetchDatabaseReader.java    From vertx-in-action with MIT License 4 votes vote down vote up
private static void readValue(String key, RecordParser parser) {
  parser.fixedSizeMode(4);
  parser.handler(valueLength -> finishEntry(key, valueLength, parser));
  parser.fetch(1);
}