Beispiel #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();
  }
    @Override
    public void run() {

      BufferedReader in = null;
      PrintWriter out = null;

      try {
        in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
        OutputStream outputStream = socket.getOutputStream();
        out = new PrintWriter(outputStream);

        String line = in.readLine();
        CommandRequest request = CommandRequest.parse(line);

        out.print("HTTP/1.1 200 OK\r\n\r\n");
        out.flush();
        if (StringUtils.isEmpty(request.getCommand())) {
          out.println("Command is blank");
          out.flush();
          return;
        }

        CommandProcessor commandProcessor = getCommandProcessor(request.getCommand());
        if (commandProcessor != null) {
          commandProcessor.execute(outputStream, request);
        } else {
          out.println("Can not find the command:[" + request.getCommand() + "]");
        }
        out.flush();

      } catch (Throwable t) {
        LOGGER.error("EventRunnable error", t);

        try {
          if (out != null) {
            out.println("CommandCenter error, message is " + t.getMessage());
            out.flush();
          }
        } catch (Exception e) {
          LOGGER.error("EventRunnable error", t);
        }

      } finally {
        try {
          if (out != null) {
            out.close();
          }
          if (in != null) {
            in.close();
          }
          socket.close();
        } catch (Exception e) {
          LOGGER.error("EventRunnable close resource error", e);
        }
      }
    }
 public void start() {
   try {
     commandProcessor.start();
   } catch (Exception e) {
     final String message = e.getMessage();
     if (message != null && message.startsWith("Connection refused")) {
       throw new RuntimeException(
           "Could not contact Selenium Server; have you started it on '"
               + commandProcessor.getRemoteControlServerLocation()
               + "' ?\nRead more at http://seleniumhq.org/projects/remote-control/not-started.html\n"
               + e.getMessage());
     }
     throw new RuntimeException("Could not start Selenium session: " + e.getMessage(), e);
   }
 }
 public void waitForCondition(String script, String timeout) {
   commandProcessor.doCommand(
       "waitForCondition",
       new String[] {
         script, timeout,
       });
 }
 public void assignId(String locator, String identifier) {
   commandProcessor.doCommand(
       "assignId",
       new String[] {
         locator, identifier,
       });
 }
 public Number getXpathCount(String xpath) {
   return commandProcessor.getNumber(
       "getXpathCount",
       new String[] {
         xpath,
       });
 }
 public Number getCursorPosition(String locator) {
   return commandProcessor.getNumber(
       "getCursorPosition",
       new String[] {
         locator,
       });
 }
 public Number getElementPositionTop(String locator) {
   return commandProcessor.getNumber(
       "getElementPositionTop",
       new String[] {
         locator,
       });
 }
 public String[] getSelectOptions(String selectLocator) {
   return commandProcessor.getStringArray(
       "getSelectOptions",
       new String[] {
         selectLocator,
       });
 }
 public boolean isSomethingSelected(String selectLocator) {
   return commandProcessor.getBoolean(
       "isSomethingSelected",
       new String[] {
         selectLocator,
       });
 }
 public String getSelectedId(String selectLocator) {
   return commandProcessor.getString(
       "getSelectedId",
       new String[] {
         selectLocator,
       });
 }
 public String getTable(String tableCellAddress) {
   return commandProcessor.getString(
       "getTable",
       new String[] {
         tableCellAddress,
       });
 }
 public boolean isChecked(String locator) {
   return commandProcessor.getBoolean(
       "isChecked",
       new String[] {
         locator,
       });
 }
 public String getEval(String script) {
   return commandProcessor.getString(
       "getEval",
       new String[] {
         script,
       });
 }
 public void highlight(String locator) {
   commandProcessor.doCommand(
       "highlight",
       new String[] {
         locator,
       });
 }
 public void setCursorPosition(String locator, String position) {
   commandProcessor.doCommand(
       "setCursorPosition",
       new String[] {
         locator, position,
       });
 }
 public boolean isOrdered(String locator1, String locator2) {
   return commandProcessor.getBoolean(
       "isOrdered",
       new String[] {
         locator1, locator2,
       });
 }
 public String getAttribute(String attributeLocator) {
   return commandProcessor.getString(
       "getAttribute",
       new String[] {
         attributeLocator,
       });
 }
 public Number getElementHeight(String locator) {
   return commandProcessor.getNumber(
       "getElementHeight",
       new String[] {
         locator,
       });
 }
 public boolean isTextPresent(String pattern) {
   return commandProcessor.getBoolean(
       "isTextPresent",
       new String[] {
         pattern,
       });
 }
 public String getExpression(String expression) {
   return commandProcessor.getString(
       "getExpression",
       new String[] {
         expression,
       });
 }
 public boolean isElementPresent(String locator) {
   return commandProcessor.getBoolean(
       "isElementPresent",
       new String[] {
         locator,
       });
 }
 public Number getCssCount(String css) {
   return commandProcessor.getNumber(
       "getCssCount",
       new String[] {
         css,
       });
 }
 public boolean isEditable(String locator) {
   return commandProcessor.getBoolean(
       "isEditable",
       new String[] {
         locator,
       });
 }
 public void allowNativeXpath(String allow) {
   commandProcessor.doCommand(
       "allowNativeXpath",
       new String[] {
         allow,
       });
 }
 public String[] getAttributeFromAllWindows(String attributeName) {
   return commandProcessor.getStringArray(
       "getAttributeFromAllWindows",
       new String[] {
         attributeName,
       });
 }
 public void ignoreAttributesWithoutValue(String ignore) {
   commandProcessor.doCommand(
       "ignoreAttributesWithoutValue",
       new String[] {
         ignore,
       });
 }
 public void setMouseSpeed(String pixels) {
   commandProcessor.doCommand(
       "setMouseSpeed",
       new String[] {
         pixels,
       });
 }
 public void setTimeout(String timeout) {
   commandProcessor.doCommand(
       "setTimeout",
       new String[] {
         timeout,
       });
 }
 public void dragAndDrop(String locator, String movementsString) {
   commandProcessor.doCommand(
       "dragAndDrop",
       new String[] {
         locator, movementsString,
       });
 }