Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Server;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.ArrayList;
- import ChatClient.Client;
- import Protocol.SimpleProtocol;
- class ServerThread implements Runnable {
- private Server server;
- private Socket clientSocket;
- private BufferedReader in;
- private DataOutputStream out;
- private SimpleProtocol protocol;
- private String username;
- private String password;
- public ServerThread(Socket clientSocket, Server server)
- {
- this.clientSocket = clientSocket;
- this.server = server;
- this.protocol = new SimpleProtocol();
- try
- {
- in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- out = new DataOutputStream(clientSocket.getOutputStream());
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- public void run()
- {
- try {
- System.out.println("processing request");
- out.writeBytes(protocol.createMessage("", "Welcome to my Server") + "\n");
- while (true)
- {
- System.out.println("while loop entered");
- String instring = in.readLine();
- System.out.println("instring is " + instring);
- if(getFunction(instring).equals("sign-up"))
- {
- out.writeBytes(signUp(instring) + "\n");
- }
- else if (getFunction(instring).equals("sign-in"))
- {
- out.writeBytes(signIn(instring) + "\n");
- }
- else if (getFunction(instring).equals("send-message"))
- {
- out.writeBytes(messageSentFromUser(instring) +"\n");
- }
- else if (getFunction(instring).equals("get-message"))
- {
- out.writeBytes(messagesSentToUser(instring) + "\n");
- }
- }
- /*else if (protocol.decodeMessage(instring)[0] == "sign-in")
- {
- signIn(protocol.decodeMessage(instring)[1], protocol.decodeMessage(instring)[2]);
- }
- else if (protocol.decodeMessage(instring)[0] == "send-message")
- {
- messageSentFromUser(protocol.decodeMessage(instring)[1]);
- }
- else if (protocol.decodeMessage(instring)[0] == "get-message")
- {
- messagesSentToUser(Integer.parseInt(protocol.decodeMessage(instring)[1]));
- }
- //clientSocket.close(); */
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- /*finally {
- try {
- in.close();
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- }*/
- }
- public String getFunction(String input)
- {
- String message[] = protocol.decodeMessage(input);
- return message[0];
- }
- public String signUp(String credentials)
- {
- String[] message = protocol.decodeMessage(credentials);
- String username = message[1];
- String password = message[2];
- if(!(this.server.getUserMap().containsKey(username)) && username.length() >=5 && username.length() <=20 &&
- password.length() >= 8 && password.length() <=32)
- {
- this.server.getUserMap().put(username, password);
- System.out.println("username: " + this.username);
- System.out.println("password: " + this.server.getUserMap().get(username));
- return protocol.createMessage("sign-up", "true","User successfully signed up");
- }
- else if (username.length() <5 || username.length() > 20)
- {
- return protocol.createMessage("sign-up", "false", "Username must be between 5 and 20 characters long");
- }
- else
- {
- return protocol.createMessage("sign-up", "false", "Passwrod must be between 8 and 32 characters long");
- }
- }
- public String signIn(String credentials)
- {
- String[] message = protocol.decodeMessage(credentials);
- String username = message[1];
- String password = message[2];
- this.username = username;
- this.password = password;
- if (!this.server.getUserMap().containsKey(username))
- {
- try {
- clientSocket.close();
- return protocol.createMessage("sign-in", "false", "username is not recognised, please try again.");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- else if (!this.server.getUserMap().get(username).equals(password))
- {
- try {
- clientSocket.close();
- return protocol.createMessage("sign-in", "false", "username and password do not match, please try again.");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- else
- {
- return protocol.createMessage("sign-in", "true", "welcome back, " + username + "!");
- }
- return protocol.createMessage("sign-in", "false", "reason unknown");
- }
- // number 4
- public String messageSentFromUser(String protocolString)
- {
- System.out.println("about to add to message arraylist");
- String[] received = protocol.decodeMessage(protocolString);
- String message = received[1];
- this.server.getMessages().add(new Message(this.username, message));
- System.out.println("size of message arraylist: " + (this.server.getMessages().size()-1));
- return protocol.createMessage("send-message", "true", ""+(this.server.getMessages().size()-1));
- }
- public String messagesSentToUser(String protocolString)
- {
- System.out.println("message array list size: " +this.server.getMessages().size());
- String[] received = protocol.decodeMessage(protocolString);
- int offset = Integer.parseInt(received[1]) + 1;
- String[] messagesArray= new String[((this.server.getMessages().size()-offset)*4)+1];
- if (offset<this.server.getMessages().size())
- {
- messagesArray[0] = "get-message";
- int j=1;
- for (int i=offset; i<this.server.getMessages().size(); i++)
- {
- messagesArray[j] = ""+ i;
- messagesArray[j+1] = this.server.getMessages().get(i).getSender();
- messagesArray[j+2] = this.server.getMessages().get(i).getTime();
- messagesArray[j+3] = this.server.getMessages().get(i).getMessage();
- j += 4;
- }
- return protocol.createMessage(messagesArray);
- }
- else
- {
- return protocol.createMessage("get-message");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement