-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerwaltung.java
172 lines (152 loc) · 6.76 KB
/
Verwaltung.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import abiturklassen.Graph;
import abiturklassen.List;
import abiturklassen.Vertex;
import abiturklassen.Edge;
// Praxisprojekt zu nicht linearen Datenstrukturen
public class Verwaltung {
/*
* Funktionen
* 1. Abfrage nach gewünstem Algorithmus über einen Binärbaum
*/
Graph graph;
BinaryConverter converter;
public Verwaltung() {
// Konstruktor
graph = new Graph();
converter = new BinaryConverter();
// Netzwerkgeräte
// Insert new network devices with this format: {"hostname": "vendor"}
// IP and MAC address are generated randomly / iterative
String devices[][] = {
{ "Lukas-Mac", "Apfle" },
{ "Alex-Mac", "Apfle" },
{ "Wardemanns-Backdoor", "Microsoft" },
{ "Bastis-Thinkpad", "Lenovo" },
{ "Lennarts-Surface", "Minimalsoft" }
};
System.out.println("Adding " + devices.length + " devices to Graph");
for (int i = 0; i < devices.length; i++) {
graph.addVertex(
new Netzwerkgerät(devices[i][0], generateRandomMac(), "192.168.1." + (i + 1), devices[i][1]));
}
System.out.println("Adding random latencys between " + devices.length + " devices");
// Netzwerkverbindungen
// Insert new network connections with this format: {"device1": "device2"}
// Latency is generated randomly
String connections[][] = {
{ "Lukas-Mac", "Alex-Mac" },
{ "Lukas-Mac", "Wardemanns-Backdoor" },
{ "Lukas-Mac", "Bastis-Thinkpad" },
{ "Lukas-Mac", "Lennarts-Surface" },
{ "Alex-Mac", "Wardemanns-Backdoor" },
{ "Alex-Mac", "Bastis-Thinkpad" },
{ "Alex-Mac", "Lennarts-Surface" },
{ "Wardemanns-Backdoor", "Bastis-Thinkpad" },
{ "Wardemanns-Backdoor", "Lennarts-Surface" },
{ "Bastis-Thinkpad", "Lennarts-Surface" }
};
for (int i = 0; i < connections.length; i++) {
Edge edge = new Edge(graph.getVertex(connections[i][0]), graph.getVertex(connections[i][1]),
(int) (Math.random() * 100));
graph.addEdge(edge);
}
BinaryConverter converter = new BinaryConverter();
converter.convertStringToBinary("Bababui");
}
public void printAdjacencyList() {
printAdjacencyList("");
}
public void printAdjacencyList(String weightUnit) {
printAdjacencyList(graph, weightUnit);
}
public void printAdjacencyList(Graph graph, String weightUnit) {
// String leftAlignFormat = "| %-15s | %-4d |%n";
// System.out.format("+---------+------+%n");
// String header = "| Knoten ";
// List<Vertex> vertices = graph.getVertices();
// vertices.toFirst();
// while (vertices.hasAccess()) {
// Netzwerkgerät device = (Netzwerkgerät) vertices.getContent();
// header += " | " + device.getHostname();
// vertices.next();
// }
// System.out.format(header + "%n");
// System.out.format("+---------+------+%n");
// vertices.toFirst();
// while (vertices.hasAccess()) {
// List<Vertex> geraete = graph.getVertices();
// geraete.toFirst();
// String weights = "";
// while (geraete.hasAccess()) {
// //weights += " | " + graph.getEdge(vertices.getContent(), geraete.getContent()).getWeight() + weightUnit;
// geraete.next();
// }
// System.out.format(leftAlignFormat, ((Netzwerkgerät) vertices.getContent()).getHostname(), weights);
// vertices.next();
// }
// System.out.format("+-----------------+------+%n");
List<Vertex> vertices = graph.getVertices();
String result = "";
vertices.toFirst();
while(vertices.hasAccess()){
result += ((Netzwerkgerät)(vertices.getContent())).getHostname();
// Iterate all connected devices and list them with their weight
List<Vertex> temp = graph.getNeighbours(vertices.getContent());
temp.toFirst();
int length = 0;
while(temp.hasAccess()){
length++;
temp.next();
}
result += " (Anzahl Nachbarn: " + length + ")";
List<Vertex> connectedDevices = graph.getNeighbours(vertices.getContent());
connectedDevices.toFirst();
while(connectedDevices.hasAccess()){
result += " -> " + ((Netzwerkgerät)(connectedDevices.getContent())).getHostname() + " (" + graph.getEdge(vertices.getContent(), connectedDevices.getContent()).getWeight() + weightUnit + ")";
connectedDevices.next();
}
result += "\n";
vertices.next();
}
System.out.println("Here we go, that's your network: ");
System.out.println(result);
}
private String generateRandomMac() {
String mac = "";
for (int i = 0; i < 6; i++) {
mac += Integer.toHexString((int) (Math.random() * 16));
}
return mac;
}
public static void main(String[] args) {
Verwaltung verwaltung = new Verwaltung();
verwaltung.printAdjacencyList("ms");
verwaltung.sendeNachricht("Bababui", "Lennarts-Surface", "Lukas-Mac");
}
public void sendeNachricht(String pNachricht, String pEmpfaengerName, String pAbsenderName) {
Netzwerkgerät empfaenger = (Netzwerkgerät) graph.getVertex(pEmpfaengerName);
Netzwerkgerät absender = (Netzwerkgerät) graph.getVertex(pAbsenderName);
String binary=converter.convertStringToBinary(pNachricht);
// Dijkstra from absender to empfaenger
List<Edge> pfad = new Dijkstra(graph, pAbsenderName, pEmpfaengerName).runDijkstra();
List<Netzwerkgerät> pfad2 = new List<>();
pfad.toFirst();
while(pfad.hasAccess()) {
if (pfad2.getContent() == null) {
if (pfad.getContent().getVertices()[0].equals(absender))
pfad2.append((Netzwerkgerät)pfad.getContent().getVertices()[1]);
else
pfad2.append((Netzwerkgerät)pfad.getContent().getVertices()[0]);
pfad.next();
continue;
}
if (pfad2.getContent().equals((Netzwerkgerät)pfad.getContent().getVertices()[0])) {
pfad2.append((Netzwerkgerät)pfad.getContent().getVertices()[1]);
} else {
pfad2.append((Netzwerkgerät)pfad.getContent().getVertices()[0]);
}
pfad.next();
}
absender.sendeNachrichtAn(binary, pfad2);
}
}