Package cz.matfyz.aai.fantom.server

Examples of cz.matfyz.aai.fantom.server.ProtocolException


          if(moves[0] == null) {
            moves[0] = m;
          } else if(moves[1] == null && mover.hasDoubleMove()) {
            moves[1] = m;
          } else {
            throw new ProtocolException("Too many moves for a single actor.", null);
          }
        }
      }

      if(!isMobile(mover, actors)) {
        immobile.add(mover);
      }

      for(MessageMoveBase.ActorMove move : moves) {
        if(move == null) {
          break;
        }

        assert(mover.equals(move.getActor()));

        if(!isMobile(mover, actors)) {
          throw new ProtocolException("Actor is immobile: " + mover.getId(), null);
        }

        Graph.Node target = move.getTargetNode();
        TransportType tt = move.getTransportType();

        for(Actor a : getActors()) {
          assert(a.getCurrentPosition() != null);
          // Note: It may be possible to move back to the origin.
          if(a.getCurrentPosition() == target
              && !a.equals(mover)
              && a.isDetective() == mover.isDetective()) { // Cannot place two actors of the same type to the same node.
            String errorMessage = String.format("Cannot place actor '%s' to node '%s', node is already occupied by actor '%s'",
                mover.getId(), target.getId(), a.getId());
            throw new ProtocolException(errorMessage, null);
          }
        }

        mover.moveTo(target, tt);
        didMove = true;
      }

      if(didMove) {
        moved.add(mover);
      }
    }

    if(moved.size() + immobile.size() != actors.size()) {
      StringBuilder sb = new StringBuilder();
      for(Actor a : actors) {
        if(!moved.contains(a) && !immobile.contains(a)) {
          if(sb.length() > 0) sb.append(", ");
          sb.append(a.getId());
        }
      }
      throw new ProtocolException("Some actor did not move: " + sb, null);
    }

    return immobile;
  }
View Full Code Here


      for(Actor a : getActors()) {
        if(a.getCurrentPosition() != null
          && a.getCurrentPosition().equals(target)
          && a.isDetective() == actor.isDetective()) { // Cannot place two actors of the same type to the same node.
          throw new ProtocolException("Cannot place an actor to an already full node.", null);
        }
      }

      actor.setCurrentPosition(target);
    }
View Full Code Here

    HashSet<Actor> allActors = new HashSet<Actor>(getActors(client.getClientType()));

    for(ActorMove move : message.getMoves()) {
      Actor actor = move.getActor();
      if(actor.getCurrentPosition() != null)
        throw new ProtocolException("Position was already assigned to actor: " + actor.getId(), client);
      if(placedActors.contains(actor))
        throw new ProtocolException("Position was specified multiple times to actor: " + actor.getId(), client);
      if(!allActors.contains(actor))
        throw new ProtocolException("Client placed other player's actor: " + actor.getId(), client);
      placedActors.add(actor);
      if(move.getTargetNode() == null)
        throw new ProtocolException("The target node was not specified for actor: " + actor.getId(), client);
      if(move.getTargetNode().isOccupied(client.getClientType()))
        throw new ProtocolException(String.format("Actor %s cannot be placed to node %s, the node is already occupied",
                      actor.getId(), move.getTargetNode().getId()),
                client);
      if(move.getTransportType() != null)
        throw new ProtocolException("Transport type was specified in the placement message for actor: " + actor.getId(), client);
      if(usedNodes.contains(move.getTargetNode()))
        throw new ProtocolException(String.format("Actor %s cannot be placed to node %s, the node is already occupied",
                            actor.getId(), move.getTargetNode().getId()),
                      client);
      usedNodes.add(move.getTargetNode());
    }
   
    for(Actor actor : allActors) {
      if(!placedActors.contains(actor))
        throw new ProtocolException("Actor was not placed: " + actor.getId(), client);
    }
  }
View Full Code Here

   * @returns information about the movement of the actor.
   */
  public PositionChange moveTo(Node target, TransportType transport) throws ProtocolException {
    Node origin = getCurrentPosition();
    if(!canMoveTo(target, transport))
      throw new ProtocolException("Cannot perform the requested move", null);
   
    // Use one ticket for the selected transport type
    addTickets(transport, -1);
    // Update position of the actor
    this.currentPosition = target;
View Full Code Here

      if(properties == null)
        throw new IllegalArgumentException("properties must not be null");
     
      String actorId = properties.getProperty(PROPERTY_ACTOR);
      if(actorId == null || actorId.isEmpty())
        throw new ProtocolException("No actor ID was specified", null);
      this.actor = graph.getActor(actorId);
      if(this.actor == null)
        throw new ProtocolException("The actor '" + actorId + "' does not exist", null);
     
      String targetId = properties.getProperty(PROPERTY_TO_NODE);
      if(targetId != null && !targetId.isEmpty()) {
        this.targetNode = graph.getNode(targetId);
        if(this.targetNode == null)
          throw new ProtocolException("The node '" + targetId + "' does not exist", null);
      }
     
      String transportName = properties.getProperty(PROPERTY_TRANSPORT_TYPE);
      if(transportName != null && !transportName.isEmpty()) {
        this.transportType = graph.getTransportType(transportName);
        if(this.transportType == null)
          throw new ProtocolException("The transport type '" + transportName + "' does not exist", null);
      }
      else if(targetNode == null)
        throw new ProtocolException("Neither transport type nor target node was not specified", null);
    }
