Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. package server;
  2.  
  3. import server.database.*;
  4.  
  5. /**
  6. * Výčtový typ sloužící pro rozparsování požadavku a následné zpracování od
  7. * klienta.
  8. *
  9. * @author Jakub
  10. */
  11. enum TYPE_REQUEST {
  12. ASYM_CRYPT {
  13. @Override
  14. public void process(String content, IClientHandler client) {
  15. if (client == null) {
  16. throw new NullPointerException("Client is null.");
  17. }
  18. client.setPublicKeyHost(content);
  19. client.sendMessages(1, SYM_CRYPT, client.getSecretKey());
  20. }
  21. },
  22. SYM_CRYPT {
  23. @Override
  24. public void process(String content, IClientHandler client) {
  25. if (client == null) {
  26. throw new NullPointerException("Client is null.");
  27. }
  28. if (!content.equals("OK")) {
  29. throw new NullPointerException("Transfer key failed.");
  30. }
  31. }
  32. },
  33. LOGIN {
  34. @Override
  35. public void process(String content, IClientHandler client) {
  36. if (client == null) {
  37. throw new NullPointerException("Client is null.");
  38. }
  39. String[] field = content.split(System.lineSeparator());
  40. String username = field[0];
  41. int password = Integer.parseInt(field[1]);
  42. User user = Databse.authencation(username, password);
  43. String builder;
  44. if (user != null) {
  45. client.setUser(user);
  46. ManazerClientHandlers.getInstance().switchClientHandlerActiveToOnline(client);
  47. client.sendMessages(2, LOGIN, user.toString());
  48. return;
  49. }
  50. client.sendMessages(2, LOGIN, "FALSE");
  51. }
  52.  
  53. },
  54. LOGOUT {
  55. @Override
  56. public void process(String content, IClientHandler client) {
  57. if (client == null) {
  58. throw new NullPointerException("Client is null.");
  59. }
  60. ManazerClientHandlers.getInstance().switchClientHandlerOnlineToActive(client);
  61. client.setUser(null);
  62. client.sendMessages(2, LOGOUT, "OK");
  63. }
  64. },
  65. MESSAGE {
  66. @Override
  67. public void process(String content, IClientHandler client) {
  68. if (client == null) {
  69. throw new NullPointerException("Client is null.");
  70. }
  71.  
  72.  
  73. }
  74.  
  75. },
  76. CLOSE_CONNECTION {
  77. @Override
  78. public void process(String content, IClientHandler client) {
  79. if (client == null) {
  80. throw new NullPointerException("Client is null.");
  81. }
  82. client.sendMessages(2, CLOSE_CONNECTION, "END");
  83. client.closeClientHandler();
  84. }
  85. },
  86. RECONNECT {
  87. @Override
  88. public void process(String content, IClientHandler client) {
  89. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  90. }
  91.  
  92. },
  93. LIST_ONLINEHOST {
  94. @Override
  95. public void process(String content, IClientHandler client) {
  96. if (client == null) {
  97. throw new NullPointerException("Client is null.");
  98. }
  99. StringBuilder builder = new StringBuilder();
  100. String online = ManazerClientHandlers.getInstance().getListOnlineHost(client.getUser().getID());
  101. for (String string : online.split(System.lineSeparator())) {
  102. builder.append(string);
  103. builder.append(System.lineSeparator());
  104. }
  105. client.sendMessages(2, LIST_ONLINEHOST, builder.toString());
  106. }
  107. },
  108. LIST_OFFLINEHOST {
  109. @Override
  110. public void process(String content, IClientHandler client) {
  111. if (client == null) {
  112. throw new NullPointerException("Client is null.");
  113. }
  114. StringBuilder builder = new StringBuilder();
  115. String offline = ManazerClientHandlers.getInstance().getListOfflineHost();
  116. for (String string : offline.split(System.lineSeparator())) {
  117. builder.append(string);
  118. builder.append(System.lineSeparator());
  119. }
  120. client.sendMessages(2, LIST_OFFLINEHOST, builder.toString());
  121. }
  122. },
  123. UPDATE_HOST_STATUS {
  124. @Override
  125. public void process(String content, IClientHandler client) {
  126. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  127.  
  128. }
  129. };
  130.  
  131. abstract public void process(String content, IClientHandler client);
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement