Internal changes
This commit is contained in:
parent
521f38468b
commit
795f020a98
@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = 'tech.nevets'
|
group = 'tech.nevets'
|
||||||
version = '1.0.0'
|
version = '1.1.0'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
@ -25,11 +25,23 @@ public class Connection implements Runnable {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
md.registerStream(username, os);
|
md.registerStream(this);
|
||||||
is.transferTo(md);
|
is.transferTo(md);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Connection Disconnected");
|
System.out.println("Connection Disconnected");
|
||||||
}
|
}
|
||||||
MessageDistributer.closeUserConnection(username);
|
MessageDistributer.closeUserConnection(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OutputStream getOutputStream() {
|
||||||
|
return this.os;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,17 @@ package tech.nevets.dliteserver;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class MessageDistributer extends OutputStream {
|
public class MessageDistributer extends OutputStream {
|
||||||
private static Map<String, OutputStream> userStreams = new HashMap<>();
|
private static List<Connection> userStreams = new ArrayList<>();
|
||||||
|
private static MessageDistributer instance;
|
||||||
private String buf = "";
|
private String buf = "";
|
||||||
|
|
||||||
|
public MessageDistributer() {
|
||||||
|
instance = this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int b) throws IOException {
|
public void write(int b) throws IOException {
|
||||||
buf += (char) b;
|
buf += (char) b;
|
||||||
@ -20,9 +22,9 @@ public class MessageDistributer extends OutputStream {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void flush() {
|
public void flush() {
|
||||||
for (OutputStream os : userStreams.values()) {
|
for (Connection conn : userStreams) {
|
||||||
try {
|
try {
|
||||||
os.write(buf.getBytes(StandardCharsets.UTF_8));
|
conn.getOutputStream().write((conn.getUsername() + "> " + buf).getBytes(StandardCharsets.UTF_8));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -30,21 +32,40 @@ public class MessageDistributer extends OutputStream {
|
|||||||
buf = "";
|
buf = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerStream(String username, OutputStream os) {
|
private void writeSystem(String message) {
|
||||||
Set<String> usernames = userStreams.keySet();
|
for (Connection conn : userStreams) {
|
||||||
while (usernames.contains(username)) {
|
|
||||||
username += ".imposter";
|
|
||||||
}
|
|
||||||
|
|
||||||
userStreams.put(username, os);
|
|
||||||
try {
|
try {
|
||||||
this.write(("SERVER> Welcome, " + username + "!\n").getBytes(StandardCharsets.UTF_8));
|
conn.getOutputStream().write(("SERVER> " + message + "\n").getBytes(StandardCharsets.UTF_8));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void closeUserConnection(String username) {
|
public void registerStream(Connection conn) {
|
||||||
userStreams.remove(username);
|
int i = 0;
|
||||||
|
String username;
|
||||||
|
for (username = conn.getUsername(); existingUsername(username); username = conn.getUsername() + "." + i) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
conn.updateUsername(username);
|
||||||
|
userStreams.add(conn);
|
||||||
|
writeSystem("Welcome " + username + "!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean existingUsername(String username) {
|
||||||
|
for (Connection c : userStreams) {
|
||||||
|
if (c.getUsername().equals(username)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MessageDistributer getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void closeUserConnection(Connection conn) {
|
||||||
|
userStreams.remove(conn);
|
||||||
|
getInstance().writeSystem(("Goodbye, " + conn.getUsername() + "!"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user