Example #1
0
  public void sendMyProxyAddress() throws IOException {
    outputStream = socket.getOutputStream();
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
    out = new BufferedWriter(outputStreamWriter);

    JsonObject jsonObject = new JsonObject();
    jsonObject
        .withKeyValue("msgtype", ModelCloudProxy.class.getSimpleName())
        .withKeyValue("hostName", modelCloud.getHostName())
        .withKeyValue("portNo", modelCloud.getAcceptPort());

    String message = jsonObject.toString();

    out.write(message + "\n");
    out.flush();

    proxy.setState("online");
  }
Example #2
0
  public void sendFile(String string, long lastModified, byte[] allBytes) {
    JsonObject jsonObject = new JsonObject();
    jsonObject
        .withKeyValue("msgtype", "fileTransfer")
        .withKeyValue("fileName", string)
        .withKeyValue("fileSize", allBytes.length)
        .withKeyValue("lastModified", lastModified);

    String message = jsonObject.toString();

    try {
      out.write(message + "\n");
      out.flush();
      outputStream.write(allBytes);
      outputStream.flush();
      out.write("There might be a problem with file end, thus I send some garbage here. \n\n\n");
      out.flush();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  @Override
  public boolean handleJSON(String... messages) {

    // analyze the JSON message
    JsonObject event = new JsonObject().withValue(messages);
    String timestampString = event.getString("@ts");
    Long timestamp = Long.parseLong(timestampString);
    String source = event.getString("@src");
    String property = event.getString("@prop");

    String oldValue = null;
    if (event.has("@ov")) oldValue = event.getString("@ov");
    String newValue = null;
    if (event.has("@nv")) newValue = event.getString("@nv");

    String sourceClass = source.split("@")[0];

    // is not responsible if the source is not correct
    if (!sourceClass.equals(CLASS_ZOMBIE_FIGHT_GAME)) return false;

    // save the data to the model
    ZombieFightGame zfg = ZombieFightGame.getInstanceById(source);
    if (zfg == null)
      zfg = Mediator.getInstance().getZombieFighter().createCurrentGame().withId(source);

    switch (property) {
      case "guide":
        Guide newGuide = Guide.getInstanceById(newValue);
        if (newGuide == null && newValue != null) newGuide = new Guide().withId(newValue);

        Guide oldGuide = Guide.getInstanceById(oldValue);

        if (oldValue != newValue) {
          zfg.withGuide(newGuide);
        }

        break;
      case "users":
        UserAssets newUser = UserAssets.getInstanceById(newValue);
        if (newUser == null && newValue != null) newUser = new UserAssets().withId(newValue);

        UserAssets oldUser = UserAssets.getInstanceById(oldValue);

        if (oldValue != newValue) {
          zfg.withoutUsers(oldUser);
          zfg.withUsers(newUser);
        }

        break;
      case "fields":
        Field newField = Field.getInstanceById(newValue);
        if (newField == null && newValue != null) newField = new Field().withId(newValue);

        Field oldField = Field.getInstanceById(oldValue);

        if (oldValue != newValue) {
          zfg.withoutFields(oldField);
          zfg.withFields(newField);
        }

        break;

      case "winner":
        GameUser newWinner = GameUser.getInstanceById(newValue);
        //			if (newWinner == null && newValue != null)
        //				newWinner = new GameUser().withId(newValue);

        GameUser oldWinner = GameUser.getInstanceById(oldValue);

        if (oldValue != newValue) {
          zfg.withWinner(newWinner);
        }

        break;
      case "name":
        zfg.withName(newValue);
        break;
      case "numPlayers":
        zfg.withNumPlayers(Integer.parseInt(newValue));
        break;
      case "running":
        zfg.withRunning(Boolean.parseBoolean(newValue));
        break;
      case "testGame":
        zfg.withTestGame(Boolean.parseBoolean(newValue));
        break;
      case "nextIncome":
        zfg.withNextIncome(Double.parseDouble(newValue));
        break;
      case "speed":
        zfg.withSpeed(Double.parseDouble(newValue));

        // Change scene after Loading datamodell
        showWaitingScreen();

        break;
      default:
        break;
    }

    return true;
  }