Exemple #1
0
  private void runAsMaster() {
    System.out.println("Run as master model ......");
    node = new DefaultMasterNode(config);
    node.addCommandHandler(
        ECHO,
        new CommandHandler() {

          @Override
          public Command handle(Command command) {
            ByteBuffer buffer = command.getPayLoad();
            int size = command.getSize() - Command.getHeaderSize();
            assert (buffer.remaining() == size);
            logger.info(
                String.format(
                    "Receive %d bytes data from slave:%s",
                    size, command.getSession().getSlaveWorker()));
            Packet.dump(buffer);
            byte[] bytes = buffer.array();
            for (int i = 0; i < bytes.length; i++) {
              bytes[i] = (byte) (1 - bytes[i]);
            }
            return CommandFactory.createCommand(ECHO, buffer);
          }
        });
    node.init();
    node.start();
  }
Exemple #2
0
  private void runAsSlave() {
    System.out.println("Run as slave model ......");
    config.setMasterAddress("127.0.0.1");
    node = new DefaultSlaveNode(config);
    // command
    ((SlaveNode) node)
        .addCommandProvider(
            new CommandProvider() {

              private int count = 0;

              @Override
              public String getName() {
                return "SmallDataCommandProvider";
              }

              @Override
              public Command produce() {
                byte[] buffer = new byte[dataSize];
                int pad = count++ % 2;
                for (int i = 0; i < dataSize; i++) {
                  buffer[i] = (byte) pad;
                }
                return CommandFactory.createCommand(ECHO, ByteBuffer.wrap(buffer));
              }

              @Override
              public List<Command> produce(long count) {
                throw new UnsupportedOperationException("Not supported");
              }
            });

    node.addCommandHandler(
        ECHO,
        new CommandHandler() {

          @Override
          public Command handle(Command command) {
            ByteBuffer buffer = command.getPayLoad();
            int size = command.getSize() - Command.getHeaderSize();
            assert (buffer.remaining() == size);
            logger.info(String.format("Receive %d bytes data from master", size));
            Packet.dump(buffer);
            return CommandFactory.createCommand(ECHO, buffer);
          }
        });

    node.init();
    node.start();
  }