View Full Code Here

        messageType = messageData[i].getProperty(PROPERTY_MESSAGE_TYPE);
        break;
      }
    }
    if(messageType == null)
      throw new ProtocolException("Message type was not specified", client);
   
    MessageLoader loader = messageLoaders.get(messageType);
    if(loader == null)
      throw new ProtocolException("Unknown message type: '" + messageType + "'", client);
   
    return loader.loadMessage(messageData, client, graph);
  }
View Full Code Here

            sendMoves();
         
          while(true) {
            msg = receiveMessage(graph);
            if(!(msg instanceof MessageUpdate))
              throw new ProtocolException(String.format("Message 'update' was expected, got '%s'", msg.getMessageType()), null);

            MessageUpdate msgUpdate = (MessageUpdate)msg;
           
            // Update the agent, if nobody did win the game in the last round, or if there
            // is information about movements of the other agent (e.g. about the movement
            // of the detectives, that captured the phantom, or the movement of the phantom
            // that commited suicide).
            if(msgUpdate.getWinner() == null || msgUpdate.getMoves().length > 0)
              receiveUpdate(msgUpdate);
            // If there is a winner, end the round
            if(msgUpdate.getWinner() != null) {
              agent.end(msgUpdate.getWinner());
              continue tournament_loop;
            }
           
            sendMoves();
          }
        }
        else if(msg instanceof MessageQuit) {
          agent.quit();
          return;
        }
        else {
          throw new ProtocolException("Unexpected message type: " + msg.getMessageType(), null);
        }
      }
    }
    catch(Throwable e) {
      e.printStackTrace();
View Full Code Here

        break;
      }
    }
   
    if(header == null)
      throw new ProtocolException("The message header was not found", null);
   
    this.clientName = header.getProperty(PROPERTY_CLIENT_NAME);
    if(this.clientName == null || this.clientName.isEmpty())
      throw new ProtocolException("The client name was not specified", null);
   
    String clientTypeStr = header.getProperty(PROPERTY_CLIENT_TYPE);
    if(clientTypeStr == null || clientTypeStr.isEmpty())
      throw new ProtocolException("The client type was not specified", null);
   
    if(clientTypeStr.equals(PROPERTY_CLIENT_TYPE_DETECTIVE))
      this.clientType = ClientType.DETECTIVE;
    else if(clientTypeStr.equals(PROPERTY_CLIENT_TYPE_PHANTOM))
      this.clientType = ClientType.PHANTOM;
    else
      throw new ProtocolException("Invalid client type: '" + clientTypeStr + "'", null);
  }
View Full Code Here

    ArrayList<ActorTickets> tickets = new ArrayList<ActorTickets>();
    for(Properties record : messageData) {
      if(record.containsKey(PROPERTY_CAPTURED)) {
        String actorId = record.getProperty(PROPERTY_CAPTURED);
        if(actorId == null || actorId.isEmpty())
          throw new ProtocolException("Actor ID was not specified", null);
        Actor capturedActor = graph.getActor(actorId);
        if(capturedActor == null)
          throw new ProtocolException("Unknown actor ID: " + actorId, null);
        capturedPhantoms.add(capturedActor);
      }
      else if(record.containsKey(PROPERTY_TICKETS)) {
        ActorTickets aticket = new ActorTickets(graph, record);
        tickets.add(aticket);
      }
      else if(record.containsKey(PROPERTY_MESSAGE_TYPE) && record.containsKey(PROPERTY_WINNER)) {
        String winnerStr = record.getProperty(PROPERTY_WINNER);
        try {
          this.winner = ClientType.valueOf(winnerStr);
        }
        catch(IllegalArgumentException e) {
          throw new ProtocolException(e.getMessage(), null);
        }
      }
    }
    this.actorTickets = tickets.toArray(new ActorTickets[tickets.size()]);   
  }
View Full Code Here

     * @throws ProtocolException if there is a problem when loading the data.
     */
    protected void loadProperties(Graph graph, Properties record) throws ProtocolException {
      String actorId = record.getProperty(PROPERTY_TICKETS);
      if(actorId == null || actorId.isEmpty())
        throw new ProtocolException("No actor ID was specified", null);
      this.actor = graph.getActor(actorId);
      if(this.actor == null)
        throw new ProtocolException("Unknown actor ID: " + actorId, null);
     
      if(record.containsKey(Graph.PROPERTY_DOUBLE_TICKET)) {
        String countStr = record.getProperty(Graph.PROPERTY_DOUBLE_TICKET);
        try {
          this.doubleTickets = Integer.parseInt(countStr);
        }
        catch(NumberFormatException e) {
          throw new ProtocolException("Number of double tickets is not a valid number", null);
        }
      }
     
      this.tickets = new HashMap<TransportType, Integer>();
      for(Entry<Object, Object> prop : record.entrySet()) {
        String key = (String)prop.getKey();
        String countStr = (String)prop.getValue();
       
        TransportType ttype = graph.getTransportType(key);
        if(ttype == null)
          continue;
       
        try {
          int count = Integer.parseInt(countStr);
          tickets.put(ttype, count);
        }
        catch(NumberFormatException e) {
          throw new ProtocolException("The number of tickets is not a valid number", null);
        }
      }
    }
View Full Code Here

TOP

Related Classes of cz.matfyz.aai.fantom.server.ProtocolException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.