Esempio n. 1
1
  // --------------------------------actionConnect------------------------------
  private void actionConnect() {
    if (oParty == null) {
      JOptionPane.showMessageDialog(frame, "Make a party before trying to connect.");
      return;
    }

    String[] oResults = (String[]) DialogManager.show(DialogManager.CONNECT, frame);

    if (oResults[DialogManager.RETURN_IP].equals("cancel")) return;

    lblStatus3.setText("Connecting...");
    try {
      oConn.connect(
          oResults[DialogManager.RETURN_IP], Integer.parseInt(oResults[DialogManager.RETURN_PORT]));
    } catch (UnknownHostException e) {
      JOptionPane.showMessageDialog(
          frame,
          "The IP of the host cannot be determined.",
          "Unknown Host Exception",
          JOptionPane.ERROR_MESSAGE);
      frame.repaint();
      return;
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          frame, e.getMessage(), "Input/Output Exception", JOptionPane.ERROR_MESSAGE);
      frame.repaint();
      return;
    }
    echo("Connected to opponent!");

    tConn = new Thread(oConn, "conn");
    tConn.start();
    tMain = new Thread(this, "main");
    tMain.start();
  }
Esempio n. 2
1
  // --------------------------------actionHost---------------------------------
  private void actionHost() {
    if (oParty == null) {
      JOptionPane.showMessageDialog(frame, "Make a party before trying to connect.");
      return;
    }

    JFileChooser oFC = new JFileChooser(DEFAULT_MAP_DIRECTORY);
    int nReturn = oFC.showOpenDialog(frame);

    if (nReturn == JFileChooser.CANCEL_OPTION) {
      return;
    }

    int nPort = Integer.parseInt((String) (DialogManager.show(DialogManager.HOST, frame)));

    JDialog dlgBox = (JDialog) (DialogManager.show(DialogManager.WAITING_FOR_CONN, frame));
    dlgBox.pack();
    try {
      oConn.host(nPort);
    } catch (IOException e) {
      JOptionPane.showMessageDialog(frame, e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE);
      frame.repaint();
      return;
    }
    dlgBox.setVisible(false);
    echo("Connected to opponent!");

    tConn = new Thread(oConn, "conn");
    tConn.start();
    tMain = new Thread(this, "main");
    tMain.start();

    try {
      oMap.loadMap(oFC.getSelectedFile());
      oConn.send("loadmap", oFC.getName(oFC.getSelectedFile()));
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
    }
  }
Esempio n. 3
1
  public void run() {
    String[] sData;
    while (oConn.isConnected()) {
      sData = oConn.getData();
      System.out.println("Received \"" + sData[0] + "\" command.");

      if (sData[0].equals("chat")) panChat.addChat(sData[1]);
      else if (sData[0].equals("loadmap")) {
        try {
          oMap.loadMap(new File(DEFAULT_MAP_DIRECTORY + sData[1]));
          int nRand = (int) Math.floor((Math.random() + 1) + 0.5);
          stateDeploy(nRand);
          if (nRand == 1) nRand = 2;
          else nRand = 1;
          oConn.send("deploy", "" + nRand);
        } catch (FileNotFoundException e) {
          oConn.send("error", "Opponent does not have map.");
          actionDisconnect();
        }
      } else if (sData[0].equals("deploy")) {
        stateDeploy(Integer.parseInt(sData[1]));
      } else if (sData[0].equals("error")) {
        JOptionPane.showMessageDialog(frame, sData[1]);
        actionDisconnect();
      } else if (sData[0].equals("deployed")) {
        bOpponentDeployed = true;
        if (bDeployed) {
          oConn.send("trade parties", "");
          tradeParties();
          makeAvatarOrder();
          long nRandSeed = Math.round((Math.random() * Long.MAX_VALUE));
          oRandom = new Random(nRandSeed);
          oConn.send("random seed", "" + nRandSeed);
          oConn.send("phase", "1");
          phase1();
        }
      } else if (sData[0].equals("trade parties")) {
        tradeParties();
      } else if (sData[0].equals("avatar order")) {
        makeAvatarOrder(sData[1]);
      } else if (sData[0].equals("random seed")) {
        oRandom = new Random(Long.parseLong(sData[1]));
      } else if (sData[0].equals("phase")) {
        switch (Integer.parseInt(sData[1])) {
          case 1:
            phase1();
            break;
          case 2:
            phase2();
            break;
          case 3:
            phase3();
            break;
          case 4:
            iChar = 0;
            bMyAT = false;
            phase4();
            break;
        }
      } else if (sData[0].equals("move")) {
        oMap.moveATAvatar(Integer.parseInt(sData[1]), Integer.parseInt(sData[2]));
        // TEMP
        stateEndTurn();
      }
    }
  }
Esempio n. 4
0
 public void makeAvatarOrder() {
   int nRand = (int) Math.floor((Math.random() + 1) + 0.5);
   String sOrder = "A,0|B,0|A,1|B,1|A,2|B,2|A,3|B,3|A,4|B,4";
   String sOrder2 = "B,0|A,0|B,1|A,1|B,2|A,2|B,3|A,3|B,4|A,4";
   if (nRand == 1) {
     makeAvatarOrder(sOrder);
     oConn.send("avatar order", sOrder2);
   } else {
     makeAvatarOrder(sOrder2);
     oConn.send("avatar order", sOrder);
   }
 }
Esempio n. 5
0
  // -------------------------------actionMove-------------------------------
  private void actionMove() {
    System.out.println("actionMove");
    oConn.send("move", listChar[iChar].getTileX() + ";" + listChar[iChar].getTileY());

    // TEMP
    stateEndTurn();
  }
Esempio n. 6
0
  // --------------------------------actionDisconnect---------------------------
  private void actionDisconnect() {

    try {
      oConn.disconnect();
      tConn.join(2000);
      tMain.join(2000);
    } catch (Exception e) {
    }
  }
  private void sendBytes(FileInputStream fis) throws Exception {
    // Construct a 1K buffer to hold bytes on their way to the socket.
    byte[] buffer = new byte[1024];
    int bytes = 0;

    while ((bytes = fis.read(buffer)) != -1) {
      sockManager.Escribir(buffer, bytes);
    }
  }
Esempio n. 8
0
 public void tradeParties() {
   oConn.sendObject(oParty);
   oOpponentParty = (Party) oConn.readObject();
   //		oOpponentParty.resetImages();
   oMap.addOpponentParty();
 }
Esempio n. 9
0
 // -------------------------------actionPrePlay----------------------------
 private void actionPrePlay() {
   oMap.prepareForPlay();
   lblStatus3.setText("Waiting for opponent to deploy...");
   bDeployed = true;
   oConn.send("deployed", "");
 }
Esempio n. 10
0
  private void processRequest() throws Exception {
    // Get the request line of the HTTP request message.
    String requestLine = sockManager.Leer();
    System.out.println("RequestLine: " + requestLine);

    // Extract the filename from the request line.
    StringTokenizer tokens = new StringTokenizer(requestLine);
    tokens.nextToken(); // skip over the method, which should be "GET"
    // System.out.println("Next Token: "+tokens.nextToken());
    String fileName = tokens.nextToken();
    fileName = "files/" + fileName;
    // Open the requested file.
    FileInputStream fis = null;
    boolean fileExists = true;
    try {
      fis = new FileInputStream(fileName);
    } catch (FileNotFoundException e) {
      fileExists = false;
      System.out.println("No abre fichero");
    }

    System.out.println("Incoming!!!");
    System.out.println("1.............." + requestLine);

    // Get and display the header lines.
    String headerLine = null;
    while ((headerLine = sockManager.Leer()).length() != 0) {
      System.out.println("2.............." + headerLine);
    }

    // Construct the response message.
    String statusLine = null;
    String contentTypeLine = null;
    String entityBody = null;
    if (fileExists) {
      statusLine = "HTTP/1.0 200 OK";
      contentTypeLine = contentType(fileName);
    } else {
      statusLine = "HTTP/1.0 404 Not Found";
      contentTypeLine = "Content-type: text/html" + "CRLF";
      entityBody =
          "<HTML>" + "<HEAD><TITLE>Not Found</TITLE></HEAD>" + "<BODY>Not Found</BODY></HTML>";
    }

    // Send the status line.
    sockManager.Escribir(statusLine + CRLF);

    // Send the content type line.
    sockManager.Escribir(contentTypeLine + CRLF);

    // Send a blank line to indicate the end of the header lines.
    sockManager.Escribir(CRLF);

    // Send the entity body.
    if (fileExists) {
      sendBytes(fis);
      fis.close();
    } else {
      // INTRODUCIR AQUI
      sockManager.Escribir(entityBody);
    }

    // Close streams and socket.
    sockManager.CerrarStreams();
    sockManager.CerrarSocket();
  }