Exemplo n.º 1
0
  public Message onExceptionThrown(Message message, InetSocketAddress destinationAddress) {

    boolean gsAvailable = checkBFFailures(destinationAddress);
    MessageRequest request = (MessageRequest) message.get("request");

    switch (request) {
      case disconnectedBF:
        if (gsAvailable) return message;
        break;
      case disconnectedUnit:
        if (gsAvailable) return message;
        break;
      case gameState:
        Unit u;
        synchronized (this) {
          u = units.remove(destinationAddress);
          map[u.getX()][u.getY()] = null;
        }
        Integer[] tempClock = vClock.incrementClock(id);
        LogEntry entry =
            new LogEntry(tempClock, LogEntryType.DISCONNECTED_UNIT, destinationAddress);
        logger.writeAsText(entry, true);
        Message replyMessage = new Message();
        replyMessage.put("request", MessageRequest.disconnectedUnit);
        replyMessage.put("unitAddress", destinationAddress);
        replyMessage.put("vclock", tempClock);

        synchronizeWithAllBF(replyMessage);
        break;
      default:
        break;
    }
    return null;
  }
Exemplo n.º 2
0
  private Message processEvent(Message msg, ActionInfo removeAction) {
    Unit unit = null;
    LogEntry entry;
    Integer[] tempClock;

    switch ((MessageRequest) removeAction.message.get("request")) {
      case spawnUnit:
        {
          // System.out.println("BATTLE FIELD:Spawn" + port);
          // System.out.println(battlefields.toString());

          Boolean succeded =
              this.spawnUnit(
                  (Unit) msg.get("unit"),
                  (InetSocketAddress) msg.get("address"),
                  (Integer) msg.get("x"),
                  (Integer) msg.get("y"));
          if (succeded) {
            units.put((InetSocketAddress) msg.get("address"), (Unit) msg.get("unit"));
          }
          Message reply = new Message();
          reply.put("request", MessageRequest.spawnAck);
          reply.put("succeded", succeded);
          reply.put("gamestate", map);
          // Puts position of the unit we are sending to in the map we are sending
          Unit u = units.get((InetSocketAddress) msg.get("address"));
          reply.put("unit", u);

          tempClock = ((Integer[]) msg.get("vclock")).clone();

          entry =
              new LogEntry(
                  tempClock,
                  LogEntryType.SPAWN,
                  (InetSocketAddress) msg.get("address"),
                  new Position((Integer) msg.get("x"), (Integer) msg.get("y")));
          logger.writeAsText(entry, true);
          if (!((InetSocketAddress) msg.get("serverAddress"))
              .equals(new InetSocketAddress(url, port))) {
            // System.out.println("<"+url+":"+port+"> Spawn will be processed -->
            // "+toStringArray(tempClock));
            vClock.updateClock(tempClock);
          }
          return reply;
        }
      case dealDamage:
        {
          int x = (Integer) msg.get("x");
          int y = (Integer) msg.get("y");
          unit = this.getUnit(x, y);
          if (unit != null) {
            unit.adjustHitPoints(-(Integer) msg.get("damage"));

            Unit attackingUnit = (Unit) msg.get("unit");
            // System.out.println(attackingUnit);
            entry =
                new LogEntry(
                    (Integer[]) msg.get("vclock"),
                    LogEntryType.ATACK,
                    (InetSocketAddress) msg.get("address"),
                    new Position(attackingUnit.getX(), attackingUnit.getY()),
                    new Position(x, y),
                    (Integer) msg.get("damage"));
            logger.writeAsText(entry, true);
            if (!((InetSocketAddress) msg.get("serverAddress"))
                .equals(new InetSocketAddress(url, port))) {
              vClock.updateClock((Integer[]) msg.get("vclock"));
            }

            if (unit.getHitPoints() <= 0) {
              removeUnit(x, y);
              // Log remove unit
              // Should we log with same clock as deal damage that cause it?
              entry =
                  new LogEntry(
                      vClock.getClock(),
                      LogEntryType.REMOVE,
                      (InetSocketAddress) msg.get("address"),
                      new Position((Integer) msg.get("x"), (Integer) msg.get("y")));
              logger.writeAsText(entry, true);
            }
          }
          break;
        }
      case healDamage:
        {
          int x = (Integer) msg.get("x");
          int y = (Integer) msg.get("y");
          unit = this.getUnit(x, y);
          if (unit != null) unit.adjustHitPoints((Integer) msg.get("healed"));
          /* Copy the id of the message so that the unit knows
           * what message the battlefield responded to.
           */
          Unit attackingUnit = (Unit) msg.get("unit");

          entry =
              new LogEntry(
                  (Integer[]) msg.get("vclock"),
                  LogEntryType.HEAL,
                  (InetSocketAddress) msg.get("address"),
                  new Position(attackingUnit.getX(), attackingUnit.getY()),
                  new Position((Integer) msg.get("x"), (Integer) msg.get("y")),
                  (Integer) msg.get("healed"));
          logger.writeAsText(entry, true);
          if (!((InetSocketAddress) msg.get("serverAddress"))
              .equals(new InetSocketAddress(url, port))) {
            vClock.updateClock((Integer[]) msg.get("vclock"));
          }
          break;
        }
      case moveUnit:
        {

          // System.out.println("BATTLEFIELD: MOVEUNIT");
          Unit tempUnit = units.get((InetSocketAddress) msg.get("address"));
          int x = tempUnit.getX();
          int y = tempUnit.getY();

          /*
          if(temptUnit == null) {
          	System.out.println("NULL");
          }*/

          boolean move = this.moveUnit(tempUnit, (Integer) msg.get("x"), (Integer) msg.get("y"));
          if (!move) System.out.println("MOVE CANCELED");

          entry =
              new LogEntry(
                  (Integer[]) msg.get("vclock"),
                  LogEntryType.MOVE,
                  (InetSocketAddress) msg.get("address"),
                  new Position(x, y),
                  new Position((Integer) msg.get("x"), (Integer) msg.get("y")));
          logger.writeAsText(entry, true);

          if (!((InetSocketAddress) msg.get("serverAddress"))
              .equals(new InetSocketAddress(url, port))) {
            vClock.updateClock((Integer[]) msg.get("vclock"));
          }
          /* Copy the id of the message so that the unit knows
           * what message the battlefield responded to.
           */
          break;
        }
      default:
        break;
    }
    return null;
  }
