Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package server;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- public class Connection implements AutoCloseable {
- private Socket socket;
- private BufferedReader reader;
- private PrintWriter writer;
- public Connection(Socket socket) throws IOException {
- this.socket = socket;
- this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- this.writer = new PrintWriter(socket.getOutputStream());
- }
- public boolean isClosed() {
- return socket.isClosed();
- }
- public Message receive() throws IOException {
- return new Message(reader.readLine());
- }
- public void send(String message) {
- writer.println(message);
- writer.flush();
- }
- @Override
- public void close() throws Exception {
- if (!socket.isClosed()) {
- socket.close();
- }
- }
- }
- -------------------------------------------------
- package server;
- import java.util.ArrayList;
- import java.util.List;
- public class Message {
- private static final List<Integer> mandatoryMarks = new ArrayList<Integer>() {{
- add(30);
- add(29);
- }};
- private List<Integer> note;
- private String error;
- public Message(String mess) {
- String[] marks = mess.split(",");
- if (marks.length != 2) {
- this.error = "Invalid number of marks";
- return;
- }
- this.note = new ArrayList<>(2);
- for (String mark : marks) {
- note.add(Integer.parseInt(mark));
- }
- this.validateNote();
- }
- private void validateNote() {
- for (Integer mandatoryMark : mandatoryMarks) {
- if (!note.contains(mandatoryMark)) {
- error = "Scrieti 30,29 pentru a incepe";
- note.clear();
- return;
- }
- }
- }
- public List<Integer> getNote() {
- return this.note;
- }
- public String getError() {
- return this.error;
- }
- }
- ----------------------------------------------------------------------
- package server;
- import java.util.ResourceBundle;
- import java.util.Scanner;
- public class Program {
- private static final int port = 8080;
- public static void main(String[] args) {
- System.out.println("Server is running, type 'exit' to close it.");
- try (Server server = new Server(port)) {
- try (Scanner scanner = new Scanner(System.in)) {
- while (true) {
- if (scanner.hasNextLine() && "exit".equalsIgnoreCase(scanner.nextLine())) {
- break;
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- System.exit(0);
- }
- }
- }
- ------------------------------------------------------------------------------------------------------------------
- package server;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- import java.util.TreeMap;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ThreadLocalRandom;
- import java.util.concurrent.TimeUnit;
- import java.util.stream.Stream;
- public class Server implements AutoCloseable {
- private ServerSocket socket;
- private ExecutorService executor;
- private int count = 0;
- private Map<Integer, Integer> marks;
- public Server(int port) throws IOException {
- marks = new HashMap<Integer,Integer>();
- for (Integer i = 0; i < 2; i++) {
- marks.put(i,0);
- }
- socket = new ServerSocket(port);
- executor = Executors.newFixedThreadPool(2);
- executor.execute(() -> {
- while (!socket.isClosed()) {
- try {
- Connection connection = new Connection(socket.accept());
- connection.send("Bun venit pe server, scrieti 30 pentru a incepe!");
- executor.submit(() -> {
- while (!connection.isClosed()) {
- try {
- Message message = connection.receive();
- String error = this.handleMessage(message);
- if (error != null && error != "") {
- connection.send(error);
- } else {
- for(int i = 30; i>0; i--){
- String str1 = Integer.toString(i);
- connection.send(str1);
- TimeUnit.SECONDS.sleep(1);
- }
- connection.send("La multi ani!");
- connection.close();
- count++;
- if (count == 2) {
- try {
- socket.close();
- this.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- } catch (Exception e) {
- }
- }
- });
- } catch (Exception e) {
- }
- }
- });
- }
- @Override
- public void close() throws Exception {
- System.out.println("La multi ani!");
- if (!executor.isTerminated()) {
- executor.shutdown();
- }
- if (!socket.isClosed()) {
- socket.close();
- }
- }
- private String handleMessage(Message message) {
- String error = message.getError();
- if (error != null) {
- return error;
- }
- List<Integer> note = message.getNote();
- for (int i = 0; i < 1; i++) {
- marks.put(i, marks.get(i) + note.get(i));
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement