Ejemplo n.º 1
0
  private static void serverWaiting() throws Exception {
    System.out.println("Waiting Mode");
    ServerSocket waitingSocket = new ServerSocket(serverInfo.getPort());
    String line;

    while (true) {
      Socket connectionSocket = waitingSocket.accept();
      BufferedReader in =
          new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
      DataOutputStream out = new DataOutputStream(connectionSocket.getOutputStream());
      // System.out.println("Waiting for migration");
      line = in.readLine();
      // System.out.println("Received: " + line);
      String info = line;
      out.writeBytes("Info OK\n");
      line = in.readLine();
      // System.out.println("Received: " + line);
      serverInit();
      game.setMoving(true);
      reconstruction(info);
      out.writeBytes("Bind OK\n");
      waitingSocket.close();
      break;
    }
    while (!allConnected) {
      System.out.print("");
    }
    game.setMoving(false);
    // System.out.println("Fin server waiting");
    serverRunning();
  }
Ejemplo n.º 2
0
  public static String infoComplete() {
    String info = "";
    try {
      // Board
      info = infoGame();

      // Players
      ArrayList<I_InfoPlayer> players = game.getPlayers();
      for (int i = 0; i < players.size(); i++) {
        info += "%%%" + infoPlayer(players.get(i));
      }
      info += "%%%%%";
      info += Integer.toString(ghostdx[0]);
      for (int i = 1; i < game.getNrofghosts(); i++) {
        info += " " + Integer.toString(ghostdx[i]);
      }
      for (int i = 0; i < game.getNrofghosts(); i++) {
        info += " " + Integer.toString(ghostdy[i]);
      }
      for (int i = 0; i < game.getNrofghosts(); i++) {
        info += " " + Integer.toString(ghostspeed[i]);
      }

    } catch (Exception exc) {
      exc.printStackTrace();
    }
    return info;
  }