Exemplo n.º 3
0
  public Message onMessageReceived(Message msg) {

    // System.out.println("MESSAGE RECEIVED:" + msg.get("request"));

    // System.out.println("MESSAGE RECEIVED " + (MessageRequest)msg.get("request"));

    if ((Boolean) msg.get("sync") != null && (Boolean) msg.get("sync") == true) {
      // System.out.println("SYNC MESSAGE RECEIVED " + (MessageRequest)msg.get("request"));
      processSyncMessage(msg);

    } else {
      MessageRequest request = (MessageRequest) msg.get("request");
      Message reply = null;
      String origin = (String) msg.get("origin");
      Unit unit;
      Integer[] tempClock;
      Message replyMessage;
      LogEntry entry;
      switch (request) {
        case disconnectedUnit:
          Unit u;
          synchronized (this) {
            u = units.remove((InetSocketAddress) msg.get("unitAddress"));
            if (u != null) map[u.getX()][u.getY()] = null;
          }
          tempClock = ((Integer[]) msg.get("vclock")).clone();
          entry =
              new LogEntry(
                  tempClock,
                  LogEntryType.DISCONNECTED_UNIT,
                  (InetSocketAddress) msg.get("serverAddress"));
          logger.writeAsText(entry, true);
          replyMessage = new Message();
          replyMessage.put("request", MessageRequest.disconnectedUnitAck);
          replyMessage.put("serverAddress", new InetSocketAddress(url, port));
          SynchronizedClientSocket client =
              new SynchronizedClientSocket(
                  replyMessage, (InetSocketAddress) msg.get("serverAddress"), this);
          client.sendMessage();
          break;

        case disconnectedBF:
          tempClock = ((Integer[]) msg.get("vclock")).clone();
          entry =
              new LogEntry(
                  tempClock,
                  LogEntryType.DISCONNECTED_BF,
                  (InetSocketAddress) msg.get("serverAddress"));
          logger.writeAsText(entry, true);
          replyMessage = new Message();
          replyMessage.put("request", MessageRequest.disconnectedBFAck);
          replyMessage.put("serverAddress", new InetSocketAddress(url, port));
          SynchronizedClientSocket syncClient =
              new SynchronizedClientSocket(
                  replyMessage, (InetSocketAddress) msg.get("serverAddress"), this);
          syncClient.sendMessage();
          break;
        case spawnUnit:
        case moveUnit:
        case dealDamage:
        case healDamage:
          syncActionWithBattlefields(msg);
          break;
        case requestBFList:
          {
            reply = new Message();
            reply.put("request", MessageRequest.replyBFList);
            battlefields.put((InetSocketAddress) msg.get("bfAddress"), 0);
            reply.put("bfList", battlefields);
            return reply;
          }

        case replyBFList:
          {
            HashMap<InetSocketAddress, Integer> bfList =
                (HashMap<InetSocketAddress, Integer>) msg.get("bfList");
            for (InetSocketAddress address : bfList.keySet()) {
              battlefields.put(address, 0);
            }
            for (InetSocketAddress address : battlefields.keySet()) {
              SynchronizedClientSocket clientSocket;
              Message message = new Message();
              message.put("request", MessageRequest.addBF);
              message.put("bfAddress", new InetSocketAddress(url, port));
              clientSocket = new SynchronizedClientSocket(message, address, this);
              clientSocket.sendMessage();
            }
            // System.out.println("BATTLEFIELDS:"+ bfList.toString());

            // reply = new Message();
            // HashSet bfList = (HashSet<InetSocketAddress>)msg.get("bfList");
            // int y = (Integer)msg.get("y");
            // reply.put("id", msg.get("id"));
            return null;
          }

        case addBF:
          {
            battlefields.put((InetSocketAddress) msg.get("bfAddress"), 0);
            // System.out.println("ADD BF:"+ battlefields.toString());

            return null;
          }

          // break;
          // case removeUnit:
          // this.removeUnit((Integer)msg.get("x"), (Integer)msg.get("y"));
          // if(syncBF(msg)) return null;

        case SyncActionResponse:
          processResponseMessage(msg);

          break;
        case SyncActionConfirm:
          return processConfirmMessage(msg);
      }
    }
    return null;

    // serverSocket.sendMessage(reply, origin);
    /*
    try {
    	if (reply != null)
    		serverSocket.sendMessage(reply, origin);
    }
    /*catch(IDNotAssignedException idnae)  {
    	// Could happen if the target already logged out
    }*/
  }