Пример #1
0
  private Message parseMessage(byte[] data) {

    Message result = new Message();
    try {
      ByteArrayInputStream bais = new ByteArrayInputStream(data);
      DataInputStream dis = new DataInputStream(new BufferedInputStream(bais));
      int cmdCount = dis.readInt();
      byte[] cmdBytes = new byte[cmdCount];
      dis.read(cmdBytes);
      String cmd = new String(cmdBytes);
      result.setCommand(cmd);

      if (cmd.equalsIgnoreCase("CALC")) {

        int valuesCount = dis.readInt();
        byte[] valuesBytes = new byte[valuesCount];
        dis.read(valuesBytes);
        String[] values = new String(valuesBytes).split("\0");
        for (String v : values) {
          String[] s = v.split("=");
          result.addValue(s[0], s[1]);
        }
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    }

    return result;
  }
Пример #2
0
  private double calculateArea() {

    double area = 0.0;
    switch (_message.getShapeType().toLowerCase()) {
      case "circle":
        double radius = Double.parseDouble(_message.getValue("radius"));
        area = radius * radius * Math.PI;
        break;
      case "rectangle":
        double a = Double.parseDouble(_message.getValue("a"));
        double b = Double.parseDouble(_message.getValue("b"));
        area = a * b;
        break;
    }
    return area;
  }
Пример #3
0
  private void processMessage(Message message) {

    MessageProcess messageProcess =
        MessageProcessFactory.getInstance(this).createMessageProcess(message);
    String responseMessage = messageProcess.response();
    sendMessage(responseMessage);

    System.out.println(message.getCommand());
  }
Пример #4
0
  public MessageProcess createMessageProcess(Message message) {

    switch (message.getCommand()) {
      case "CONN":
        return new ConnectMessageProcess();
      case "DISC":
        return new DisconnectMessageProcess(_service);
      case "CALC":
        return new CalculateMessageProcess(message);
      default:
        return new NullMessageProcess();
    }
  }