package netzprog01_j_ultimate; import java.net.*; /** * * @author Daniel Leese */ public class Receive implements Runnable { DatagramPacket dp; Sockenschuss s1 = null; HMap h1 = null; String progKey = "012345678"; public Receive() { } private boolean forMe(String str) { for (int i = str.length() - 1; i >= 0; i--) { if (str.charAt(i) == '|') { str = str.substring(0, i); break; } } String progKey = new String(""); String clientKey = new String(""); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '|') { progKey = str.substring(0, i); clientKey = str.substring(i + 1); break; } } if (progKey.compareTo(s1.progKey) != 0) { return false; } if (clientKey.compareTo(s1.clientKey) == 0) { return false; } return true; } /** * wird ausgefuehrt wenn thread gestartet wird */ public void run() { s1 = Sockenschuss.getInstance(); h1 = HMap.getInstance(); String received = new String(""); // uebertragener String DatagramPacket recv = null; while (true) { recv = null; received = ""; recv = s1.receive(); for (int i = 0; i < recv.getLength(); i++) { received += (char) recv.getData()[i]; } if (forMe(received) == false) { // wenn programmKey oder ClientKey falsch sind continue; } received = received.substring(16); // progKey und clientKey aus recieved entfernen if (received.compareTo("getHashMap") == 0) { System.out.println(s1.clientKey + "\t" + "sende HashMap"); String send = new String(h1.serializeHashmap()); s1.send(send); continue; } if (received.charAt(0) == '(' && received.charAt(received.length() - 1) == ')') { int help = 0; for (int i = 0; i < received.length(); i++) { if (received.charAt(i) == '=') { help = i; break; } } h1.insertOnly(received.substring(1, help), received.substring(help + 1, received.length() - 1)); continue; } if (received.charAt(0) == '<' && received.charAt(received.length() - 1) == '>') { h1.deleteOnly(received.substring(1, received.length() - 1)); continue; } if (received.substring(0, 7).contentEquals("serHash")) { h1.unserializeHashmap(received.substring(7)); continue; } } } }