Help with Java programming...

Discussion in 'OT Technology' started by Dj_MikeyMike, Nov 6, 2006.

  1. Dj_MikeyMike

    Dj_MikeyMike New Member

    Joined:
    Jul 30, 2006
    Messages:
    290
    Likes Received:
    0
    Location:
    oZ
    Okay, basically we are creating a location based battleships game, that uses a GPS to play the battleship game..

    Now we have agreed to change our login protocol.. and the problem is.. i have no clue where to start changing this.

    There is one file on the Midlet side of things and one on the Servlet side of things

    The Midlet file:
    Code:
    import java.io.IOException;
    import java.io.OutputStream;
    /*
     * PostRequestHandler.java
     *
     * Created on 7 May 2006, 09:56
     *
     * To change this template, choose Tools | Options and locate the template under
     * the Source Creation and Management node. Right-click the template and choose
     * Open. You can then make changes to the template in the Source Editor.
     */
    
    /**
     *
     * @author Paul
     */
    public class PostRequestHandler {
        private BattleshipMidlet m;
        private String currentMenu;
        private String uname;
        private String pword;
        private String choice;
        private String ID;
        private String opponent;
        private String response;
        private myChecksum myCS;
        
        /** Creates a new instance of PostRequestHandler */
        public PostRequestHandler(BattleshipMidlet m) {
            this.m = m;
            
        }
        public void send(OutputStream os, String msg) throws IOException {
            response = msg;
            
            //response = getChecksum(response);
            
            os.write(response.getBytes());
        }
        
        /***************************************************************************
         * Unit Test: Added in a helper function called getChecksum(String msg)
         * which in turn calls the myChecksum class to compute a CRC32 checksum.
         * The getChecksum(String msg) takes the current String as an argument
         * and returns that String with the checksum added to the end.
         * E.G. - Takes     LOGON p b
         *        Returns   LOGON p b CRC=1254637493;
         ***************************************************************************/
        public void send(OutputStream os) throws IOException {
            currentMenu = m.getCurrentMenu();
            choice = m.getChoice();
            m.blankChoice();
            
            if (currentMenu.equals("logon")) {
                uname = m.getUserName();
                pword = m.getPassword();
    
            //Change This to "&State=login&?UserId=&Username=" " Password=" "       
                String logon = "LOGON " + uname + " " + pword;
                
                logon = getChecksum(logon);
                System.out.println(logon);
                
                m.setPreviousMsg(logon); 
                os.write(logon.getBytes());
                            
            } else if (currentMenu.equals("main") && choice.equals("player list")) {
                choice = "";
                String pl = "LIST " + m.getID();
                
                pl = getChecksum(pl);
                System.out.println(pl);
                
                m.setPreviousMsg(pl);
                os.write(pl.getBytes());
                                        
            } else if (currentMenu.equals("main") && choice.equals("play game")) {
                choice = "";
                String playerList = "LIST " + m.getID();
                
                playerList = getChecksum(playerList);
                System.out.println(playerList);
                
                m.setPreviousMsg(playerList);
                os.write(playerList.getBytes());
                            
            /***********************************************************************
             * Claudia - this is used for sending the request to the server for 
             * the current player list.
             ***********************************************************************/
            } else if (currentMenu.equals("main") && choice.equals("chat")) {
                m.setCurrentMenu("chatMenu");
                String chatList = "LIST " + m.getID();
                
                chatList = getChecksum(chatList);
                
                m.setPreviousMsg(chatList);
                os.write(chatList.getBytes());
                            
            } else if (currentMenu.equals("logout")) {
                ID = m.getID();
                String logout = "LOGOUT " + uname + " " + pword + " " + ID;
                
                //logout = getChecksum(logout);
                
                os.write(logout.getBytes());
                m.setLoggedIn(false);
                m.ShowLogon();
                
            } else if (currentMenu.equals("userlist") && m.getPlayerSelectedStatus()) {
                ID = m.getID();
                String opID = "";
                opponent = m.getOpponent();
                
                for (int i = 0; i < 4; i++) {
                    if (opponent.charAt(i) != ' ') {
                        opID += opponent.charAt(i);
                    } else
                        break;
                }
                m.setOpponentID(opID);
                
                String playMsg = "PLAY " + opponent + " " + ID;
                
                //playMsg = getChecksum(playMsg);
                
                os.write(playMsg.getBytes());
                m.setPlayerSelected(false);
                
            } else {
                if (m.getResponse()) {
                    String message = m.getResponseMessage();
                    
                    //message = getChecksum(message);
                    
                    os.write(message.getBytes());
                    m.setResponseMessage("");
                } else {
                    String message = m.getMessage();
                    
                    //message = getChecksum(message);
                    
                    os.write(message.getBytes());
                }
                
            }
        }
        
        private String getChecksum(String msg) {
            myCS = new myChecksum(msg);
            myCS.compute();
            return myCS.AppendChecksum();
        }
    }
    
    The Servlet File:
    Code:
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.StringTokenizer;
    /*
     * Verifier.java
     *
     * Created on 4 April 2006, 15:54
     *
     * To change this template, choose Tools | Options and locate the template under
     * the Source Creation and Management node. Right-click the template and choose
     * Open. You can then make changes to the template in the Source Editor.
     */
    
    /**
     *
     * @author Paul
     */
    public class Verifier {
        
        private String user = null;
        private String pass = null;
        private String msg = "";
        private BufferedReader br;
        protected String fname = "";
        protected String lname = "";
        protected int wins = 0;
        protected int losses = 0;
        
        /** Creates a new instance of Verifier */
        public Verifier(String u, String p) {
            user = u;
            pass = p;
        }
        
        public String verify() {
            String line;
            /*try {
                FileWriter fileWriter = new FileWriter("test.txt");
                String testSTR = "This is a test";
                fileWriter.write(testSTR);
                fileWriter.close();
            } catch (IOException e) {  // problem during write
                // Log the exception. See .
            }*/
            try {
                // You need to change the path to where the ValidUsers.txt file is located
                // For a test case you can use username: test, password: test
                //br = new BufferedReader(new FileReader("d:\\JavaProjects\\Battleship\\ValidUsers.txt"));
                br = new BufferedReader(new FileReader("/usr/apache/tomcat/webapps/cp3120g1/ValidUsers.txt"));
                //br = new BufferedReader(new FileReader("C:\\Documents and Settings\\jc147495.JAMES-9565BDAD5.003\\Desktop\\Battleship\\ValidUsers.txt"));
                
                // read in a line from the file, break it up and compare the values
                while ((line = br.readLine()) != null ) {
                    StringTokenizer st = new StringTokenizer(line);
                    if (st.nextToken().equals(user)) {
                        if (st.nextToken().equals(pass)) {
                            msg = "OK";
                        }
                    }
                    if (msg.equals("OK")) {
                        fname = st.nextToken();
                        lname = st.nextToken();
                        wins = Integer.parseInt(st.nextToken());
                        losses = Integer.parseInt(st.nextToken());
                        break;
                    }
                    
                }
                if (msg.length() == 0)
                    msg = "BAD";
            } catch (FileNotFoundException fe) {
                System.out.println("Error opening file");
            } catch (IOException ioe) {
                System.out.println("Error reading from file");
            }
            try {
                br.close();
            } catch (IOException ioe) {
                System.out.println("Error closing file");
            }
            System.out.println("Message: " + msg);
            return msg;
            //return "OK";
        }
        
    }
    
    So the whole username / password protocol is to be changed from what it is now too..

    Midlet to servlet Game=" "&State=login&UserId=&Username=" " Password=" "
    Servlet to Midlet Game=" "&State=login&UserId=" "&Login=ok
    error message Game=" "&State=login&UserId=INVALIDID
    error message Game=" "&State=login&UserId=INVALIDTOOMANYUSERS

    Any help would be awesomely appreciated! Might even buy a sub for someone who helps me fix it all up!
     
  2. JustJeff

    JustJeff www.youtube.com/thisisjustjeff

    Joined:
    Oct 30, 2006
    Messages:
    1,651
    Likes Received:
    0
    Location:
    Long Island // Virginia Tech
    I'll look at it in my class. I think I found where the change has to go, but I'm not 100% sure. 20 minutes to get to my class and I'm not dressed yet o_0

    I'll see what I can do. There is hope!
     
  3. Dj_MikeyMike

    Dj_MikeyMike New Member

    Joined:
    Jul 30, 2006
    Messages:
    290
    Likes Received:
    0
    Location:
    oZ
    Wicked dude
     
  4. JustJeff

    JustJeff www.youtube.com/thisisjustjeff

    Joined:
    Oct 30, 2006
    Messages:
    1,651
    Likes Received:
    0
    Location:
    Long Island // Virginia Tech
    In public void send() in your postClass class.

    Look at the first if statement about 4 lines down or so. There's a comment. I don't know if you put it there. I think that's where the try/catch statement has to go for the login.
     

Share This Page