From 7aa4e0dd006a1979abe91e2a3a03855bad766b68 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Fri, 7 Oct 2022 14:28:29 +0200 Subject: [PATCH] Initial Commit --- .gitignore | 4 + src/be/jeffcheasey88/Client.java | 185 ++++++++++++++++++ src/be/jeffcheasey88/Main.java | 59 ++++++ src/be/jeffcheasey88/Pipeline.java | 16 ++ src/be/jeffcheasey88/Player.java | 14 ++ src/be/jeffcheasey88/PlayerRepository.java | 24 +++ src/be/jeffcheasey88/commands/Command.java | 21 ++ .../commands/CommandManager.java | 41 ++++ .../jeffcheasey88/commands/NameCommand.java | 18 ++ .../jeffcheasey88/commands/OtherCommand.java | 26 +++ 10 files changed, 408 insertions(+) create mode 100644 .gitignore create mode 100644 src/be/jeffcheasey88/Client.java create mode 100644 src/be/jeffcheasey88/Main.java create mode 100644 src/be/jeffcheasey88/Pipeline.java create mode 100644 src/be/jeffcheasey88/Player.java create mode 100644 src/be/jeffcheasey88/PlayerRepository.java create mode 100644 src/be/jeffcheasey88/commands/Command.java create mode 100644 src/be/jeffcheasey88/commands/CommandManager.java create mode 100644 src/be/jeffcheasey88/commands/NameCommand.java create mode 100644 src/be/jeffcheasey88/commands/OtherCommand.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f004aaf --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.settings/ +/bin +.classpath +.project \ No newline at end of file diff --git a/src/be/jeffcheasey88/Client.java b/src/be/jeffcheasey88/Client.java new file mode 100644 index 0000000..8b7ddfa --- /dev/null +++ b/src/be/jeffcheasey88/Client.java @@ -0,0 +1,185 @@ +package be.jeffcheasey88; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.Socket; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Scanner; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.xml.bind.DatatypeConverter; + +public class Client extends Thread{ + + private Socket clientSocket; + private InputStream inputStream; + private OutputStream outputStream; + private Queue messages; + + public Client(Socket socket) throws Exception { + this.clientSocket = socket; + this.inputStream = clientSocket.getInputStream(); + this.outputStream = clientSocket.getOutputStream(); + this.messages = new LinkedList<>(); + } + + @Override + public void run(){ + try { + doHandShakeToInitializeWebSocketConnection(); + + int buffLenth = 1024; + int len = 0; + byte[] b = new byte[buffLenth]; + //rawIn is a Socket.getInputStream(); + while(true){ + len = inputStream.read(b); + if(len!=-1){ + + byte rLength = 0; + int rMaskIndex = 2; + int rDataStart = 0; + //b[0] is always text in my case so no need to check; + byte data = b[1]; + byte op = (byte) 127; + rLength = (byte) (data & op); + + if(rLength==(byte)126) rMaskIndex=4; + if(rLength==(byte)127) rMaskIndex=10; + + byte[] masks = new byte[4]; + + int j=0; + int i=0; + for(i=rMaskIndex;i<(rMaskIndex+4);i++){ + masks[j] = b[i]; + j++; + } + + rDataStart = rMaskIndex + 4; + + int messLen = len - rDataStart; + + byte[] message = new byte[messLen]; + + for(i=rDataStart, j=0; i= 126 && rawData.length <= 65535){ + frame[1] = (byte) 126; + int len = rawData.length; + frame[2] = (byte)((len >> 8 ) & (byte)255); + frame[3] = (byte)(len & (byte)255); + frameCount = 4; + }else{ + frame[1] = (byte) 127; + int len = rawData.length; + frame[2] = (byte)((len >> 56 ) & (byte)255); + frame[3] = (byte)((len >> 48 ) & (byte)255); + frame[4] = (byte)((len >> 40 ) & (byte)255); + frame[5] = (byte)((len >> 32 ) & (byte)255); + frame[6] = (byte)((len >> 24 ) & (byte)255); + frame[7] = (byte)((len >> 16 ) & (byte)255); + frame[8] = (byte)((len >> 8 ) & (byte)255); + frame[9] = (byte)(len & (byte)255); + frameCount = 10; + } + + int bLength = frameCount + rawData.length; + + byte[] reply = new byte[bLength]; + + int bLim = 0; + for(int i=0; i players; + + public PlayerRepository(){ + this.players = new HashMap<>(); + } + + public void update(Player player, String lastname, String newName){ + this.players.remove(lastname); + player.setName(newName); + this.players.put(newName, player); + System.out.println("Hello "+newName); + } + + public Player getPlayer(String name){ + return this.players.get(name); + } +} diff --git a/src/be/jeffcheasey88/commands/Command.java b/src/be/jeffcheasey88/commands/Command.java new file mode 100644 index 0000000..9004578 --- /dev/null +++ b/src/be/jeffcheasey88/commands/Command.java @@ -0,0 +1,21 @@ +package be.jeffcheasey88.commands; + +import org.json.simple.JSONObject; + +import be.jeffcheasey88.Player; +import be.jeffcheasey88.PlayerRepository; + +public abstract class Command { + + private String type; + + public Command(String type){ + this.type = type; + } + + public String getType(){ + return this.type; + } + + public abstract void execute(PlayerRepository repo, Player player, JSONObject json); +} diff --git a/src/be/jeffcheasey88/commands/CommandManager.java b/src/be/jeffcheasey88/commands/CommandManager.java new file mode 100644 index 0000000..6127901 --- /dev/null +++ b/src/be/jeffcheasey88/commands/CommandManager.java @@ -0,0 +1,41 @@ +package be.jeffcheasey88.commands; + +import java.util.HashMap; +import java.util.Map; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; + +import be.jeffcheasey88.Pipeline; +import be.jeffcheasey88.Player; +import be.jeffcheasey88.PlayerRepository; + +public class CommandManager{ + + private Map commands; + private PlayerRepository repo; + + public CommandManager(PlayerRepository repo){ + this.commands = new HashMap<>(); + this.repo = repo; + } + + public void register(Command command){ + this.commands.put(command.getType(), command); + } + + public void readPipeline(Player player, Pipeline pipe){ + try { + while(true){ + String value = pipe.read(); + System.out.println(value); + JSONObject json = (JSONObject) new JSONParser().parse(value); + String type = (String) json.get("type"); + Command command = this.commands.get(type); + command.execute(repo,player, json); + } + }catch(Exception e){ + e.printStackTrace(); + } + } +} diff --git a/src/be/jeffcheasey88/commands/NameCommand.java b/src/be/jeffcheasey88/commands/NameCommand.java new file mode 100644 index 0000000..cddcabc --- /dev/null +++ b/src/be/jeffcheasey88/commands/NameCommand.java @@ -0,0 +1,18 @@ +package be.jeffcheasey88.commands; + +import org.json.simple.JSONObject; + +import be.jeffcheasey88.Player; +import be.jeffcheasey88.PlayerRepository; + +public class NameCommand extends Command{ + + public NameCommand(){ + super("name"); + } + + @Override + public void execute(PlayerRepository repo, Player player, JSONObject json){ + repo.update(player, player.getName(), (String) json.get("value")); + } +} diff --git a/src/be/jeffcheasey88/commands/OtherCommand.java b/src/be/jeffcheasey88/commands/OtherCommand.java new file mode 100644 index 0000000..9d44ef9 --- /dev/null +++ b/src/be/jeffcheasey88/commands/OtherCommand.java @@ -0,0 +1,26 @@ +package be.jeffcheasey88.commands; + +import java.util.regex.Pattern; + +import org.json.simple.JSONObject; + +import be.jeffcheasey88.Player; +import be.jeffcheasey88.PlayerRepository; + +public class OtherCommand extends Command{ + + public static void main(String[] args){ + String regex = "^\\d$"; + System.out.println(Pattern.matches(regex,"2")); + System.out.println(Pattern.matches("2-210208-1-4", regex)); + } + + public OtherCommand(){ + super("other"); + } + + @Override + public void execute(PlayerRepository repo, Player player, JSONObject json){ + + } +}