Ejemplo n.º 3
0
  public void LevelContinue() {
    short i;
    int dx = 1;
    int random;

    try {
      int blocksize = game.getBlocksize();
      int[] ghosty = game.getGhosty();
      int[] ghostx = game.getGhostx();
      int nrofghosts = game.getNrofghosts();

      for (i = 0; i < nrofghosts; i++) {
        ghosty[i] = 4 * blocksize;
        ghostx[i] = 4 * blocksize;
        ghostdy[i] = 0;
        ghostdx[i] = dx;
        dx = -dx;
        random = (int) (Math.random() * (currentspeed + 1));
        if (random > currentspeed) random = currentspeed;
        ghostspeed[i] = validspeeds[random];
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 4
0
  public void LevelInit() {
    try {
      for (int i = 0; i < game.getNrofblocks() * game.getNrofblocks(); i++)
        game.setScreendata(leveldata[i], i);
      LevelContinue();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 5
0
  public Server() {

    game.setServer(this);
    game.InitScreenData();
    game.Initghostx(new int[maxghosts]);
    ghostdx = new int[maxghosts];
    game.Initghosty(new int[maxghosts]);
    ghostdy = new int[maxghosts];
    ghostspeed = new int[maxghosts];
    dx = new int[4];
    dy = new int[4];

    numberFinished = 0;
  }
Ejemplo n.º 6
0
 public void finished() {
   int numberPlayer = game.getNumberPlayer();
   numberFinished += 1;
   if (numberFinished == numberPlayer) {
     // Ghosts update
     int nrOfGhosts = game.getNrofghosts();
     if (nrOfGhosts < maxghosts) {
       game.setNrofghosts(nrOfGhosts++);
     }
     if (currentspeed < maxspeed) {
       currentspeed++;
     }
     LevelInit();
   }
 }
Ejemplo n.º 7
0
 public static void serverConstruction(String s) {
   String[] infos = s.split(" ");
   int cont = 0;
   for (int i = 0; i < game.getNrofghosts(); i++) {
     ghostdx[i] = Integer.valueOf(infos[cont]);
     cont++;
   }
   for (int i = 0; i < game.getNrofghosts(); i++) {
     ghostdy[i] = Integer.valueOf(infos[cont]);
     cont++;
   }
   for (int i = 0; i < game.getNrofghosts(); i++) {
     ghostspeed[i] = Integer.valueOf(infos[cont]);
     cont++;
   }
 }
Ejemplo n.º 8
0
  private static void recuperacionFallas() {
    File file = new File(serverName + ".swp");
    if (file.exists()) {
      try {
        System.out.println("Un fichero de salvaguarda es presente:");
        System.out.println("Quiere recuperar el antiguo juego(y/n)?");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
          String s = in.readLine();
          if (s.equals("y")) {
            recuperationServer();
            InputStream ips = new FileInputStream(serverName + ".swp");
            InputStreamReader ipsr = new InputStreamReader(ips);
            BufferedReader br = new BufferedReader(ipsr);
            String info = br.readLine();
            reconstruction(info);
            game.setRecoveringServer(false);
            break;
          } else if (s.equals("n")) {
            serverInit();
            break;
          }
        }

      } catch (FileNotFoundException e) {
        System.out.println("Failed to wait");
      } catch (IOException e) {
        System.out.println("Failed to wait");
      }

    } else serverInit();
    saveStateServer();
  }
Ejemplo n.º 9
0
 public static void serverInit() {
   String serverAddress = "rmi://" + serverInfo.getIpAddr() + ":1099/I_InfoGame";
   try {
     game = new InfoGame();
     server = new Server();
     game.setServer(server);
     game.setServerAddress(serverInfo.getIpAddr());
     game.setNextAddress(serverInfo.getNext().getIpAddr());
     game.setNbPlayerExpected(numPlayer);
     Naming.rebind(serverAddress, game);
     socketPortClient = serverInfo.getPortClient();
     // System.out.println("End initialisation");
   } catch (RemoteException e) {
     System.out.println("Hubo una excepcion creando la instancia del objeto distribuido");
   } catch (MalformedURLException e) {
     System.out.println("URL mal formada al tratar de publicar el objeto");
   }
 }
Ejemplo n.º 10
0
 public static void setDiscussion() {
   discussion = new ServerChat(socketPortClient);
   Thread thread = new Thread(discussion);
   thread.start();
   try {
     game.setUnbind(true);
   } catch (RemoteException e) {
     System.out.println("Failed setUnbind on game");
   }
 }
Ejemplo n.º 11
0
  public static void reconstruction(String s) {
    try {
      String[] infos = s.split("%%%%%");

      game.construction(infos[0]);
      serverConstruction(infos[1]);
    } catch (RemoteException e) {
      System.out.println("Failed during reconstruction of the game");
    }
  }
Ejemplo n.º 12
0
 public void GameInit() {
   if (!isInitialized) {
     LevelInit();
     try {
       game.setNrofghosts(6);
     } catch (Exception e) {
       e.printStackTrace();
     }
     currentspeed = 3;
   }
 }
Ejemplo n.º 13
0
 private static void serverRunning() {
   System.out.println("Server Running");
   ServerTimer timer = new ServerTimer(60);
   Thread thread = new Thread(timer);
   thread.start();
   // System.out.println("Waiting for timer");
   while (!timer.finished()) ;
   timer.shutDown();
   // System.out.println("Migration !");
   timer = null;
   System.gc();
   try {
     game.setMoving(true);
     serverCall();
   } catch (RemoteException e) {
     System.out.println("Failed to modify game");
     System.exit(1);
   } catch (Exception ex) {
     System.out.println("Failed to call");
   }
 }
Ejemplo n.º 14
0
  private static void serverCall() throws Exception {
    String sentence = "move";
    String line;

    while (!gameStopped) {
      System.out.print("");
    }
    playing = false;
    try {
      setDiscussion();
    } catch (Exception ex) {
      System.out.println("Failed to discuss with clients");
    }

    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    try {
      InetAddress inetAddress = InetAddress.getByName(serverInfo.getNext().getIpAddr());
      Socket clientSocket = new Socket(inetAddress, serverInfo.getNext().getPort());
      DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
      BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
      sendInfo(out);
      line = in.readLine();
      while (!game.getUnbinding()) ;
      Naming.unbind("rmi://" + serverInfo.getIpAddr() + ":1099/I_InfoGame");
      out.writeBytes("Unbinding finished\n");
      line = in.readLine();
      clientSocket.close();
    } catch (RemoteException e) {
      System.out.println("Failed get info from game");
      System.exit(1);
    } catch (Exception ex) {
      System.out.println("Failed to socket with waiting server");
    }
    discussion.shutDown();
    discussion = null;
    game = null;
    System.gc();
    serverWaiting();
  }
Ejemplo n.º 15
0
 /*
  *function who transformed all the caracteristic of InfoGame in a string
  *to sent them at the new server
  */
 public static String infoGame() {
   String info = "";
   try {
     info = Integer.toString(game.getNrofghosts());
     info += " " + Integer.toString(game.getDeathcounter());
     int[] ghostx = game.getGhostx();
     for (int i = 0; i < game.getNrofghosts(); i++) {
       info += " " + Integer.toString(ghostx[i]);
     }
     int[] ghosty = game.getGhosty();
     for (int i = 0; i < game.getNrofghosts(); i++) {
       info += " " + Integer.toString(ghosty[i]);
     }
     short[] screenData = game.getScreendata();
     for (int i = 0; i < screenData.length; i++) {
       info += " " + Integer.toString(screenData[i]);
     }
     info += " " + Integer.toString(game.getNumberPlayer());
     info += " " + Integer.toString(game.getNbPlayerExpected());
     info += " " + Integer.toString(game.getNbPlayerWaiting());
     info += " " + Integer.toString(game.getNbrPlayerInGame());
     info += " " + String.valueOf(game.isPlaying());
     info += " " + String.valueOf(game.isWaiting());
     info += " " + String.valueOf(game.isEnded());
     info += " " + String.valueOf(game.isPause());
     info += " " + game.getPlayerCallPause();
   } catch (Exception exc) {
     exc.printStackTrace();
   }
   return info;
 }
Ejemplo n.º 16
0
  public void moveGhosts() {
    saveStateServer(); // We save the configuration of the game
    try {
      int nbrPlayerInGame = game.getNbrPlayerInGame();
      int[] ghosty = game.getGhosty();
      int[] ghostx = game.getGhostx();
      if (ghostMove == 0) {
        short i;
        int pos;
        int count;
        int blocksize = game.getBlocksize();
        int nrofblocks = game.getNrofblocks();
        int nrofghosts = game.getNrofghosts();
        short[] screendata = game.getScreendata();

        for (i = 0; i < nrofghosts; i++) {
          if (ghostx[i] % blocksize == 0 && ghosty[i] % blocksize == 0) {
            pos = ghostx[i] / blocksize + nrofblocks * (int) (ghosty[i] / blocksize);

            count = 0;
            if ((screendata[pos] & 1) == 0 && ghostdx[i] != 1) {
              dx[count] = -1;
              dy[count] = 0;
              count++;
            }
            if ((screendata[pos] & 2) == 0 && ghostdy[i] != 1) {
              dx[count] = 0;
              dy[count] = -1;
              count++;
            }
            if ((screendata[pos] & 4) == 0 && ghostdx[i] != -1) {
              dx[count] = 1;
              dy[count] = 0;
              count++;
            }
            if ((screendata[pos] & 8) == 0 && ghostdy[i] != -1) {
              dx[count] = 0;
              dy[count] = 1;
              count++;
            }

            if (count == 0) {
              if ((screendata[pos] & 15) == 15) {
                ghostdx[i] = 0;
                ghostdy[i] = 0;
              } else {
                ghostdx[i] = -ghostdx[i];
                ghostdy[i] = -ghostdy[i];
              }
            } else {
              count = (int) (Math.random() * count);
              if (count > 3) count = 3;
              ghostdx[i] = dx[count];
              ghostdy[i] = dy[count];
            }
          }
          ghostx[i] = ghostx[i] + (ghostdx[i] * ghostspeed[i]);
          ghosty[i] = ghosty[i] + (ghostdy[i] * ghostspeed[i]);
        }
      }
      ghostMove = (ghostMove + 1) % nbrPlayerInGame;
      game.setGhostx(ghostx);
      game.setGhosty(ghosty);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }