From 947651987a8f213075dff16940f07f543f39b99b Mon Sep 17 00:00:00 2001 From: Kuba Date: Sun, 21 May 2017 14:32:52 +0200 Subject: [PATCH] =?UTF-8?q?sko=C5=84czona=20agregacja=20sieci,=20wypada?= =?UTF-8?q?=C5=82oby=20przetestowa=C4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/netKnow/Class/IP.java | 37 +++++++++++++++-- src/netKnow/Code/NetworkAggregation.java | 52 ++++++++++++++++++++++++ src/netKnow/Main.java | 8 ++-- 3 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 src/netKnow/Code/NetworkAggregation.java diff --git a/src/netKnow/Class/IP.java b/src/netKnow/Class/IP.java index 43a2830..72320f1 100644 --- a/src/netKnow/Class/IP.java +++ b/src/netKnow/Class/IP.java @@ -1,6 +1,6 @@ package netKnow.Class; -public class IP { +public class IP implements Comparable { private int [] ipArray; private int [] maskArray; private String fullIPAdress []; @@ -15,6 +15,8 @@ public IP(String [] fullIPAdress){ ipArray = new int[4]; maskArray = new int[4]; this.fullIPAdress = fullIPAdress; + convertStringToIPAdress(fullIPAdress); + convertStringToMask(); } private void convertStringToIPAdress(String [] fullIPAdress){ @@ -23,7 +25,7 @@ private void convertStringToIPAdress(String [] fullIPAdress){ } } - private void convertStringToMask(String mask){ + private void convertStringToMask(){ int n = Integer.parseInt(fullIPAdress[4]); for(int i = 0; i < n; i++){ maskArray[i/8] |= (128 >> (i%8)); @@ -31,8 +33,6 @@ private void convertStringToMask(String mask){ } public void computeData(){ - convertStringToIPAdress(fullIPAdress); - convertStringToMask(fullIPAdress[4]); network = computeNetwork(); broadcast = computeBroadcast(); numberOfHosts = Integer.toString(numberOfHosts()); @@ -77,6 +77,22 @@ private String maxHost() return (tmp1[0] + "." + tmp1[1] + "." + tmp1[2] + "." + tmp2); } + public int compareTo(IP ip){ + if(this.ipArray[0] == ip.ipArray[0] && this.ipArray[1] == ip.ipArray[1] && this.ipArray[2] == ip.ipArray[2] + && this.ipArray[3] == ip.ipArray[3]){ + return 0; + } + else if((this.ipArray[0] > ip.ipArray[0]) || (this.ipArray[0] == ip.ipArray[0] && this.ipArray[1] > ip.ipArray[1]) || + (this.ipArray[0] == ip.ipArray[0] && this.ipArray[1] == ip.ipArray[1] && this.ipArray[2] > ip.ipArray[2]) || + (this.ipArray[0] == ip.ipArray[0] && this.ipArray[1] == ip.ipArray[1] && this.ipArray[2] == ip.ipArray[2] + && this.ipArray[3] > ip.ipArray[3])){ + return 1; + } + else{ + return -1; + } + } + public String getNetwork() { return network; } @@ -96,4 +112,17 @@ public String getMinHost() { public String getNumberOfHosts() { return numberOfHosts; } + + public String getIP(){ + return (ipArray[0]+ "." + ipArray[1]+ "." + ipArray[2]+ "." + ipArray[3] + "/" + fullIPAdress[4]); + } + + public int[] getIPAsInt(){ return ipArray; } + + public int getMaskAsInt(){return Integer.parseInt(fullIPAdress[4]);} + + public void setMask(int k){ + fullIPAdress[4] = Integer.toString(k); + convertStringToMask(); + }; } diff --git a/src/netKnow/Code/NetworkAggregation.java b/src/netKnow/Code/NetworkAggregation.java new file mode 100644 index 0000000..f523d86 --- /dev/null +++ b/src/netKnow/Code/NetworkAggregation.java @@ -0,0 +1,52 @@ +package netKnow.Code; + +import netKnow.Class.IP; + +import java.util.Arrays; + + +/** + * Created by Kuba on 16.05.2017. + */ +public class NetworkAggregation { + private IP[] IPArray; + + public NetworkAggregation(IP[] Array) { + this.IPArray = Array; + } + + public IP[] aggregateNetwork() { + int [][] tmp = new int[IPArray.length][4]; + IP [] result = new IP[IPArray.length]; + for (int i = 0; i < IPArray.length; i++) { + tmp[i] = IPArray[i].getIPAsInt(); + } + Arrays.sort(IPArray); + int k = 0; + for(int i = 0; i < IPArray.length; i++){ + int [] tmp2 = tmp[i]; + int [] tmp3 = IPArray[i].getIPAsInt(); + int j = IPArray[i].getMaskAsInt() - 1; + int mask = j+1; + String [] tmpString1 = {Integer.toString(tmp3[0]), Integer.toString(tmp3[1]), Integer.toString(tmp3[2]), + Integer.toString(tmp3[3]), Integer.toString(mask)}; + result[k] = new IP(tmpString1); + while(j > 0) { + tmp2[j / 8] |= 128 >> j % 8; + String[] tmpString = {Integer.toString(tmp2[0]), Integer.toString(tmp2[1]), Integer.toString(tmp2[2]), + Integer.toString(tmp2[3]), Integer.toString(j)}; + IP newIP = new IP(tmpString); + int key = Arrays.binarySearch(IPArray, newIP); + if (key > 0 && key < IPArray.length && IPArray[key].compareTo(newIP) == 0) { + i = key; + mask = j; + } + if(i < IPArray.length - 1) + result[k].setMask(mask); + j--; + } + k++; + } + return result; + } +} \ No newline at end of file diff --git a/src/netKnow/Main.java b/src/netKnow/Main.java index d36e14c..fef2579 100644 --- a/src/netKnow/Main.java +++ b/src/netKnow/Main.java @@ -6,6 +6,8 @@ import javafx.stage.Stage; import netKnow.scene.LoginScene; import netKnow.scene.RoutingScene; +import netKnow.Class.IP; +import netKnow.Code.NetworkAggregation; public class Main extends Application { @@ -18,10 +20,10 @@ public void start(Stage primaryStage) throws Exception{ window.setTitle("netKnow - aplication that will change your life"); Scene scene = new Scene(new VBox(), 1000, 800); window.setScene(scene); - //new LoginScene(scene); - new RoutingScene(scene); + new LoginScene(scene); + //new RoutingScene(scene); window.show(); - //window.setFullScreen(true); + //window.setFullScreen(true);*/ }