Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author mxr808
- */
- package groupworkGUI;
- import java.net.*;
- import java.io.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class HandlerThread extends Thread {
- server myServer;
- Socket sock;
- boolean connected;
- String username;
- database accountdb;
- public HandlerThread(Socket s, server currentServer, database db)
- {
- try{
- //Handler thread for each client. Specify socket on which client is operating, specify the server and the database.
- myServer = currentServer;
- sock = s;
- connected = true;
- accountdb = db;
- boolean loggedIn= false;
- System.out.println("Handler thread created for new connection");
- try{
- //Wait for client to select "1" (Login) or "2" (Create account)
- BufferedReader readMessage = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- String choice = readMessage.readLine();
- //If 1, call login() method.
- if(choice.equals("1"))
- {
- System.out.println(loggedIn);
- while(loggedIn == false)
- {
- loggedIn = login();
- System.out.println(loggedIn);
- }
- start();
- onlineUsers userUpdate = new onlineUsers();
- userUpdate.start();
- }
- else
- {
- //Else create the account.
- String username = readMessage.readLine();
- String password = readMessage.readLine();
- loggedIn = createAccount(username, password);
- start();
- onlineUsers userUpdate = new onlineUsers();
- userUpdate.start();
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- System.err.println("Exception in creation of handler thread");
- }
- }
- catch(Exception e)
- {
- }
- }
- @Override
- public void run()
- {
- try
- {
- while(connected == true)
- {
- //While running, wait for any received data over the socket.
- System.out.println("Waiting for received messages");
- System.out.println(sock);
- BufferedReader readMessage = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- String message = readMessage.readLine();
- System.out.println(message);
- //If null (Client has disconnected), remove the socket.
- if(message == null)
- {
- myServer.removeSocket(sock);
- System.out.println("Removed socket" + sock);
- accountdb.insertLog(username, "logged out", "N/A");
- connected = false;
- }
- //Otherwise, split the message by spaces.
- else
- {
- System.out.println("Message received: " + message);
- //First index in that array is the target username.
- String clientUsername = null;
- String textMessage = null;
- try{
- clientUsername = message.substring(0, message.indexOf('@'));
- textMessage = message.substring(clientUsername.length()+1, message.length());
- }
- catch(StringIndexOutOfBoundsException e)
- {
- }
- //The message itself is the rest of the message.
- //Send message to that username.
- System.out.println("Sending message to: " + clientUsername);
- accountdb.insertLog(username, "sent message", clientUsername);
- myServer.sendChatMessage(clientUsername, myServer.getUsername(sock), textMessage);
- while(myServer.wait == 1)
- {
- Thread.sleep(1000);
- }
- }
- }
- }
- catch(Exception e)
- {
- System.out.println("Client Disconnected: " + sock);
- System.out.println(e);
- System.out.println(e.getMessage());
- e.printStackTrace();
- myServer.removeSocket(sock);
- }
- }
- //Client is attempting to login.
- public boolean login()
- {
- String user = null;
- String password = null;
- try{
- System.out.println("Ran login method");
- //Wait for username and password to be received.
- System.out.println("Ready to receive username/password");
- BufferedReader readDetails = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- System.out.println("Received username/password");
- username = readDetails.readLine();
- password = readDetails.readLine();
- for(int i = 0; i < myServer.usernames.length; i++)
- {
- if(myServer.usernames[i].equals(username))
- {
- try{
- PrintWriter sendResult = new PrintWriter(sock.getOutputStream(), true);
- sendResult.println("false");
- }
- catch(Exception e)
- {
- }
- return false;
- }
- }
- }
- catch(Exception e)
- {
- System.out.println("Error receiving login");
- }
- //Split message by space.
- System.out.println("Received: " + username + " " + password);
- //Username is at 0 index, password at index 1.
- //Call the database checkLogin on these details. If returns true
- //tell the client they succeeded.
- //CHANGE TO accountdb.checkLogin(username, password)
- //username.equals("test")||username.equals("test1");
- if(accountdb.checkLogin(username, password))
- {
- myServer.addUser(username, sock);
- System.out.println("User " + username + " logged in successfully");
- accountdb.insertLog(username, "Logged in", "N/A");
- try{
- PrintWriter sendResult = new PrintWriter(sock.getOutputStream(), true);
- System.out.println("Sending result of login");
- sendResult.println("true");
- }
- catch(Exception e)
- {
- System.out.println("Error");
- }
- return true;
- }
- //If returns false:
- else
- {
- System.out.println("Password incorrect");
- System.out.println("Username not found");
- try{
- PrintWriter sendResult = new PrintWriter(sock.getOutputStream(), true);
- sendResult.println("false");
- }
- catch(Exception e)
- {
- }
- return false;
- }
- }
- public boolean createAccount(String newUsername, String password)
- {
- System.out.println("Creating account");
- boolean result = accountdb.createAccount(newUsername, password);
- //boolean result = true;
- System.out.println("createAccount: " + result);
- if(result)
- {
- PrintWriter sendResult = null;
- try {
- sendResult = new PrintWriter(sock.getOutputStream(), true);
- System.out.println("Sending result of login");
- sendResult.println("true");
- System.out.println("Returned true after telling database to create account");
- myServer.addUser(newUsername, sock);
- username = newUsername;
- accountdb.insertLog(username, "Created Account", "N/A");
- return true;
- } catch (IOException ex) {
- Logger.getLogger(HandlerThread.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- public String getUsername()
- {
- return username;
- }
- class onlineUsers extends Thread
- {
- @Override
- public void run()
- {
- while(true)
- {
- try{
- Thread.sleep(5000);
- StringBuffer onlineUsers = new StringBuffer("");
- onlineUsers.append("|WhoIs|^");
- for(int i = 0; i < myServer.usernames.length; i++)
- {
- if(myServer.usernames[i] != null)
- {
- onlineUsers.append(" ");
- onlineUsers.append(myServer.usernames[i]);
- }
- }
- PrintWriter userListSend = new PrintWriter(sock.getOutputStream(), true);
- userListSend.println(onlineUsers.toString());
- }
- catch(Exception e)
- {
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment