Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.Vector;
- import javax.swing.JFrame;
- public class ServerThread extends Thread {
- private static Socket socket; // The Socket connected to our client
- public String input;
- public String output;
- public static String username;
- public static int userNumber = -1;//if user number remains 0 then the user hasn't logged in
- public String action;
- private String password; //you don't want the password accessible from other classes so private
- private static boolean destroyThread;
- boolean loggedIn = false;
- static int userThreadNumber;
- static DataInputStream din;
- static DataOutputStream dout;
- Vector <ServerConvo> convoArray = new Vector <ServerConvo>();
- public ServerThread(Socket socket, int threadNumber) {
- userThreadNumber = threadNumber;
- Server.console("Connect: socket =\""+socket+"\""+" threadNumber =\""+userThreadNumber+"\"");
- this.socket = socket;//save the parameters
- try {
- this.socket.setKeepAlive(true);
- } catch (SocketException e) {
- Server.console("Socket Broken: "+socket);
- destroyThread = true;
- }
- start();//start the thread running
- }
- public void run() {
- try {
- //Create a DataInputStream for communication;
- //the client is using a DataOutputStream to write to us
- din = new DataInputStream( socket.getInputStream() );
- dout = new DataOutputStream( socket.getOutputStream() );
- destroyThread=false;
- while (!destroyThread) {
- action = din.readUTF();
- if (action.equals("logIn")){
- username = din.readUTF();
- password = din.readUTF();
- for (int i = 0; i < Server.userList.size(); i++){
- if (Server.userList.get(i).username.equals(username)){
- if (Server.userList.get(i).password.equals(password) && !Server.userList.get(i).isloggedin){
- Server.console("Login: username =\""+username+"\" threadNumber=\""+userThreadNumber+"\"");
- loggedIn = true;
- userNumber = i;//sets the userNumber to the place in the userList array
- Server.userList.get(userNumber).userThreadNumber=userThreadNumber;
- Server.userList.get(userNumber).isloggedin=true;
- dout.writeUTF("confirmLogin");//tell the user that the login worked
- dout.flush();
- //for the amount of friend requests in the users "inbox"
- for (int ii=0; ii< Server.userList.get(userNumber).friendRequestArray.size();ii++) {
- dout.writeUTF("friendRequest");//the protocol
- dout.flush();
- dout.writeUTF(Server.userList.get(userNumber).friendRequestArray.get(ii)+"");//send the user number of the person trying to add you
- dout.flush();
- dout.writeUTF(Server.userList.get( Server.userList.get(userNumber).friendRequestArray.get(ii) ).username+"");//send the username of the person trying to add you
- dout.flush();
- }
- //tells the client the list of friends in the userlist
- for (int ii=0; ii<Server.userList.get(userNumber).friendArray.size();ii++){
- int friendNumber = Server.userList.get(userNumber).friendArray.get(ii); //the friend ID
- dout.writeUTF("friendList");
- dout.flush();
- dout.writeUTF(""+friendNumber);//write the user ID
- dout.flush();
- dout.writeUTF(Server.userList.get(friendNumber).username);//write the friend's name;
- dout.flush();
- }
- //tell user which friends are online
- for (int ii=0; ii<Server.userList.get(userNumber).friendArray.size();ii++){
- int friendNumber = Server.userList.get(userNumber).friendArray.get(ii); //the friend ID
- if (Server.userList.get(Server.userList.get(userNumber).friendArray.get(ii)).isloggedin){// if the friend is online
- dout.writeUTF("friendOnline");
- dout.flush();
- dout.writeUTF(friendNumber+"");//write the user ID
- dout.flush();
- }
- }
- //tell user which friends are offline
- for (int ii=0; ii<Server.userList.get(userNumber).friendArray.size();ii++){
- int friendNumber = Server.userList.get(userNumber).friendArray.get(ii); //the friend ID
- if (!Server.userList.get(friendNumber).isloggedin){// if the friend is online
- dout.writeUTF("friendOffline");
- dout.flush();
- dout.writeUTF(friendNumber+"");//write the user ID
- dout.flush();
- }
- }
- dout.writeUTF("updateFriendDisplay");
- dout.flush();
- //update all my friend's lists if they're online
- for (int ii=0; ii<Server.userList.get(userNumber).friendArray.size();ii++){
- int friendNumber = Server.userList.get(userNumber).friendArray.get(ii); //the friend ID
- if (Server.userList.get(friendNumber).isloggedin){// if the friend is online
- //go into other friends arrays and invoke the change online method with the current usernumber
- int friendThreadNumber = Server.userList.get(friendNumber).userThreadNumber;
- JFrame hi = new JFrame(friendThreadNumber+"=friend thread number");
- hi.setVisible(true);
- Server.serverThreadList.get( friendThreadNumber ).changeOnline(userNumber);
- }
- }
- break;
- }
- else {
- Server.console("Login Error: Wrong username/password");
- dout.writeUTF("wrong:user/pass");
- dout.flush();
- dout.close();
- din.close();
- socket.close();
- destroyThread = true;
- break;
- }
- }
- //once the login process has finished for the right user
- }
- //if the username hasn't been found in the userList
- if (!loggedIn){
- Server.console("Login Error: Cannot Find user in the database");
- dout.writeUTF("wrong:user/pass");
- dout.flush();
- dout.close();
- din.close();
- socket.close();
- destroyThread = true;
- }
- }
- else if (action.equals("invite")){
- int friendID=0;
- friendID = Integer.parseInt(din.readUTF());
- int convoNumber = 0;
- convoNumber =Integer.parseInt(din.readUTF());
- }
- else if (action.equals("message")){
- int convoNumber = Integer.parseInt(din.readUTF());
- String message = din.readUTF();
- convoArray.get(convoNumber).sendMessage(message);
- }
- else if (action.equals("register")){
- username = din.readUTF();
- password = din.readUTF();
- for (int i = 0; i < Server.userList.size(); i++){
- if (Server.userList.get(i).username.equals(username)){
- dout.writeUTF("taken");//if the username already exists display taken
- destroyThread = true;
- Server.console("Register: username taken =\""+username+"\"");
- break;//stops the loop
- }
- }
- //if the thread hasn't been stopped
- if (!destroyThread){
- dout.writeUTF("confirm");
- Server.userList.add(new UserInfo(Server.userList.size(), username, password));
- Server.console("Register: username added =\""+username+"\" Password =\""+password+"\" UserNumber =\""+ (Server.userList.size()-1)+"\"");
- destroyThread=true;
- }
- }
- else if (action.equals("addFriend")){
- String friend = "";
- friend = din.readUTF();
- System.out.println("Friend added to PendingArray");
- boolean friendFound = false;
- for (int i = 0; i < Server.userList.size(); i++){
- if (Server.userList.get(i).username.equals(friend)){
- Server.userList.get(i).friendRequestArray.add(userNumber);
- Server.userList.get(userNumber).pendingFriendArray.add(i);
- System.out.print("successfully");
- friendFound = true;
- break;
- }
- }
- if (!friendFound) {
- System.out.print(" unsuccessfully");
- }
- }
- else if (action.equals("removeFriend")){
- int friendID = 0;
- friendID = Integer.parseInt(din.readUTF());
- //goes through current user's friends
- for (int i = 0; i < Server.userList.get(userNumber).friendArray.size(); i++){
- if (Server.userList.get(userNumber).friendArray.get(i).equals(friendID)){
- //removes the friend from the current user's friend list.
- Server.userList.get(userNumber).friendArray.remove(i);
- }
- }
- //removes the user from the friend's list
- for (int i = 0; i < Server.userList.get(friendID).friendArray.size(); i++){
- if (Server.userList.get(friendID).friendArray.get(i).equals(userNumber)){
- Server.userList.get(friendID).friendArray.remove(i);
- }
- }
- }
- else if (action.equals("")){
- }
- else if (action.equals("logOut")){
- destroyThread=true;
- }
- else if (action.equals("requestReply")){
- int friendID = Integer.parseInt(din.readUTF());
- String status = din.readUTF();
- if (status.equals("accept")){
- for (int i =0; i<Server.userList.get(userNumber).friendRequestArray.size() ;i++ ){//removes the numbers from the friendRequestArray
- if (Server.userList.get(userNumber).friendRequestArray.get(i).equals(friendID)){
- Server.userList.get(userNumber).friendRequestArray.remove(i);
- break;
- }
- }
- for (int i=0; i < Server.userList.get(friendID).pendingFriendArray.size(); i++){
- if (Server.userList.get(friendID).pendingFriendArray.get(i).equals(userNumber)){
- Server.userList.get(friendID).pendingFriendArray.remove(i);
- }
- }
- Server.userList.get(userNumber).friendArray.add(friendID);
- Server.userList.get(friendID).friendArray.add(userNumber);
- }
- else if (status.equals("deny")){
- for (int i =0; i<Server.userList.get(userNumber).friendRequestArray.size() ;i++ ){//removes the numbers from the friendRequestArray
- if (Server.userList.get(userNumber).friendRequestArray.get(i).equals(friendID)){
- Server.userList.get(userNumber).friendRequestArray.remove(i);
- break;
- }
- }
- for (int i=0; i < Server.userList.get(friendID).pendingFriendArray.size(); i++){
- if (Server.userList.get(friendID).pendingFriendArray.get(i).equals(userNumber)){
- Server.userList.get(friendID).pendingFriendArray.remove(i);
- }
- }
- }
- }
- }
- } catch( EOFException ie ) {
- Server.console("flag1");
- } catch( IOException ie ) {
- // This does; tell the world!
- Server.console("flag2");
- }
- logout();
- }
- public void changeOnline(int ID){
- try {
- dout.writeUTF("friendOnline");
- dout.flush();
- dout.writeUTF(""+ID);
- dout.flush();
- dout.writeUTF("updateFriendDisplay");
- dout.flush();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void changeOffline(int ID){
- try {
- dout.writeUTF("friendOffline");
- dout.flush();
- dout.writeUTF(""+ID);
- dout.flush();
- dout.writeUTF("updateFriendDisplay");
- dout.flush();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void sendMessageToClient(int convoID, String name, String message){
- try {
- dout.writeUTF("message");
- dout.flush();
- dout.writeUTF(""+convoID);
- dout.flush();
- dout.writeUTF(name);
- dout.flush();
- dout.writeUTF(message);
- dout.flush();
- //need to finish sending the message to the user
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void logout(){
- try {
- dout.close();
- din.close();
- socket.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Server.console("Disconnect: username =\""+username+ "\" socket =\""+socket+"\"");
- if (userNumber>-1){
- Server.userList.get(userNumber).isloggedin=false;
- }
- //change everybody's thread numbers as removing this thread
- for (int i = userThreadNumber; i < Server.serverThreadList.size();i++){
- int currentUserNumber = Server.serverThreadList.get(i).userNumber;
- Server.console(""+Server.serverThreadList.get(i).userNumber);
- Server.userList.get(currentUserNumber).userThreadNumber-=1;//reduces every user's thread id by 1
- Server.console(""+Server.userList.get(currentUserNumber).userThreadNumber);
- Server.serverThreadList.remove(userThreadNumber);
- }
- }
- protected void finalize(){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement