Esempio n. 1
0
  private void sendActionAck(
      Message message, boolean valid, Integer messageID, InetSocketAddress address) {

    Message toSend = new Message();
    toSend = message.clone();
    toSend.put("request", MessageRequest.SyncActionResponse);
    toSend.put("serverAddress", (InetSocketAddress) new InetSocketAddress(url, port));
    toSend.put("ack", (Boolean) valid);

    SynchronizedClientSocket socket = new SynchronizedClientSocket(toSend, address, this);
    socket.sendMessage();
  }
Esempio n. 2
0
  private synchronized Message processResponseMessage(Message msg) {
    Integer messageID = (Integer) msg.get("serverMessageID");
    ActionInfo actionInfo = pendingOwnActions.get(messageID);
    InetSocketAddress serverAddress = (InetSocketAddress) msg.get("serverAddress");

    Message message = msg.clone();
    message.put("request", MessageRequest.SyncActionConfirm);
    message.put("serverAddress", new InetSocketAddress(url, port));
    message.put("serverMessageID", messageID);

    if (actionInfo != null) {
      if ((Boolean) msg.get("ack")) {
        // System.out.println("[S"+port+"] "+actionInfo.message.get("address")+" ACK TRUE from
        // "+serverAddress.getHostName()+":"+serverAddress.getPort()+" Adding info to queue.");
        actionInfo.ackReceived.add((InetSocketAddress) msg.get("serverAddress"));
        if (actionInfo.ackReceived.size() == battlefields.size() - 1) {
          message.put("confirm", true);
          Integer[] tempClock = vClock.incrementClock(id);
          // System.out.println("<"+url+":"+port+"> Clock added when action is ready to ship -->
          // "+toStringArray(tempClock));
          message.put("vclock", tempClock);
          for (InetSocketAddress address : actionInfo.ackReceived) {
            SynchronizedClientSocket clientSocket =
                new SynchronizedClientSocket(message, address, this);
            clientSocket.sendMessage();
          }
          ActionInfo removeAction = pendingOwnActions.remove(messageID);
          removeAction.timer.cancel();
          msg.put("vclock", tempClock);
          Message toPlayer = processEvent(message, removeAction);
          if (toPlayer != null) {
            SynchronizedClientSocket clientSocket =
                new SynchronizedClientSocket(
                    toPlayer, (InetSocketAddress) msg.get("address"), this);
            clientSocket.sendMessage();
          }
        }
      } else {
        pendingOwnActions.remove(messageID).timer.cancel();
        message.put("confirm", false);
        SynchronizedClientSocket clientSocket =
            new SynchronizedClientSocket(message, serverAddress, this);
        clientSocket.sendMessage();
      }

    } else {
      message.put("confirm", false);
      SynchronizedClientSocket clientSocket =
          new SynchronizedClientSocket(message, serverAddress, this);
      clientSocket.sendMessage();
    }
    return null;
  }