示例#1
0
  public static void main(String[] args) throws IOException, InterruptedException {

    // create the root class with a thread pool size of 1
    DNP3Manager manager = DNP3ManagerFactory.createManager(1);

    // You can send the log messages anywhere you want
    // but PrintingLogSubscriber just prints them to the console
    manager.addLogSubscriber(PrintingLogSubscriber.getInstance());

    // Create a tcp channel class that will connect to the loopback
    Channel channel = manager.addTCPClient("client", LogLevel.INFO, 5000, "127.0.0.1", 20000);

    // You can optionally add a listener to receive state changes on the channel
    channel.addStateListener(
        new ChannelStateListener() {
          @Override
          public void onStateChange(ChannelState state) {
            System.out.println("Client state: " + state);
          }
        });

    // You can modify the defaults to change the way the master behaves
    MasterStackConfig config = new MasterStackConfig();

    // Create a master instance, pass in a simple singleton to print received values to the console
    Master master =
        channel.addMaster("master", LogLevel.INTERPRET, PrintingDataObserver.getInstance(), config);

    // You can optionally add a listener to receive state changes on the stack
    master.addStateListener(
        new StackStateListener() {
          @Override
          public void onStateChange(StackState state) {
            System.out.println("Master state: " + state);
          }
        });

    // This sub-interface can issue command requests
    CommandProcessor processor = master.getCommandProcessor();

    // all this stuff just to read a line of text in Java. Oh the humanity.
    String line = "";
    InputStreamReader converter = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(converter);

    while (true) {
      System.out.println("Enter something to issue a command or type <quit> to exit");
      line = in.readLine();
      if (line.equals("quit")) break;
      else {
        ControlRelayOutputBlock crob =
            new ControlRelayOutputBlock(
                ControlCode.LATCH_ON, (short) 1, 100, 100, CommandStatus.SUCCESS);
        ListenableFuture<CommandStatus> future = processor.selectAndOperate(crob, 0);
        System.out.println("Control result: " + future.get().name());
      }
    }

    manager.shutdown();
  }