Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. package server;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.Socket;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.Collections;
  11. import java.util.List;
  12.  
  13. public class Connection implements AutoCloseable {
  14. private Socket socket;
  15. private BufferedReader reader;
  16. private PrintWriter writer;
  17.  
  18. public Connection(Socket socket) throws IOException {
  19. this.socket = socket;
  20. this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  21. this.writer = new PrintWriter(socket.getOutputStream());
  22. }
  23.  
  24. public boolean isClosed() {
  25. return socket.isClosed();
  26. }
  27.  
  28. public Message receive() throws IOException {
  29. return new Message(reader.readLine());
  30. }
  31.  
  32. public void send(String message) {
  33. writer.println(message);
  34. writer.flush();
  35. }
  36.  
  37. @Override
  38. public void close() throws Exception {
  39. if (!socket.isClosed()) {
  40. socket.close();
  41. }
  42. }
  43. }
  44. -------------------------------------------------
  45.  
  46. package server;
  47.  
  48. import java.util.ArrayList;
  49. import java.util.List;
  50.  
  51. public class Message {
  52. private static final List<Integer> mandatoryMarks = new ArrayList<Integer>() {{
  53. add(30);
  54. add(29);
  55. }};
  56.  
  57. private List<Integer> note;
  58. private String error;
  59.  
  60. public Message(String mess) {
  61. String[] marks = mess.split(",");
  62. if (marks.length != 2) {
  63. this.error = "Invalid number of marks";
  64. return;
  65. }
  66. this.note = new ArrayList<>(2);
  67. for (String mark : marks) {
  68. note.add(Integer.parseInt(mark));
  69. }
  70. this.validateNote();
  71. }
  72.  
  73. private void validateNote() {
  74. for (Integer mandatoryMark : mandatoryMarks) {
  75. if (!note.contains(mandatoryMark)) {
  76. error = "Scrieti 30,29 pentru a incepe";
  77. note.clear();
  78. return;
  79. }
  80. }
  81. }
  82.  
  83. public List<Integer> getNote() {
  84. return this.note;
  85. }
  86.  
  87. public String getError() {
  88. return this.error;
  89. }
  90. }
  91. ----------------------------------------------------------------------
  92. package server;
  93.  
  94. import java.util.ResourceBundle;
  95. import java.util.Scanner;
  96.  
  97. public class Program {
  98. private static final int port = 8080;
  99.  
  100. public static void main(String[] args) {
  101. System.out.println("Server is running, type 'exit' to close it.");
  102. try (Server server = new Server(port)) {
  103. try (Scanner scanner = new Scanner(System.in)) {
  104. while (true) {
  105. if (scanner.hasNextLine() && "exit".equalsIgnoreCase(scanner.nextLine())) {
  106. break;
  107. }
  108. }
  109. }
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. } finally {
  113. System.exit(0);
  114. }
  115. }
  116. }
  117. ------------------------------------------------------------------------------------------------------------------
  118. package server;
  119.  
  120. import java.io.IOException;
  121. import java.net.ServerSocket;
  122. import java.util.ArrayList;
  123. import java.util.Collections;
  124. import java.util.HashMap;
  125. import java.util.List;
  126. import java.util.Map;
  127. import java.util.Map.Entry;
  128. import java.util.Set;
  129. import java.util.TreeMap;
  130. import java.util.concurrent.ExecutorService;
  131. import java.util.concurrent.Executors;
  132. import java.util.concurrent.ThreadLocalRandom;
  133. import java.util.concurrent.TimeUnit;
  134. import java.util.stream.Stream;
  135.  
  136. public class Server implements AutoCloseable {
  137. private ServerSocket socket;
  138. private ExecutorService executor;
  139. private int count = 0;
  140. private Map<Integer, Integer> marks;
  141.  
  142. public Server(int port) throws IOException {
  143. marks = new HashMap<Integer,Integer>();
  144. for (Integer i = 0; i < 2; i++) {
  145. marks.put(i,0);
  146. }
  147. socket = new ServerSocket(port);
  148. executor = Executors.newFixedThreadPool(2);
  149. executor.execute(() -> {
  150. while (!socket.isClosed()) {
  151. try {
  152. Connection connection = new Connection(socket.accept());
  153. connection.send("Bun venit pe server, scrieti 30 pentru a incepe!");
  154. executor.submit(() -> {
  155. while (!connection.isClosed()) {
  156. try {
  157. Message message = connection.receive();
  158. String error = this.handleMessage(message);
  159. if (error != null && error != "") {
  160. connection.send(error);
  161. } else {
  162. for(int i = 30; i>0; i--){
  163. String str1 = Integer.toString(i);
  164. connection.send(str1);
  165. TimeUnit.SECONDS.sleep(1);
  166. }
  167. connection.send("La multi ani!");
  168. connection.close();
  169. count++;
  170. if (count == 2) {
  171. try {
  172. socket.close();
  173. this.close();
  174. } catch (IOException e) {
  175. e.printStackTrace();
  176. } catch (Exception e) {
  177. e.printStackTrace();
  178. }
  179. }
  180. }
  181. } catch (Exception e) {
  182. }
  183. }
  184. });
  185. } catch (Exception e) {
  186. }
  187. }
  188. });
  189. }
  190.  
  191. @Override
  192. public void close() throws Exception {
  193.  
  194.  
  195. System.out.println("La multi ani!");
  196. if (!executor.isTerminated()) {
  197. executor.shutdown();
  198. }
  199. if (!socket.isClosed()) {
  200. socket.close();
  201. }
  202. }
  203.  
  204. private String handleMessage(Message message) {
  205. String error = message.getError();
  206. if (error != null) {
  207. return error;
  208. }
  209. List<Integer> note = message.getNote();
  210. for (int i = 0; i < 1; i++) {
  211. marks.put(i, marks.get(i) + note.get(i));
  212. }
  213. return null;
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement