-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataObject.java
More file actions
581 lines (574 loc) · 15.7 KB
/
DataObject.java
File metadata and controls
581 lines (574 loc) · 15.7 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import java.util.Arrays;
/**
*
* @author Tanzim Ahmed Sagar
* @version 1.0
*/
public class DataObject {
/** Total amount of binary bit in an octet of IPv4*/
private final int TOTAL_BIT_IN_IPv4OCTET = 8;
/** Total amount of Octet in IPv4*/
private final int TOTAL_OCTET_IN_IPv4 = 4;
/** given IP */
private int[] gIP;
/** given IP in boolean bits */
private boolean[][] booleanGIP;
/** subnet class [ABCDEF] */
private char subnetClass;
/** default subnet mask /? format */
private int dSMslashValue;
/** default subnet mask */
private int[] dSM;
/** given subnet mask */
private int[] gSM;
/** given subnet mask /? format */
private int gSMslashValue;
/** borrowed bits */
private int s;
/** amount of subnetID can be created */
private int S;
/** amount of host bits */
private int h;
/** amount of host IP can be created */
private int hIP;
/** amount of usable host IP */
private int usableHIp;
/** Subnetwork Address, wired ID, Network ID */
private int[] sID;
/** Subnetwork Address in Binary */
private int[][] sIDinBinary;
/** Broadcast ID */
private int[] bID;
//other variables
private boolean calculateBorrowedBits = true;
private char subnetClassMaskedOctetAmount;
/**
* @since 1.0
* @return {@code int[]} gIP
*/
public int[] getGIP() {
return gIP;
}
/**
* modifier for gIP (given IP)
* also provokes modifiers for subnetwork class
* @since 1.0
* @param gIP the gIP to set
*/
public void setGIP(int[] gIP) {
this.gIP = new int[4];
boolean ipIsValid = false;
//validation
for (int octet : gIP) {
if (octet <= 255 && octet >= 0) {
ipIsValid = true;
} else {
ipIsValid = false;
}
}
if (ipIsValid) {
this.gIP = gIP;
this.setBooleanGIP();
this.setSubnetClass();
this.setSIDinBinary();
} else {
System.err.println("setting GIP failed!");
}
}
/**
* @since 2.0
* @return the gIP in boolean[4][8]
*/
protected boolean[][] getBooleanGIP() {
return booleanGIP;
}
/**
* Converts int[4] GivenIP to boolean[4][8]
* @since 2.0
*/
private void setBooleanGIP() {
this.booleanGIP = new boolean[TOTAL_OCTET_IN_IPv4][TOTAL_BIT_IN_IPv4OCTET];
for (int octetIndex = 0; octetIndex < booleanGIP.length; octetIndex++) {
for (int octetBinaryIndex = 0; octetBinaryIndex < this.toBinary(this.gIP[octetIndex]).length; octetBinaryIndex++) {
int[] temp = this.toBinary(this.gIP[octetIndex]);
if (temp[octetBinaryIndex] != 0) {
booleanGIP[octetIndex][octetBinaryIndex] = true;
} else {
booleanGIP[octetIndex][octetBinaryIndex] = false;
}
}
}
}
/**
* @since 1.0
* @return Class of the gIP (given IP) in {@code char}
*/
public char getSubnetClass() {
return subnetClass;
}
/**
* updates subnetClass based on gIP
* also triggers all the modifiers of DSM in different form.
* @since 1.0
*/
private void setSubnetClass() {
if (gIP[0] == 0) {
System.err.println("First octet of gIP is 0; network unavailable");
} else if (gIP[0] > 0 && gIP[0] < 128) {
subnetClass = 'A';
subnetClassMaskedOctetAmount = 1;
} else if (gIP[0] > 127 && gIP[0] <192) {
subnetClass = 'B';
subnetClassMaskedOctetAmount = 2;
} else if (gIP[0] > 192 && gIP[0] < 223) {
subnetClass = 'C';
subnetClassMaskedOctetAmount = 3;
} else if (gIP[0] > 224 && gIP[0] < 239) {
subnetClass = 'D';
} else if (gIP[0] > 240 && gIP[0] <= 254) {
subnetClass = 'E';
}
//provoking other modifiers
this.setDSMslashValue();
}
/**
* @since 1.0
* @return {@code int} the dSM in /? format
*/
public int getDSMslashValue() {
return dSMslashValue;
}
/**
* modifier of {@code dSMslashValue}
* @since 1.0
*/
private void setDSMslashValue() {
switch (subnetClass) {
case 'A':
this.dSMslashValue = TOTAL_BIT_IN_IPv4OCTET * 1;
break;
case 'B':
this.dSMslashValue = TOTAL_BIT_IN_IPv4OCTET * 2;
break;
case 'C':
this.dSMslashValue = TOTAL_BIT_IN_IPv4OCTET * 3;
break;
}
//provoking other modifiers
if (dSM == null) {
this.setDSM();
}
}
/**
* @since 1.0
* @return {@code int[]} the dSM
*/
public int[] getDSM() {
return dSM;
}
/**
* modifier for dSM (default Subnet Mask)
* triggers the modifier of dSMslashValue
* @since 1.0
*/
private void setDSM() {
dSM = new int[4];
int oct = 0;
switch (subnetClass) {
case 'A':
oct = 1;
break;
case 'B':
oct = 2;
break;
case 'C':
oct = 3;
break;
}
for (int index = 0; index < oct; index++) {
dSM[index] = 255;
}
//provoking other modifiers
this.setDSMslashValue();
}
/**
* @since 1.0
* @return {@code int[]} the gSM
*/
public int[] getGSM() {
return gSM;
}
/**
* @since 1.0
* @param gSM the gSM to set
*/
public void setGSM(int[] gSM) {
this.gSM = new int[4];
boolean ipIsValid;
for (int octet : gSM) {
if (octet < 256 && octet > -1) {
ipIsValid = true;
} else {
ipIsValid = false;
} // end if-else
if (ipIsValid) {
this.gSM = gSM;
} else {
System.err.println("setting GSM failed!");
} // end if-else
} //end of for loop
//provoking other modifiers
this.setGSMslashValue();
this.setSIDinBinary();
}
/**
* @since 1.0
* @return the gSMslashValue {@code int}
*/
public int getGSMslashValue() {
return gSMslashValue;
}
/**
* Modifier for gSM in /? form
* calculates value based on inputed GSM
* @since 1.0
*/
private void setGSMslashValue() {
gSMslashValue = 0;
for(int octet : this.gSM) {
int binary[] = this.toBinary(octet);
for (int digit : binary) {
if (digit != 0) {
gSMslashValue++;
} //end if
} //end binary[] iterator
}//end gSM iterator
//provoking other modifiers
int borrowedbit = this.gSMslashValue - (((int)this.gSMslashValue/this.TOTAL_BIT_IN_IPv4OCTET) * this.TOTAL_BIT_IN_IPv4OCTET);
this.setAmountOfBorrowedBits(borrowedbit);
this.setAmountOfHostBits(32 - this.gSMslashValue);
if (this.dSMslashValue != 0) {
this.setAmountOfBorrowedBits(this.gSMslashValue - this.dSMslashValue);
}
this.setSIDinBinary();
}
/**
* Modifier for gSM in /? form
* also triggers the modifier of s (Amount Of Borrowed Bits)
* and the modifier of h (Amount Of host Bits)
* @since 1.0
* @param gSMslashValue the gSM in /? format
*/
public void setGSMslashValue(int gSMslashValue) {
this.gSMslashValue = gSMslashValue;
//calculating gSM in actual form
this.gSM = new int[4];
int completeMaskedOctet = (int)this.gSMslashValue/TOTAL_BIT_IN_IPv4OCTET;
for (int index = 0; index < completeMaskedOctet; index++) {
this.gSM[index] = 255;
}
int borrowedbit = this.gSMslashValue - (completeMaskedOctet * TOTAL_BIT_IN_IPv4OCTET);
String binary = "";
for (int index = 0; index < borrowedbit; index++) {
binary = binary + "1";
}
if (binary.length() != TOTAL_BIT_IN_IPv4OCTET) {
for (int index = binary.length(); index < TOTAL_BIT_IN_IPv4OCTET; index++) {
binary = binary + "0";
}
}
this.gSM[completeMaskedOctet] = Integer.parseInt(binary,2);
//provoking other modifiers
if (this.calculateBorrowedBits) {
this.setAmountOfBorrowedBits(borrowedbit);
}
this.setAmountOfHostBits(32 - this.gSMslashValue);
if (this.dSMslashValue != 0 && this.calculateBorrowedBits) {
this.setAmountOfBorrowedBits(this.gSMslashValue - this.dSMslashValue);
}
this.setSIDinBinary();
}
/**
* @since 1.0
* @return the s (Amount Of Borrowed Bits)
*/
public int getAmountOfBorrowedBits() {
return s;
}
/**
* Modifier for s (Amount Of Borrowed Bits)
* also triggers the modifier of S (Amount of subnet)
* @since 1.0
* @param s {@code int} (Amount Of Borrowed Bits)
*/
public void setAmountOfBorrowedBits(int s) {
int temp_s = (int)s/TOTAL_BIT_IN_IPv4OCTET;
if (s > TOTAL_BIT_IN_IPv4OCTET ) {
this.calculateBorrowedBits = false;
this.setGSMslashValue(((this.subnetClassMaskedOctetAmount + temp_s) * TOTAL_BIT_IN_IPv4OCTET)+(s - (temp_s * TOTAL_BIT_IN_IPv4OCTET)));
this.calculateBorrowedBits = true;
}
this.s = s;
//provoking other modifiers
this.setAmountOfSubnet();
}
/**
* Modifier for s (Amount Of Borrowed Bits) using S (Amount Of Subnet)
* @since 1.0
*/
private void setAmountOfBorrowedBits() {
this.s = (int) Math.round(Math.log(S)/Math.log(2));
}
/**
* @since 1.0
* @return the S {@code int} (Amount Of Subnets)
*/
public int getAmountOfSubnet() {
return S;
}
/**
* modifier for S (Amount of subnet)
* triggers a private modifier of s (Amount Of Borrowed Bits)
* @since 1.0
* @param S {@code int} Amount of subnet
*/
public void setAmountOfSubnet(int S) {
this.S = S;
//provoking other modifiers
this.setAmountOfBorrowedBits();
}
/**
* modifier for S (Amount of subnetwork)
* @since 1.0
*/
private void setAmountOfSubnet() {
S = (int) Math.round(Math.pow(2, s));
}
/**
* @since 1.0
* @return the h {@code int}
*/
public int getAmountOfHostBits() {
return h;
}
/**
* modifier for h (amount of host bits)
* automatically updates the value of {@code hIP} and {@code usableHIp}
* @since 1.0
*/
public void setAmountOfHostBits(int h) {
this.h = h;
this.hIP = (int) Math.round(Math.pow(2, this.h));
this.usableHIp = this.hIP - 2;
}
/**
* @since 1.0
* @return the hIP as {@code int}
*/
public int getAmountOfHostIP() {
return hIP;
}
/**
* modifier for hIP (Amount Of Host IP)
* @since 1.0
* @param hIP amount of host IP can be created
*/
public void setAmountOfHostIP(int hIP) {
this.hIP = hIP;
this.h = (int) Math.round(Math.pow(hIP,(0-h)));
this.usableHIp = this.hIP - 2;
}
/**
* @since 1.0
* @return the usableHIp {@code int}
*/
public int getAmountOfUsableHIP() {
return usableHIp;
}
/**
* modifier for usableHIP
* also generates the value of hIP (Amount Of Host IP)
* and the value of h (amount of host bits)
* @since 1.0
* @param usableHIp the usableHIp to set
*/
public void setAmountOfUsableHIP(int usableHIp) {
this.usableHIp = usableHIp;
this.hIP = usableHIp + 2;
}
/**
* @since 1.0
* @return {@code int[]} sIDinBinary (Subnetwork Address)
*/
public int[] getSID() {
return sID;
}
/**
* modifier for sID
* generates SID from boolean[4][8]
* @since 1.0
*/
private void setSID() {
sID = new int[4];
for (int octetIndex = 0; octetIndex < this.sIDinBinary.length; octetIndex++) {
String octetInString = "";
for (int octetBinaryIndex = 0; octetBinaryIndex < this.sIDinBinary[octetIndex].length; octetBinaryIndex++) {
if (this.sIDinBinary[octetIndex][octetBinaryIndex] != 0) {
octetInString = octetInString + 1;
} else {
octetInString = octetInString + 0;
}
}
this.sID[octetIndex] = Integer.parseInt(octetInString,2);
}
}
/**
* @since 1.0
* @return {@code int[][]} sIDinBinary (Subnetwork Address in binary form)
*/
public int[][] getSIDinBinary() {
return sIDinBinary;
}
private void setSIDinBinary() {
if (this.booleanGIP != null && this.gSM != null) {
sIDinBinary = new int[TOTAL_OCTET_IN_IPv4][TOTAL_BIT_IN_IPv4OCTET];
boolean[][] binaryGSM = new boolean[TOTAL_OCTET_IN_IPv4][TOTAL_BIT_IN_IPv4OCTET];
for (int octetIndex = 0; octetIndex < binaryGSM.length; octetIndex++) {
for (int octetBinaryIndex = 0; octetBinaryIndex < this.toBinary(this.gIP[octetIndex]).length; octetBinaryIndex++) {
if (this.toBinary(this.gIP[octetIndex])[octetBinaryIndex] != 0) {
binaryGSM[octetIndex][octetBinaryIndex] = true;
} else {
binaryGSM[octetIndex][octetBinaryIndex] = false;
}
}
}
String octetInString = "";
for (int octetIndex = 0; octetIndex < this.sIDinBinary.length; octetIndex++) {
for (int octetBinaryIndex = 0; octetBinaryIndex < this.sIDinBinary[octetIndex].length; octetBinaryIndex++) {
if (booleanGIP[octetIndex][octetBinaryIndex] && binaryGSM[octetIndex][octetBinaryIndex]) {
this.sIDinBinary[octetIndex][octetBinaryIndex] = 1;
octetInString = octetInString + 1;
} else {
this.sIDinBinary[octetIndex][octetBinaryIndex] = 0;
octetInString = octetInString + 0;
}
}
}
this.setSID();
this.setBID();
}
}
/**
* Generates the value of {@code bID}
* @since 2.0
* @param bID the bID to set
*/
private void setBID() {
bID = new int[this.TOTAL_OCTET_IN_IPv4];
int classNumber = 0;
int[] temp;
switch (subnetClass) {
case 'A':
classNumber = 1;
for (int index = 0; index < classNumber; index++) { //copying sID based on the default subnet mask
bID[index] = sID[index];
}
for (int index = classNumber + 1; index < TOTAL_OCTET_IN_IPv4; index++) { //filling the rest of the bID octets
bID[index] = 255;
}
temp = Arrays.copyOf(sIDinBinary[classNumber], sIDinBinary[classNumber].length);
for (int index = TOTAL_BIT_IN_IPv4OCTET; index > s; index--) {
temp[index-1] = 1;
}
bID[classNumber] = this.toDecimal(temp);
break;
case 'B':
classNumber = 2;
for (int index = 0; index < classNumber; index++) {
bID[index] = sID[index];
}
for (int index = classNumber + 1; index < TOTAL_OCTET_IN_IPv4; index++) { //filling the rest of the bID octets
bID[index] = 255;
}
temp = Arrays.copyOf(sIDinBinary[classNumber], sIDinBinary[classNumber].length);
for (int index = TOTAL_BIT_IN_IPv4OCTET; index > s; index--) {
temp[index-1] = 1;
}
bID[classNumber] = this.toDecimal(temp);
break;
case 'C':
classNumber = 3;
for (int index = 0; index < classNumber; index++) {
bID[index] = sID[index];
}
temp = Arrays.copyOf(sIDinBinary[classNumber], sIDinBinary[classNumber].length);
for (int index = TOTAL_BIT_IN_IPv4OCTET; index > s; index--) {
temp[index-1] = 1;
}
bID[classNumber] = this.toDecimal(temp);
break;
}
}
/**
* @since 2.0
* @return the broadcast ID
*/
public int[] getBID() {
return bID;
}
//Supplementary Methods
/**
* method to turn an int[] representing an IP into string format
* @since 1.0
* @param ip IP address in {@code int[]} that needs to be in {@code string}
* @return String representation of the IP address in {@code int[]}
*/
public String ipToString(int[] ip) {
String iP = "";
int counter = 1;
for (int octet : ip) {
iP = iP + octet;
if (counter < ip.length) {
iP = iP + ".";
}
counter++;
}
return iP;
}
/**
* Method to turn an int to an int[] representing the binary format of the int.
* @since 1.0
* @param decimal Decimal version of the {@code int}
* @return {@code int[]} binary format
*/
public int[] toBinary(int decimalNumber){
int binaryReversed[] = new int[TOTAL_BIT_IN_IPv4OCTET];
int[] binary = new int[TOTAL_BIT_IN_IPv4OCTET];
int index = 0;
while(decimalNumber > 0){
binaryReversed[index++] = decimalNumber%2;
decimalNumber = decimalNumber/2;
}
int secoundIndex = 0;
for (int firstIndex = binaryReversed.length-1; firstIndex >= 0; firstIndex--) {
binary[secoundIndex] = binaryReversed[firstIndex];
secoundIndex++;
}
return binary;
}
/**
* Converts a binary number to its decimal representation.
* @param binary an array of integers representing the binary number
* @return the decimal representation of the binary number
*/
public int toDecimal(int[] binary) {
int decimal = 0;
for (int i = 0; i < binary.length; i++) {
int bit = binary[binary.length - 1 - i];
if (bit == 1) {
decimal += Math.pow(2, i);
}
}
return decimal;
}
}// End of Class