forked from srieger/verteilte-systeme-bsc-ai-examples
Sebastian Rieger
7 years ago
4 changed files with 249 additions and 178 deletions
-
71VerteilteSysteme-Examples/src/verteiltesysteme/rmi/RMIEchoServer.java
-
148VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterClient.java
-
122VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterClientPing.java
-
86VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterServer.java
@ -1,36 +1,37 @@ |
|||
package verteiltesysteme.rmi; |
|||
|
|||
import java.rmi.*; |
|||
import java.rmi.server.*; |
|||
import java.rmi.registry.*; |
|||
|
|||
public class RMIEchoServer implements RMIEchoInterface { |
|||
public RMIEchoServer() {} |
|||
|
|||
public static void main(String args[]) { |
|||
|
|||
try { |
|||
RMIEchoServer obj = new RMIEchoServer(); |
|||
RMIEchoInterface stub = (RMIEchoInterface) UnicastRemoteObject.exportObject(obj, 0); |
|||
|
|||
// Bind the remote object's stub in the registry |
|||
Registry registry = LocateRegistry.getRegistry(); |
|||
registry.bind("RMIEchoInterface", stub); |
|||
|
|||
System.err.println("Server ready"); |
|||
} catch (Exception e) { |
|||
System.err.println("Server exception: " + e.toString()); |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public String toLowerCase(String input) throws RemoteException { |
|||
return input.toLowerCase(); |
|||
} |
|||
|
|||
@Override |
|||
public String toUpperCase(String input) throws RemoteException { |
|||
return input.toUpperCase(); |
|||
} |
|||
package verteiltesysteme.rmi; |
|||
|
|||
import java.rmi.RemoteException; |
|||
import java.rmi.registry.LocateRegistry; |
|||
import java.rmi.registry.Registry; |
|||
import java.rmi.server.UnicastRemoteObject; |
|||
|
|||
public class RMIEchoServer implements RMIEchoInterface { |
|||
public RMIEchoServer() {} |
|||
|
|||
public static void main(String args[]) { |
|||
|
|||
try { |
|||
RMIEchoServer obj = new RMIEchoServer(); |
|||
RMIEchoInterface stub = (RMIEchoInterface) UnicastRemoteObject.exportObject(obj, 0); |
|||
|
|||
// Bind the remote object's stub in the registry |
|||
Registry registry = LocateRegistry.getRegistry(); |
|||
registry.bind("RMIEchoInterface", stub); |
|||
|
|||
System.err.println("Server ready"); |
|||
} catch (Exception e) { |
|||
System.err.println("Server exception: " + e.toString()); |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public String toLowerCase(String input) throws RemoteException { |
|||
return input.toLowerCase(); |
|||
} |
|||
|
|||
@Override |
|||
public String toUpperCase(String input) throws RemoteException { |
|||
return input.toUpperCase(); |
|||
} |
|||
} |
@ -1,55 +1,93 @@ |
|||
package verteiltesysteme.socket.timecounter; |
|||
|
|||
import java.io.BufferedReader; |
|||
import java.io.InputStreamReader; |
|||
import java.net.DatagramPacket; |
|||
import java.net.DatagramSocket; |
|||
import java.net.InetAddress; |
|||
import java.text.DateFormat; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.Date; |
|||
|
|||
class UDPTimeCounterClient { |
|||
public static void main(String args[]) throws Exception { |
|||
String hostname = ""; |
|||
int udpPort = 36038; |
|||
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); |
|||
DatagramSocket clientSocket = new DatagramSocket(); |
|||
byte[] sendData = new byte[1024]; |
|||
byte[] receiveData = new byte[1024]; |
|||
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); |
|||
|
|||
System.out.println("Bitte geben Sie die Server-Adresse ein: "); |
|||
hostname = inFromUser.readLine(); |
|||
InetAddress IPAddress = InetAddress.getByName(hostname); |
|||
System.out.println("Bitte geben Sie eine Zahl ein: "); |
|||
Long number = new Long(inFromUser.readLine()); |
|||
sendData = number.toString().getBytes(); |
|||
|
|||
System.out.println(System.lineSeparator()); |
|||
long timestampStart = System.currentTimeMillis(); |
|||
System.out.println("Zeit vor Versand = " + timestampStart + " ms, " + df.format(new Date(timestampStart))); |
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort); |
|||
clientSocket.send(sendPacket); |
|||
long timestampSent = System.currentTimeMillis(); |
|||
System.out.println("Zeit nach Versand = " + timestampSent + " ms, " + df.format(new Date(timestampSent))); |
|||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
|
|||
clientSocket.receive(receivePacket); |
|||
|
|||
long timestampEnd = System.currentTimeMillis(); |
|||
System.out.println("Zeit nach Empfang = " + timestampEnd + " ms, " + df.format(new Date(timestampEnd))); |
|||
long delay = timestampEnd - timestampStart; |
|||
System.out.println("Delay = " + delay + " ms"); |
|||
System.out.println(System.lineSeparator()); |
|||
|
|||
String result = new String(receivePacket.getData()); |
|||
System.out.println("Ausgabe vom Server:" + System.lineSeparator() + result); |
|||
long timestampServer = new Long(result.split(" ")[2]).longValue(); |
|||
float oneWayDelayEstimate = (timestampEnd - timestampSent) / 2; |
|||
float timestampSkew = timestampServer - timestampSent - oneWayDelayEstimate; |
|||
System.out.println("Zeitversatz zwischen Server und Client ca. = " + timestampSkew + " ms"); |
|||
System.out.println(System.lineSeparator()); |
|||
clientSocket.close(); |
|||
} |
|||
} |
|||
package verteiltesysteme.socket.timecounter; |
|||
|
|||
import java.io.BufferedReader; |
|||
import java.io.InputStreamReader; |
|||
import java.net.DatagramPacket; |
|||
import java.net.DatagramSocket; |
|||
import java.net.InetAddress; |
|||
import java.text.DateFormat; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.Date; |
|||
|
|||
class UDPTimeCounterClient { |
|||
|
|||
/** |
|||
* Gibt einen Zeitstempel (timestamp) formatiert auf der Konsole aus. Der |
|||
* Zeitstempel wird in aktuellen Millisekunden seit 1.1.1970 (UTC) und als |
|||
* formatiertes Datum inkl. Uhrzeit ausgegeben. Der Ausgabe wird der übergebene |
|||
* String outputPrefix vorangestellt. |
|||
* |
|||
* @param outputPrefix |
|||
* String der als Beschreibung des Zeitpunkts vor die Ausgabe des |
|||
* timestamp gestellt wird. |
|||
* @param timestamp |
|||
* Zeitstempel basierend auf System.currentTimeMillis() |
|||
*/ |
|||
private static void printFormattedTimestamp(String outputPrefix, long timestamp) { |
|||
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); |
|||
System.out.println(outputPrefix + " = " + timestamp + " ms, " + df.format(new Date(timestamp))); |
|||
} |
|||
|
|||
public static void main(String args[]) throws Exception { |
|||
String hostname = ""; |
|||
int udpPort = 36038; |
|||
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); |
|||
byte[] sendData = new byte[1024]; |
|||
byte[] receiveData = new byte[1024]; |
|||
long timestampStart, timestampSent, timestampEnd, delay; |
|||
|
|||
// Socket erzeugen |
|||
DatagramSocket clientSocket = new DatagramSocket(); |
|||
|
|||
// Hostname vom Benutzer anfragen auf dem der Server läuft |
|||
System.out.println("Bitte geben Sie die Server-Adresse ein: "); |
|||
hostname = inFromUser.readLine(); |
|||
InetAddress IPAddress = InetAddress.getByName(hostname); |
|||
|
|||
// Zahl vom Benutzer abfragen, die dann vom Server auf den aktuellen Zählerstand |
|||
// addiert wird |
|||
System.out.println("Bitte geben Sie eine Zahl ein: "); |
|||
Long number = new Long(inFromUser.readLine()); |
|||
sendData = number.toString().getBytes(); |
|||
|
|||
// Zahl an Server senden |
|||
System.out.println(System.lineSeparator()); |
|||
timestampStart = System.currentTimeMillis(); |
|||
printFormattedTimestamp("Zeit vor Versand", timestampStart); |
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort); |
|||
clientSocket.send(sendPacket); |
|||
timestampSent = System.currentTimeMillis(); |
|||
printFormattedTimestamp("Zeit nach Versand", timestampSent); |
|||
|
|||
// Antwort mit neuem Zählerstand und Timestamp vom Server empfangen |
|||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
clientSocket.receive(receivePacket); |
|||
timestampEnd = System.currentTimeMillis(); |
|||
printFormattedTimestamp("Zeit nach Empfang", timestampEnd); |
|||
|
|||
// Delay als Zeit zwischen timestampSent und timestampEnd berechnen und |
|||
// ausgeben |
|||
delay = timestampEnd - timestampStart; |
|||
System.out.println("Delay = " + delay + " ms"); |
|||
System.out.println(System.lineSeparator()); |
|||
|
|||
// Antwort vom Server auswerten und ausgeben |
|||
String result = new String(receivePacket.getData()); |
|||
System.out.println("Ausgabe vom Server:" + System.lineSeparator() + result); |
|||
// Vom Server übergebenen Zeitstempel aus der Antwort herausschneiden |
|||
// |
|||
// Format Ausgabe vom Server "<Zählerstand> Zeit: <Zeitstempel> <Datum/Uhrzeit>" |
|||
// Beispiel: 60 Zeit: 1510735274021 15.11.2017 08:41:14 |
|||
long timestampServer = new Long(result.split(" ")[2]).longValue(); |
|||
// One-Way-Delay (Bitverzögerung) aus Rount-Trip-Time (RTT) ermitteln |
|||
float oneWayDelayEstimate = (timestampEnd - timestampSent) / 2; |
|||
// Zeitabweichung zwischen Client und Server ermitteln (Zeitstempel vom Server |
|||
// minus Zeitstempel bei Versand am Client minus One-Way-Delay) |
|||
float timestampSkew = timestampServer - timestampSent - oneWayDelayEstimate; |
|||
System.out.println("Zeitversatz zwischen Server und Client ca. = " + timestampSkew + " ms"); |
|||
System.out.println(System.lineSeparator()); |
|||
|
|||
// Socket schließen |
|||
clientSocket.close(); |
|||
} |
|||
} |
@ -1,52 +1,70 @@ |
|||
package verteiltesysteme.socket.timecounter; |
|||
|
|||
import java.io.BufferedReader; |
|||
import java.io.InputStreamReader; |
|||
import java.net.DatagramPacket; |
|||
import java.net.DatagramSocket; |
|||
import java.net.InetAddress; |
|||
|
|||
class UDPTimeCounterClientPing { |
|||
public static void main(String args[]) throws Exception { |
|||
String hostname = ""; |
|||
int udpPort = 36038; |
|||
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); |
|||
DatagramSocket clientSocket = new DatagramSocket(); |
|||
byte[] sendData = new byte[1024]; |
|||
byte[] receiveData = new byte[1024]; |
|||
|
|||
System.out.println("Bitte geben Sie die Server-Adresse ein: "); |
|||
hostname = inFromUser.readLine(); |
|||
InetAddress IPAddress = InetAddress.getByName(hostname); |
|||
System.out.println("Bitte geben Sie die Anzahl gewünschter Pings ein: "); |
|||
Long pingCount = new Long(inFromUser.readLine()); |
|||
Long number = new Long(1); |
|||
sendData = number.toString().getBytes(); |
|||
|
|||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
|
|||
long average = 0; |
|||
for (long i = 0; i < pingCount; i++) |
|||
{ |
|||
long timestampStart = System.currentTimeMillis(); |
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort); |
|||
clientSocket.send(sendPacket); |
|||
receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
|
|||
clientSocket.receive(receivePacket); |
|||
|
|||
long timestampEnd = System.currentTimeMillis(); |
|||
long delay = timestampEnd - timestampStart; |
|||
average += delay; |
|||
System.out.println("Delay = " + delay + " ms"); |
|||
Thread.sleep(1000); |
|||
} |
|||
System.out.println("Durchschnitt: " + (average / (float)pingCount) + " ms"); |
|||
System.out.println(System.lineSeparator()); |
|||
|
|||
String result = new String(receivePacket.getData()); |
|||
System.out.println("Ausgabe vom Server:" + System.lineSeparator() + result); |
|||
System.out.println(System.lineSeparator()); |
|||
clientSocket.close(); |
|||
} |
|||
} |
|||
package verteiltesysteme.socket.timecounter; |
|||
|
|||
import java.io.BufferedReader; |
|||
import java.io.InputStreamReader; |
|||
import java.net.DatagramPacket; |
|||
import java.net.DatagramSocket; |
|||
import java.net.InetAddress; |
|||
|
|||
class UDPTimeCounterClientPing { |
|||
public static void main(String args[]) throws Exception { |
|||
String hostname = ""; |
|||
int udpPort = 36038; |
|||
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); |
|||
byte[] sendData = new byte[1024]; |
|||
byte[] receiveData = new byte[1024]; |
|||
|
|||
// Socket erzeugen |
|||
DatagramSocket clientSocket = new DatagramSocket(); |
|||
|
|||
// Hostname vom Benutzer anfragen auf dem der Server läuft |
|||
System.out.println("Bitte geben Sie die Server-Adresse ein: "); |
|||
hostname = inFromUser.readLine(); |
|||
InetAddress IPAddress = InetAddress.getByName(hostname); |
|||
|
|||
// Gewünschte Anzahl von Pings vom Benutzer abfragen |
|||
System.out.println("Bitte geben Sie die Anzahl gewünschter Pings ein: "); |
|||
Long pingCount = new Long(inFromUser.readLine()); |
|||
// bei jedem Ping wird Zählerstand auf dem Server um 1 erhöht |
|||
Long number = new Long(1); |
|||
sendData = number.toString().getBytes(); |
|||
|
|||
// Paket für Empfang erzeugen |
|||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
|
|||
long average = 0; // Variable zur Ermittlung des durchschnittlichen Delays |
|||
|
|||
// Pings ausführen |
|||
for (long i = 0; i < pingCount; i++) { |
|||
// Anfrage an Server senden |
|||
long timestampStart = System.currentTimeMillis(); |
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort); |
|||
clientSocket.send(sendPacket); |
|||
|
|||
// Antwort von Server empfangen |
|||
receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
clientSocket.receive(receivePacket); |
|||
// Delay aus Zeit zwischen Zeitstempeln vor Versand und nach Empfang berechnen |
|||
long timestampEnd = System.currentTimeMillis(); |
|||
long delay = timestampEnd - timestampStart; |
|||
// Delay auf Variable für Ermittlung des durchschnittlichen Delays addieren |
|||
average += delay; |
|||
System.out.println("Delay = " + delay + " ms"); |
|||
|
|||
// 1 Sekunde warten |
|||
Thread.sleep(1000); |
|||
} |
|||
|
|||
// Durchscnitt berechnen und ausgeben |
|||
System.out.println("Durchschnitt: " + (average / (float) pingCount) + " ms"); |
|||
System.out.println(System.lineSeparator()); |
|||
|
|||
// Antwort vom Server ausgeben |
|||
String result = new String(receivePacket.getData()); |
|||
System.out.println("Letzte Ausgabe vom Server:" + System.lineSeparator() + result); |
|||
System.out.println(System.lineSeparator()); |
|||
|
|||
// Socket schließen |
|||
clientSocket.close(); |
|||
} |
|||
} |
@ -1,36 +1,50 @@ |
|||
package verteiltesysteme.socket.timecounter; |
|||
|
|||
import java.net.DatagramPacket; |
|||
import java.net.DatagramSocket; |
|||
import java.net.InetAddress; |
|||
import java.text.DateFormat; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.Date; |
|||
|
|||
class UDPTimeCounterServer { |
|||
private static long counter = 0; |
|||
|
|||
public static void main(String args[]) throws Exception { |
|||
int udpPort = 36038; |
|||
@SuppressWarnings("resource") |
|||
DatagramSocket serverSocket = new DatagramSocket(udpPort); |
|||
byte[] receiveData = new byte[1024]; |
|||
byte[] sendData = new byte[1024]; |
|||
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); |
|||
|
|||
while (true) { |
|||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
serverSocket.receive(receivePacket); |
|||
long number = new Long(new String(receivePacket.getData(), 0, receivePacket.getLength())); |
|||
|
|||
InetAddress IPAddress = receivePacket.getAddress(); |
|||
int clientSrcPort = receivePacket.getPort(); |
|||
|
|||
counter += number; |
|||
|
|||
sendData = (Long.toString(counter) + " Zeit: " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis()))).getBytes(); |
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, clientSrcPort); |
|||
serverSocket.send(sendPacket); |
|||
} |
|||
} |
|||
} |
|||
package verteiltesysteme.socket.timecounter; |
|||
|
|||
import java.net.DatagramPacket; |
|||
import java.net.DatagramSocket; |
|||
import java.net.InetAddress; |
|||
import java.text.DateFormat; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.Date; |
|||
|
|||
class UDPTimeCounterServer { |
|||
// Variable für Zählerstand des Servers, dieser Zähler wird durch Anfragen der |
|||
// Clients erhöht |
|||
private static long counter = 0; |
|||
|
|||
public static void main(String args[]) throws Exception { |
|||
int udpPort = 36038; |
|||
byte[] receiveData = new byte[1024]; |
|||
byte[] sendData = new byte[1024]; |
|||
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); |
|||
|
|||
// Socket erzeugen |
|||
// Warnung, dass Socket nicht geschlossen wird unterdrücken ;) |
|||
@SuppressWarnings("resource") |
|||
DatagramSocket serverSocket = new DatagramSocket(udpPort); |
|||
System.out.println("Server ready... waiting for packets..."); |
|||
|
|||
while (true) { |
|||
// Anfrage vom Client empfangen |
|||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
serverSocket.receive(receivePacket); |
|||
|
|||
// Vom Client gesendete Zahl einlesen und auf aktuellen Zählerstand addieren |
|||
long number = new Long(new String(receivePacket.getData(), 0, receivePacket.getLength())); |
|||
counter += number; |
|||
|
|||
// Absender IP-Adresse und Port für Rückversand (Antwort) ermitteln |
|||
InetAddress IPAddress = receivePacket.getAddress(); |
|||
int clientSrcPort = receivePacket.getPort(); |
|||
//System.out.println("Anfrage von Client " + IPAddress + ":" + clientSrcPort + " Zahl: " + number + " Zählerstand: " + counter); |
|||
|
|||
// Antwort vom Server senden |
|||
String output = (Long.toString(counter) + " Zeit: " + System.currentTimeMillis() + " " |
|||
+ df.format(new Date(System.currentTimeMillis()))); |
|||
//System.out.println("Antwort des Servers: " + output); |
|||
sendData = output.getBytes(); |
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, clientSrcPort); |
|||
serverSocket.send(sendPacket); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue