Advertisement
Guest User

Untitled

a guest
Mar 13th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. package Server;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintWriter;
  8. import java.net.Socket;
  9. import java.util.ArrayList;
  10.  
  11. import ChatClient.Client;
  12. import Protocol.SimpleProtocol;
  13.  
  14. class ServerThread implements Runnable {
  15.  
  16. private Server server;
  17. private Socket clientSocket;
  18. private BufferedReader in;
  19. private DataOutputStream out;
  20. private SimpleProtocol protocol;
  21. private String username;
  22. private String password;
  23.  
  24.  
  25. public ServerThread(Socket clientSocket, Server server)
  26. {
  27. this.clientSocket = clientSocket;
  28. this.server = server;
  29. this.protocol = new SimpleProtocol();
  30. try
  31. {
  32. in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  33. out = new DataOutputStream(clientSocket.getOutputStream());
  34. }
  35. catch (IOException e)
  36. {
  37. e.printStackTrace();
  38. }
  39.  
  40. }
  41.  
  42. public void run()
  43. {
  44. try {
  45. System.out.println("processing request");
  46. out.writeBytes(protocol.createMessage("", "Welcome to my Server") + "\n");
  47. while (true)
  48. {
  49. System.out.println("while loop entered");
  50. String instring = in.readLine();
  51. System.out.println("instring is " + instring);
  52. if(getFunction(instring).equals("sign-up"))
  53. {
  54. out.writeBytes(signUp(instring) + "\n");
  55. }
  56. else if (getFunction(instring).equals("sign-in"))
  57. {
  58. out.writeBytes(signIn(instring) + "\n");
  59. }
  60. else if (getFunction(instring).equals("send-message"))
  61. {
  62. out.writeBytes(messageSentFromUser(instring) +"\n");
  63. }
  64. else if (getFunction(instring).equals("get-message"))
  65. {
  66. out.writeBytes(messagesSentToUser(instring) + "\n");
  67. }
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*else if (protocol.decodeMessage(instring)[0] == "sign-in")
  77. {
  78. signIn(protocol.decodeMessage(instring)[1], protocol.decodeMessage(instring)[2]);
  79. }
  80. else if (protocol.decodeMessage(instring)[0] == "send-message")
  81. {
  82. messageSentFromUser(protocol.decodeMessage(instring)[1]);
  83. }
  84. else if (protocol.decodeMessage(instring)[0] == "get-message")
  85. {
  86. messagesSentToUser(Integer.parseInt(protocol.decodeMessage(instring)[1]));
  87. }
  88.  
  89. //clientSocket.close(); */
  90.  
  91.  
  92.  
  93. }
  94. catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. /*finally {
  98. try {
  99. in.close();
  100. } catch (IOException ioException) {
  101. ioException.printStackTrace();
  102. }
  103. }*/
  104.  
  105.  
  106. }
  107.  
  108. public String getFunction(String input)
  109. {
  110. String message[] = protocol.decodeMessage(input);
  111. return message[0];
  112. }
  113.  
  114. public String signUp(String credentials)
  115. {
  116. String[] message = protocol.decodeMessage(credentials);
  117. String username = message[1];
  118. String password = message[2];
  119.  
  120. if(!(this.server.getUserMap().containsKey(username)) && username.length() >=5 && username.length() <=20 &&
  121. password.length() >= 8 && password.length() <=32)
  122. {
  123. this.server.getUserMap().put(username, password);
  124. System.out.println("username: " + this.username);
  125. System.out.println("password: " + this.server.getUserMap().get(username));
  126.  
  127. return protocol.createMessage("sign-up", "true","User successfully signed up");
  128. }
  129.  
  130. else if (username.length() <5 || username.length() > 20)
  131. {
  132. return protocol.createMessage("sign-up", "false", "Username must be between 5 and 20 characters long");
  133. }
  134. else
  135. {
  136. return protocol.createMessage("sign-up", "false", "Passwrod must be between 8 and 32 characters long");
  137. }
  138.  
  139.  
  140. }
  141.  
  142. public String signIn(String credentials)
  143. {
  144. String[] message = protocol.decodeMessage(credentials);
  145. String username = message[1];
  146. String password = message[2];
  147.  
  148. this.username = username;
  149. this.password = password;
  150.  
  151. if (!this.server.getUserMap().containsKey(username))
  152. {
  153. try {
  154. clientSocket.close();
  155. return protocol.createMessage("sign-in", "false", "username is not recognised, please try again.");
  156.  
  157. } catch (IOException e) {
  158. e.printStackTrace();
  159. }
  160. }
  161. else if (!this.server.getUserMap().get(username).equals(password))
  162. {
  163. try {
  164. clientSocket.close();
  165. return protocol.createMessage("sign-in", "false", "username and password do not match, please try again.");
  166.  
  167. } catch (IOException e) {
  168. e.printStackTrace();
  169. }
  170. }
  171. else
  172. {
  173. return protocol.createMessage("sign-in", "true", "welcome back, " + username + "!");
  174. }
  175. return protocol.createMessage("sign-in", "false", "reason unknown");
  176. }
  177.  
  178.  
  179.  
  180. // number 4
  181. public String messageSentFromUser(String protocolString)
  182. {
  183. System.out.println("about to add to message arraylist");
  184. String[] received = protocol.decodeMessage(protocolString);
  185. String message = received[1];
  186.  
  187. this.server.getMessages().add(new Message(this.username, message));
  188. System.out.println("size of message arraylist: " + (this.server.getMessages().size()-1));
  189. return protocol.createMessage("send-message", "true", ""+(this.server.getMessages().size()-1));
  190. }
  191.  
  192.  
  193.  
  194.  
  195. public String messagesSentToUser(String protocolString)
  196. {
  197. System.out.println("message array list size: " +this.server.getMessages().size());
  198. String[] received = protocol.decodeMessage(protocolString);
  199. int offset = Integer.parseInt(received[1]) + 1;
  200. String[] messagesArray= new String[((this.server.getMessages().size()-offset)*4)+1];
  201.  
  202. if (offset<this.server.getMessages().size())
  203. {
  204. messagesArray[0] = "get-message";
  205. int j=1;
  206. for (int i=offset; i<this.server.getMessages().size(); i++)
  207. {
  208. messagesArray[j] = ""+ i;
  209. messagesArray[j+1] = this.server.getMessages().get(i).getSender();
  210. messagesArray[j+2] = this.server.getMessages().get(i).getTime();
  211. messagesArray[j+3] = this.server.getMessages().get(i).getMessage();
  212. j += 4;
  213. }
  214.  
  215.  
  216.  
  217. return protocol.createMessage(messagesArray);
  218. }
  219. else
  220. {
  221. return protocol.createMessage("get-message");
  222. }
  223.  
  224.  
  225. }
  226.  
  227.  
  228.  
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement