diff --git a/modules/artem/pom.xml b/modules/artem/pom.xml index 7c28363..ebe499d 100644 --- a/modules/artem/pom.xml +++ b/modules/artem/pom.xml @@ -4,6 +4,13 @@ com.hillel.elementary.java-geeks artem 0.0.1 + + + ch.qos.logback + logback-classic + RELEASE + + com.hillel.elementary diff --git a/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ClientRunner.java b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ClientRunner.java new file mode 100644 index 0000000..ec18f1a --- /dev/null +++ b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ClientRunner.java @@ -0,0 +1,7 @@ +package com.hillel.elementary.javageeks.artem.chat; + +public class ClientRunner { + public static void main(String[] args) { + new Thread(new Output()).start(); + } +} diff --git a/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/Input.java b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/Input.java new file mode 100644 index 0000000..c48e29b --- /dev/null +++ b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/Input.java @@ -0,0 +1,32 @@ +package com.hillel.elementary.javageeks.artem.chat; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.Socket; +import java.util.Scanner; + +public class Input implements Runnable{ + + private Socket socket; + private Logger LOG = LoggerFactory.getLogger(ClientRunner.class); + + Input(Socket socket) { + this.socket = socket; + } + + @Override + public void run() { + try (Scanner scanner = new Scanner(socket.getInputStream())) { + + while (true) { + if (scanner.hasNext()) { + System.out.println(scanner.nextLine()); + } + } + } catch (IOException e) { + LOG.error(String.valueOf(e)); + } + } +} diff --git a/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/Output.java b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/Output.java new file mode 100644 index 0000000..6a3823f --- /dev/null +++ b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/Output.java @@ -0,0 +1,48 @@ +package com.hillel.elementary.javageeks.artem.chat; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.Scanner; + +public class Output implements Runnable { + + private Logger LOG = LoggerFactory.getLogger(ClientRunner.class); + + @Override + public void run() { + try (Scanner scanner = new Scanner(System.in)) { + + System.out.println("Your name:"); + String name = scanner.nextLine().trim(); + System.out.println("Host:"); + String host = scanner.nextLine().trim(); + System.out.println("Port:"); + int port = Integer.parseInt(scanner.nextLine()); + + Socket socket = new Socket(); + socket.connect(new InetSocketAddress(host, port)); + LOG.info("You connected to the: " + host + ":" + port); + Input input = new Input(socket); + new Thread(input).start(); + + try (PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) { + writer.println(name); + boolean run = true; + while (run) { + if (scanner.hasNext()) { + String message = scanner.nextLine(); + writer.println(message); + run = !message.trim().equalsIgnoreCase("bye"); + } + } + } + } catch (IOException e) { + LOG.error(String.valueOf(e)); + } + } +} + diff --git a/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ServerInput.java b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ServerInput.java new file mode 100644 index 0000000..44222d8 --- /dev/null +++ b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ServerInput.java @@ -0,0 +1,50 @@ +package com.hillel.elementary.javageeks.artem.chat; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.Socket; +import java.util.HashMap; +import java.util.Scanner; + +public class ServerInput implements Runnable{ + + private Logger LOG = LoggerFactory.getLogger(ServerRunner.class); + public static HashMap users = ServerOptions.getMapOfUsers(); + private Socket socket; + String name; + + public ServerInput(Socket socket) { + this.socket = socket; + } + + @Override + public void run() { + + try (Scanner scanner = new Scanner(socket.getInputStream())) { + try (PrintWriter writer = new PrintWriter(socket.getOutputStream(), true)) { + name = scanner.nextLine(); + LOG.info(name + " connected."); + users.put(name, writer); + + boolean run = true; + while (run) { + if (scanner.hasNext()) { + String text = scanner.nextLine(); + String messageToAll = name + ": " + text; + ServerOptions.messageToUsers(messageToAll); + if (text != null && text.trim().equalsIgnoreCase("bye")) { + ServerOptions.userDisconect(name); + socket.close(); + break; + } + } + } + } + } catch (IOException e) { + LOG.error(String.valueOf(e)); + } + } +} diff --git a/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ServerOptions.java b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ServerOptions.java new file mode 100644 index 0000000..e1bfc3d --- /dev/null +++ b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ServerOptions.java @@ -0,0 +1,47 @@ +package com.hillel.elementary.javageeks.artem.chat; + +import com.hillel.elementary.javageeks.artem.chat.Server; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Scanner; + +public class ServerOptions { + + + private static Logger LOG = LoggerFactory.getLogger(Server.class); + public static HashMap users = new HashMap<>(); + + public static HashMap getMapOfUsers(){ + return users; + } + + public static synchronized void messageToUsers(String message) { + if (message != null && !message.isEmpty()) { + for (PrintWriter writer : users.values()) { + writer.println(message); + } + } + } + + public static synchronized void userDisconect(String name) { + String userDisconnected = "user " + name + " disconnected"; + messageToUsers(userDisconnected); + + LOG.info(userDisconnected); + + users.get(name).close(); + users.remove(name); + } + + public static Integer getNumberOfPort() { + try (Scanner scanner = new Scanner(System.in)) { + System.out.println("Server port: "); + Integer port = Integer.parseInt(scanner.next()); + LOG.info("Server started."); + return port; + } + } +} diff --git a/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ServerRunner.java b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ServerRunner.java new file mode 100644 index 0000000..6b5b50a --- /dev/null +++ b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/chat/ServerRunner.java @@ -0,0 +1,24 @@ +package com.hillel.elementary.javageeks.artem.chat; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +public class ServerRunner { + + private static Logger LOG = LoggerFactory.getLogger(ServerRunner.class); + + public static void main(String[] args) { + try (ServerSocket serverSocket = new ServerSocket(ServerOptions.getNumberOfPort())) { + while (true) { + Socket socket = serverSocket.accept(); + new Thread(new ServerInput(socket)).start(); + } + } catch (IOException e) { + LOG.error(String.valueOf(e)); + } + } +}