Skip to content

Commit a000a28

Browse files
committed
Update
1 parent 7fc39b8 commit a000a28

File tree

1 file changed

+63
-38
lines changed

1 file changed

+63
-38
lines changed

src/components/Assignment3/UserInput.js

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ export default class UserInput extends React.Component {
1010
classful: true,
1111
classNameValue: "A",
1212
numberOfHostsNeeded: 0,
13+
generatedIPAddress: "ip...",
1314
addresses: JSON.stringify(addresses)
1415
}
16+
localStorage.clear();
1517
}
1618

1719
handleclassNameChange(event) {
1820
this.setState({classNameValue: event.target.value})
1921
}
2022

2123
handleclassNameRadioButtonChange(bool) {
22-
this.setState({classNameful: bool});
24+
this.setState({classful: bool});
2325
}
2426

2527
handleSubmit(event) {
@@ -31,10 +33,6 @@ export default class UserInput extends React.Component {
3133
return Math.floor(Math.random() * Math.floor(max));
3234
}
3335

34-
dec2bin(dec) {
35-
return (dec >>> 0).toString(2);
36-
}
37-
3836
generateIPv4Address() {
3937
console.log(this.state.addresses);
4038
let map;
@@ -45,37 +43,51 @@ export default class UserInput extends React.Component {
4543
}
4644
let randomAddress;
4745
let r = 0;
48-
if (this.state.classful) {
49-
if (this.state.classNameValue === "A") {
50-
r += (this.getRandomInt(128)) << 24;
51-
} else if (this.state.classNameValue === "B") {
52-
r += (this.getRandomInt(64) + 128) << 24;
53-
r += (this.getRandomInt(256) << 16);
54-
} else if (this.state.classNameValue === "C") {
55-
r += (this.getRandomInt(32) + 192) << 24;
56-
r += (this.getRandomInt(256)) << 16;
57-
r += (this.getRandomInt(256)) << 8;
58-
} else if (this.state.classNameValue === "D") {
59-
r += (this.getRandomInt(16) + 224) << 24;
60-
r += (this.getRandomInt(256)) << 16;
61-
r += (this.getRandomInt(256)) << 8;
62-
r += (this.getRandomInt(256));
63-
} else if (this.state.classNameValue === "E") {
64-
r += Math.ceil(Math.log2(this.state.numberOfHostsNeeded));
65-
console.elog(r);
46+
do {
47+
if (this.state.classful) {
48+
console.log("classful!");
49+
if (this.state.classNameValue === "A") {
50+
r += (this.getRandomInt(128)) << 24;
51+
} else if (this.state.classNameValue === "B") {
52+
r += (this.getRandomInt(64) + 128) << 24;
53+
r += (this.getRandomInt(256) << 16);
54+
} else if (this.state.classNameValue === "C") {
55+
r += (this.getRandomInt(32) + 192) << 24;
56+
r += (this.getRandomInt(256)) << 16;
57+
r += (this.getRandomInt(256)) << 8;
58+
} else if (this.state.classNameValue === "D") {
59+
r += (this.getRandomInt(16) + 224) << 24;
60+
r += (this.getRandomInt(256)) << 16;
61+
r += (this.getRandomInt(256)) << 8;
62+
r += (this.getRandomInt(256));
63+
} else if (this.state.classNameValue === "E") {
64+
console.log("Cannot assign class E!");
65+
}
66+
} else {
67+
let prefixBits = Math.ceil(Math.log2(this.state.numberOfHostsNeeded));
68+
r += this.getRandomInt(Math.pow(2, 32 - prefixBits));
69+
r = r << (prefixBits);
6670
}
67-
} else {
68-
69-
}
71+
randomAddress = this.intToIPAddressString(r);
72+
} while ((randomAddress in map)) ;
7073

71-
randomAddress = r.toString();
72-
console.log(map);
73-
if (!(randomAddress in map)) map[randomAddress] = "yes";
74-
else console.log("Address already existed!");
75-
74+
this.setState({generatedIPAddress: randomAddress});
75+
map[randomAddress] = true;
7676
localStorage.setItem("addresses", JSON.stringify(map));
77-
console.log(r.toString(2));
78-
// localStorage.clear();
77+
console.log(map)
78+
}
79+
80+
intToIPAddressString(ip) {
81+
let bytes = [4];
82+
bytes[3] = ip & 0xFF;
83+
bytes[2] = (ip >> 8) & 0xFF;
84+
bytes[1] = (ip >> 16) & 0xFF;
85+
bytes[0] = (ip >> 24) & 0xFF;
86+
return `${bytes[0]}.${bytes[1]}.${bytes[2]}.${bytes[3]}`;
87+
}
88+
89+
sleep(ms) {
90+
return new Promise(resolve => setTimeout(resolve, ms));
7991
}
8092

8193
handleNumberOfHostsChange(event) {
@@ -87,18 +99,18 @@ export default class UserInput extends React.Component {
8799
<div>
88100
<form onSubmit={e => this.handleSubmit(e)}>
89101
<div className="form-group-row">
90-
<legend className="col-form-label">classNameful or classNameless?</legend>
102+
<legend className="col-form-label">classful or classless?</legend>
91103
<div className="radio">
92104
<label>
93105
<input type="radio"
94-
checked={this.state.classNameful === true}
106+
checked={this.state.classful === true}
95107
onChange={e => this.handleclassNameRadioButtonChange(true)} />
96108
classful
97109
</label>
98110
</div>
99111
<div className="form-group">
100112
<label htmlFor="classNameSelect">Pick addressing className</label>
101-
<select className="form-control" id="classNameSelect" value={this.state.classNameValue} onChange={e => this.handleclassNameChange(e)} disabled={!this.state.classNameful}>
113+
<select className="form-control" id="classNameSelect" value={this.state.classNameValue} onChange={e => this.handleclassNameChange(e)} disabled={!this.state.classful}>
102114
<option>A</option>
103115
<option>B</option>
104116
<option>C</option>
@@ -109,14 +121,14 @@ export default class UserInput extends React.Component {
109121
<div className="radio">
110122
<label>
111123
<input type="radio"
112-
checked={this.state.classNameful === false}
124+
checked={this.state.classful === false}
113125
onChange={e => this.handleclassNameRadioButtonChange(false)} />
114126
classless
115127
</label>
116128
</div>
117129
<div className="input-group">
118130
<span className="input-group-addon" id="basic-addon1">Number of hosts needed:</span>
119-
<input type="number" className="form-control" placeholder="hosts.." aria-describedby="basic-addon1" disabled={this.state.classNameful} onChange={e=>{this.handleNumberOfHostsChange(e)}}/>
131+
<input type="number" className="form-control" placeholder="hosts.." aria-describedby="basic-addon1" disabled={this.state.classful} onChange={e=>{this.handleNumberOfHostsChange(e)}}/>
120132
</div>
121133
<br/>
122134
<button type="submit" value="submit" className="btn btn-primary">Assign!</button>
@@ -126,6 +138,17 @@ export default class UserInput extends React.Component {
126138
)
127139
}
128140

141+
getIPAddressesDisplay() {
142+
return (
143+
<div>
144+
<div className="form-group">
145+
<label for="exampleFormControlInput1">Your IP address:</label>
146+
<input className="form-control" type="text" name="country" value={this.state.generatedIPAddress} readOnly />
147+
</div>
148+
</div>
149+
)
150+
}
151+
129152
getIPv6TabContent() {
130153
return (
131154
<div>
@@ -152,6 +175,8 @@ export default class UserInput extends React.Component {
152175
{this.getIPv6TabContent()}
153176
</div>
154177
</div>
178+
<br></br>
179+
{this.getIPAddressesDisplay()}
155180
</div>
156181
)
157182
}

0 commit comments

Comments
 (0)