示例#1
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;
  }
示例#2
0
 private void sendSyncMessage(Message message) {
   SynchronizedClientSocket clientSocket;
   message.put("sync", (Boolean) true);
   message.put("serverAddress", new InetSocketAddress(url, port));
   message.put("serverMessageID", localMessageCounter);
   for (InetSocketAddress address : battlefields.keySet()) {
     if (address.equals(new InetSocketAddress(url, port))) continue;
     clientSocket = new SynchronizedClientSocket(message, address, this);
     clientSocket.sendMessage();
   }
 }
示例#3
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();
  }
示例#4
0
  private void synchronizeWithAllBF(Message messageToSend) {

    // SyncLog syncLog = new SyncLog();
    SynchronizedClientSocket syncClientSocket;

    for (InetSocketAddress address : battlefields.keySet()) {
      if (address.getHostName().equals(url) && address.getPort() == port) continue;
      syncClientSocket = new SynchronizedClientSocket(messageToSend, address, this);
      syncClientSocket.sendMessage();
    }

    /*
    		//Assume that it always gets a response from at least one of the GS
    		if(gridSchedulersList.size() > 1)
    			syncLog.check();
    */
  }
示例#5
0
 /**
  * @param message
  * @return true if message is already a sync message, or false if the if it was not a sync message
  *     and it was propagated.
  */
 private boolean syncBF(Message message) {
   for (InetSocketAddress address : battlefields.keySet()) {
     if (address.equals(new InetSocketAddress(url, port))) continue;
     message.put("sync", (Boolean) true);
     String s =
         "[S"
             + port
             + "] SENDING SYNC MESSAGE\nBefore change: "
             + message.get("address")
             + "\nAfter Change: ";
     message.put("address", new InetSocketAddress(url, port));
     s += message.get("address");
     // System.out.println(s);
     // System.out.println("####################");
     SynchronizedClientSocket clientSocket;
     clientSocket = new SynchronizedClientSocket(message, address, this);
     clientSocket.sendMessage();
     // messageList.put(message, 0);
   }
   return false;
 }
示例#6
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
    }*/
  }