Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. package battleship.server;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintStream;
  7. import java.net.Socket;
  8.  
  9. public class ServerThread implements Runnable {
  10.  
  11. private BattleshipServer server;
  12. private Socket socketOfClient;
  13. private BufferedReader reader;
  14. private PrintStream cout;
  15. private int actionID;
  16. private SQLConnection SQL = new SQLConnection();
  17.  
  18. ServerThread(Socket client, int actionID, BattleshipServer server) throws IOException {
  19. this.server = server;
  20. this.socketOfClient = client;
  21. this.actionID = actionID;
  22. this.reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
  23. this.cout = new PrintStream(client.getOutputStream());
  24.  
  25. }
  26.  
  27. @Override
  28. public void run() {
  29.  
  30. String clientmsg;
  31.  
  32. clientmsg = getStringFromClient();
  33.  
  34. System.out.print("Action(" + actionID + ") :" + clientmsg + "\n");
  35.  
  36. operation(clientmsg);
  37.  
  38. close();
  39.  
  40. System.out.println("Action is over");
  41.  
  42. }
  43.  
  44. private void operation(String operation) {
  45.  
  46. switch (operation) {
  47. case "registrar":
  48. registrar();
  49. break;
  50. case "logout":
  51. logout();
  52. break;
  53. case "login":
  54. login();
  55. break;
  56. case "findGame":
  57. findGame();
  58. break;
  59. case "setMyBoard":
  60. setMyBoard();
  61. break;
  62. case "resetPassword":
  63. resetPassword();
  64. break;
  65. default:
  66. setDataToClient("Not supported yet");
  67. System.out.println("error: the operation " + operation
  68. + "not supported yet");
  69. break;
  70. }
  71. }
  72.  
  73. private void registrar() {
  74.  
  75. String result = "";
  76.  
  77. String ip = getStringFromClient().trim();
  78. String mac = getStringFromClient().trim();
  79. String name = getStringFromClient().trim();
  80. String mail = getStringFromClient().trim();
  81. String pass = getStringFromClient().trim();
  82.  
  83. result = SQL.Register(ip, mac, name, mail, pass);
  84.  
  85. setDataToClient(result);
  86.  
  87. }
  88.  
  89. private void logout() {
  90.  
  91. String result = "";
  92.  
  93. int ID = getIntFromClient();
  94. String ip = getStringFromClient().trim();
  95. String mac = getStringFromClient().trim();
  96.  
  97. result = SQL.logout(ID, ip, mac);
  98.  
  99. setDataToClient(result);
  100.  
  101. }
  102.  
  103. private void login() {
  104.  
  105. String result = "";
  106.  
  107. String mail = getStringFromClient().trim();
  108. String password = getStringFromClient().trim();
  109. String ip = getStringFromClient().trim();
  110. String mac = getStringFromClient().trim();
  111.  
  112. result = SQL.login(mail, password, ip, mac);
  113.  
  114. setDataToClient(result);
  115.  
  116. }
  117.  
  118. private void findGame() {
  119.  
  120. String[] result = new String[2];
  121.  
  122. int ID = getIntFromClient();
  123.  
  124. result = SQL.findGame(ID);
  125.  
  126. setDataToClient(result[0]);
  127. setDataToClient(result[1]);
  128.  
  129. }
  130.  
  131. private void setMyBoard() {
  132.  
  133. String result = "";
  134.  
  135. int userID = getIntFromClient();
  136. int gameID = getIntFromClient();
  137. String board = getStringFromClient();
  138.  
  139. result = SQL.setBoard(userID, gameID, board);
  140.  
  141. setDataToClient(result);
  142.  
  143. }
  144.  
  145. private void resetPassword() {
  146.  
  147. String result = "";
  148.  
  149. String mail = getStringFromClient();
  150. String ip = getStringFromClient();
  151. String mac = getStringFromClient();
  152.  
  153. result = SQL.resetPassword(mail, ip, mac);
  154.  
  155. if (result.length() != 5) {
  156. setDataToClient(result);
  157. } else {
  158. if (sendMail.sendEmailTo(mail, result)) {
  159. setDataToClient("1");
  160. } else {
  161. setDataToClient("-2");
  162. }
  163. }
  164. }
  165.  
  166. private void close() {
  167.  
  168. try {
  169. this.reader.close();
  170. this.cout.close();
  171. this.socketOfClient.close();
  172. server.closeClient();
  173.  
  174. } catch (IOException ex) {
  175. System.out.println("Action(" + actionID + ") : error in close");
  176. System.out.println(" -> " + ex);
  177. }
  178.  
  179. //System.exit(0);
  180. }
  181.  
  182. private String getStringFromClient() {
  183.  
  184. String str = "";
  185. try {
  186. str = reader.readLine();
  187. } catch (IOException ex) {
  188. str = "-1";
  189. close();
  190. System.out.println("Action(" + actionID + ") : error in gate String");
  191. }
  192. return str;
  193. }
  194.  
  195. private int getIntFromClient() {
  196.  
  197. int mag = 0;
  198. try {
  199. mag = Integer.parseInt(reader.readLine());
  200. } catch (IOException ex) {
  201. mag = -1;
  202. close();
  203. System.out.println("Action(" + actionID + ") : error in gate Int");
  204. }
  205. return mag;
  206. }
  207.  
  208. private void setDataToClient(String str) {
  209. cout.println(str);
  210. cout.flush();
  211. }
  212.  
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement