/* *Class Receiver * *Created by Martin Carlsson * *Internetprogrammering 1 - Course **/ import java.net.*; import java.io.*; import java.awt.*; import javax.swing.*; /**Receiver is a subklass of class Thread that takes care of receiving information from the opponent peer. * *@see java.lang.Thread */ public class Receiver extends Thread { private GameArea gA; private DatagramSocket socket; private Player p2, p1; private int myPort; private Ball ball; private int playerXpos; private int playerYpos; private MagnificentBall gui; private boolean looping = false; private String portSettingsText = "Your might wanna try a different port and / or different firewall settings"; private final static int MAX_PACKET_SIZE = 8192; /**Constructs a Receiver object with a port number to listen *to and references to the GameArea, Player and Ball objects. *@param gA reference to the GameArea *@param gui reference to the GUI(MagnificentBall) *@param myPort local port to listen to *@param p1 reference to the Player object for player 1 *@param p2 reference to the Player object for player 2 *@param ball reference to the Ball object *@see java.lang.Thread */ public Receiver(GameArea gA, MagnificentBall gui, int myPort, Player p1, Player p2, Ball ball) { this.myPort = myPort; this.p2 = p2; this.p1 = p1; this.gA = gA; this.ball = ball; this.gui = gui; // Creates a socket that listens to the local port try { socket = new DatagramSocket(myPort); looping = true; gA.setConnected(true); } catch(SocketException se) { JOptionPane.showMessageDialog(null, portSettingsText, "Port settings problem", JOptionPane.INFORMATION_MESSAGE); gA.disconnect(); } } /** *Listens for incoming DatagramPackets, reads each packet's content and *does what it should depending on the content of the packet. *@see java.net.DatagramPacket */ public void run() { byte[] buffer = new byte[MAX_PACKET_SIZE]; while(looping) { DatagramPacket packet = new DatagramPacket(buffer, buffer.length); char t; try { socket.receive(packet); InputStream in = new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength()); // creates a ByteArrayInputStream DataInputStream din = new DataInputStream(in); // that is used for creating a DataInputStream for reading char sent = din.readChar(); if(sent == 'a'){ // the opponent has sent a request to be active gA.setActive(false); // makes one self passive gui.setTitle(gui.getGameTitle() + "--Connected to " + gui.getHost() + "--Passive Mode"); // show passive mode status gA.sendStatus("inActiv"); // send info on request to be inactive(passive) gA.reset(); // reset to initial values } else if(sent == 'i') { // opponent has sent a request to be inactive gA.setActive(true); // makes one self active gui.setTitle(gui.getGameTitle() + "--Connected to " + gui.getHost() + "--Active Mode"); // show active mode status gA.reset(); // reset to initial values } else if(sent == 's') { // opponent has stoped the game if(!gA.activePlayer()) gui.setTitle(gui.getGameTitle() + "--PAUSED BY OPPONENT"); // show paus status } else if(sent == 'r') { // opponent has restarted paused game if(!gA.activePlayer()) gui.setTitle(gui.getGameTitle() + "--Connected to " + gui.getHost() + "--Passive Mode"); // again, show passive status } else if(sent == 'h') // oppenent has sent info on a "hit", i.e. the ball has hit a player Toolkit.getDefaultToolkit().beep(); // play a beep sound else if(sent == 'g') // opponent has sent info on game over gA.setGameOver(); // play a game over sequence else if(sent == 'q') { // opponent has disconnected gA.disconnect(); // disconnect one self gui.setTitle(gui.getGameTitle()+"--OPPONENT DISCONNECTED"); // show disconnected status } else if(sent == 'b') { // oppenent has sent ball positon int x = din.readInt(); // read x-pos t = din.readChar(); // read away limiter sign, in this case a '\t' int y = din.readInt(); // read the ball y-pos ball.setXPos(gA.getXMax() - x); // set x-pos, but opposite ball.setYPos(y); // set y-pos } else if(sent == 'p') { //opponent has it's player position int x = din.readInt(); // set opponent x-pos(is sent for in case of future development to make the players go horizontally, not just vertically.) t = din.readChar(); // read away limiter, in this case a'\t' int y = din.readInt(); // read opponent y-pos p2.setXpos(x); // set opponent x-pos (for now the same as the GameArea width) p2.setYpos(y); // sets opponents y-pos } else if(sent == 'n') // opponent has sent info om "New Game" gA.reset(); // reset to initial values else if(sent == 'l') { // opponent has sent info on "lifeAndScore" for both players int l = din.readInt(); // read 1's life t = din.readChar(); // read away delimiter, in this case a'\t' int c = din.readInt(); // read player 1's poäng t = din.readChar(); // read away delimiter, in this case a'\t' int l2 = din.readInt(); // read player 2's life t = din.readChar(); // read away delimiter, in this case a'\t' int c2 = din.readInt(); // read player 2's poäng p1.setLife(l); // read player 1's life if(c == Player.getBonusLimit()) // if player 1's score is higher than the bonus limit gui.setPlayerLifeColor("p1"); //change the color of the life status text p1.setPoints(c); // set player 1's score p2.setLife(l2); // set player 2's life if(c2 == Player.getBonusLimit()) // if player 2's score is higher than the bonus limit gui.setPlayerLifeColor("p2"); //change the color of the life status text p2.setPoints(c2); // set player 2's score gui.setPlayerOneLife(String.valueOf(l)); // show change of player 1's life in the GUI gui.setPlayerOnePoints(String.valueOf(c)); // show change of player 1's score in the GUI gui.setPlayerTwoLife(String.valueOf(l2)); // show change of player 2's life in the GUI gui.setPlayerTwoPoints(String.valueOf(c2)); // show change of player 2's score in the GUI } din.close(); // close DataInputStream gA.paintGameComponents(); // update GameArea } catch(IOException ioe) { gA.disconnect(); } } // end while } // end run /** *Close socket for incoming traffic */ public void closeReceiveSocket() { try { socket.close(); } catch(Exception e){ } } /** *Sets whether to loop or not *@param b true or false */ public void setLooping(boolean b) { looping = b; } } // End class Receiver