Advertisement
vallec

Server

Jan 10th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.*;
  5.  
  6. public class Server {
  7. private ServerSocket server;
  8. private static final String REGEXES_FILENAME = "regex_db.txt";
  9.  
  10. private final Object regexesLock;
  11.  
  12. public Server() {
  13. //initRegexes();
  14. regexesLock = new Object();
  15. }
  16.  
  17. public void start()
  18. {
  19. try
  20. {
  21. System.out.println("Server listening.");
  22. server = new ServerSocket(8080);
  23.  
  24. while (true)
  25. {
  26. Socket client = server.accept();
  27.  
  28. Thread clientThread = new Thread(() ->
  29. {
  30. System.out.println("Accepted client.");
  31. Scanner sc = null;
  32. PrintStream out = null;
  33.  
  34. try
  35. {
  36. sc = new Scanner(client.getInputStream());
  37. out = new PrintStream(client.getOutputStream());
  38. menu(sc, out);
  39. }
  40. catch (IOException e)
  41. {
  42. e.printStackTrace();
  43. }
  44. finally
  45. {
  46. if (sc != null)
  47. sc.close();
  48. if (out != null)
  49. out.close();
  50. }
  51. });
  52.  
  53. clientThread.start();
  54. }
  55. }
  56. catch (IOException e)
  57. {
  58. e.printStackTrace();
  59. }
  60. }
  61.  
  62. /*unchecked*/
  63. public List<Regex> loadRegexes() throws IOException {
  64. File file = new File(REGEXES_FILENAME);
  65.  
  66. if (!file.exists()) {
  67. boolean created = file.createNewFile();
  68.  
  69. if (!created) {
  70. throw new IOException("Error while creating file (" + REGEXES_FILENAME + ")");
  71. }
  72. return new ArrayList<>();
  73. } else {
  74. List<Regex> regexes = new ArrayList<>();
  75.  
  76. try(BufferedReader reader = new BufferedReader(new FileReader(REGEXES_FILENAME))){
  77. String currentLine;
  78. while ((currentLine = reader.readLine()) != null) {
  79. String[] params = currentLine.split(",");
  80.  
  81. if (params.length == 4) {
  82. int id = Integer.parseInt(params[0]);
  83. String pattern = params[1];
  84. String description = params[2];
  85. int rating = Integer.parseInt(params[3]);
  86.  
  87. Regex currRegex = new Regex(pattern, description);
  88. currRegex.setId(id);
  89. currRegex.setRating(rating);
  90.  
  91. regexes.add(currRegex);
  92. }
  93. }
  94. } catch (FileNotFoundException e) {
  95. throw new RuntimeException(e);
  96. }
  97.  
  98. return regexes;
  99. }
  100. }
  101.  
  102. private void saveRegex(String pattern, String description) throws IOException {
  103. Regex regex = new Regex(pattern, description);
  104. synchronized (regexesLock)
  105. {
  106. List<Regex> regexes = loadRegexes();
  107. boolean found = false;
  108. for (Regex currRegex : regexes) {
  109. if(currRegex.getPattern().equals(regex.getPattern())){
  110. found = true;
  111. }
  112. }
  113. if(!found) {
  114. regexes.add(regex);
  115. }
  116. saveRegexes(regexes);
  117. }
  118. }
  119.  
  120. public void saveRegexes(List<Regex> regexes)
  121. {
  122. synchronized (regexesLock)
  123. {
  124. try(BufferedWriter wr = new BufferedWriter(new FileWriter(REGEXES_FILENAME))) {
  125. for (Regex regex : regexes) {
  126. wr.write(regex.toString());
  127. wr.newLine();
  128. wr.flush();
  129. }
  130. } catch (IOException e) {
  131. throw new RuntimeException(e);
  132. }
  133. }
  134. }
  135.  
  136. private void menu(Scanner sc, PrintStream out) throws IOException {
  137. while (true) {
  138. out.println("Commands List - Just enter number: \n - Create Regex: 1\n - Find Regex: 2\n - Any other key will be considered as end program\n Enter command:");
  139. String command = sc.nextLine();
  140.  
  141. if (command.equals("1")) {
  142. createRegexHandler(sc, out);
  143. } else if (command.equals("2")){
  144. findRegexHandler(sc, out);
  145. }
  146. else {
  147. out.println("The program has stopped working");
  148. return;
  149. }
  150. }
  151. }
  152.  
  153. private void createRegexHandler(Scanner sc, PrintStream out) throws IOException {
  154. out.println("Enter pattern:");
  155. String pattern = sc.nextLine();
  156.  
  157. out.println("Enter description:");
  158. String description = sc.nextLine();
  159.  
  160. //saveRegex(pattern, description);
  161. out.println("Enter message separated with space:");
  162. String[] message = sc.nextLine().trim().split(" ");
  163. Regex tempRegex = new Regex(pattern, description);
  164. List<Boolean> result = RegexTester.test(tempRegex, message);
  165. out.println("Result: " + result);
  166.  
  167. out.println("1 - Save, Any other key - Do not save");
  168. String choice = sc.nextLine();
  169. if(choice.equals("1")) {
  170. saveRegex(pattern, description);
  171. //out.println("Regex saved!");
  172. }
  173. }
  174.  
  175. private void findRegexHandler(Scanner sc, PrintStream out) throws IOException {
  176. out.println("Enter keyword:");
  177. String keyword = sc.nextLine();
  178.  
  179. List<Regex> regexes = loadRegexes();
  180. out.println(regexes.size());
  181. if(regexes.isEmpty()) {
  182. out.println("There are no regexes added yet!");
  183. return;
  184. }
  185. List<Regex> foundRegexes = new ArrayList<>();
  186. for(Regex regex : regexes) {
  187. if(regex.getDescription().contains(keyword)) {
  188. foundRegexes.add(regex);
  189. }
  190. }
  191. out.println(foundRegexes.size());
  192. if(foundRegexes.isEmpty()) {
  193. out.println("There are no matches!");
  194. return;
  195. }
  196.  
  197.  
  198.  
  199. foundRegexes.sort(new Comparator<Regex>() {
  200. @Override
  201. public int compare(Regex o1, Regex o2) {
  202. return o2.getRating() - o1.getRating();
  203. }
  204. });
  205.  
  206. for(Regex regex : foundRegexes) {
  207. out.println("ID: " + regex.getId() + " Pattern: " + regex.getPattern() + " Description: " + regex.getDescription() + " Rating: " + regex.getRating());
  208. }
  209.  
  210. out.println("Enter id:");
  211. int chosenId = Integer.parseInt(sc.nextLine());
  212. Regex chosenRegex = foundRegexes.stream().filter(r -> r.getId() == chosenId).findFirst().orElse(null);
  213. if(chosenRegex == null) {
  214. out.println("This regex does not exist");
  215. return;
  216. } else {
  217. out.println("Pattern: " + chosenRegex.getPattern());
  218. }
  219.  
  220. out.println("Enter message separated with space:");
  221. String[] message = sc.nextLine().trim().split(" ");
  222. List<Boolean> result = RegexTester.test(chosenRegex, message);
  223. out.println("Result: " + result);
  224.  
  225. out.println("Add rating: (plus), (minus): ");
  226. String ratingCommand = sc.nextLine();
  227. if(ratingCommand.equalsIgnoreCase("plus")){
  228. chosenRegex.setRating(chosenRegex.getRating()+1);
  229. } else if (ratingCommand.equalsIgnoreCase("minus")) {
  230. chosenRegex.setRating(chosenRegex.getRating()-1);
  231. }
  232.  
  233. List<Regex> finalRegexes = new ArrayList<>();
  234. for(Regex regex : regexes) {
  235. if(regex.getId() == chosenRegex.getId()) {
  236. finalRegexes.add(chosenRegex);
  237. } else {
  238. finalRegexes.add(regex);
  239. }
  240. }
  241.  
  242. saveRegexes(finalRegexes);
  243. }
  244. }
  245.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement