Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chat ArtemBut #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions modules/artem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
<groupId>com.hillel.elementary.java-geeks</groupId>
<artifactId>artem</artifactId>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>

<parent>
<groupId>com.hillel.elementary</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

Original file line number Diff line number Diff line change
@@ -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<String, PrintWriter> 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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String, PrintWriter> users = new HashMap<>();

public static HashMap<String, PrintWriter> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
}