-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathremove_connections.js
188 lines (158 loc) · 5.71 KB
/
remove_connections.js
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
import {list_devices, End_device} from './Device.js';
import {list_hubs, Hub} from './Hub.js';
import {list_switches, Switch} from './Switch.js';
import {list_bridges, Bridge} from './Bridge.js';
// remove connection from one end device to another end device
export function remove_connection_end_to_end()
{
if(Math.max(arguments[0], arguments[1]) >= list_devices.length)
{
return "invalid";
}
var d1 = list_devices[arguments[0]];
var d2 = list_devices[arguments[1]];
if(d1.port[0] != d2.get_mac_address() || d2.port[0] != d1.get_mac_address())
{
return "no connection";
}
d1.port[0] = "-1";
d2.port[0] = "-1";
return "success";
}
// remove connection between an end device and a hub
export function remove_connection_hub_to_end()
{
if(arguments[0] >= list_hubs.length || arguments[1] >= list_devices.length)
{
return "invalid";
}
var h = list_hubs[arguments[0]];
var d = list_devices[arguments[1]];
if(d.port[0] != h.serial)
{
return "no connection";
}
var add = d.get_mac_address();
for(var i = 0; i < h.ports.length; i++)
{
if(h.ports[i] == add)
{
h.ports[i] = "-1";
break;
}
}
d.port[0] = "-1";
return "success";
}
// Remove connection between a bridge and a device
export function remove_connection_bge_to_device()
{
if(arguments[0] >= list_bridges.length || arguments[1] >= list_devices.length)
{
return "invalid";
}
var b = list_bridges[arguments[0]];
var d = list_devices[arguments[1]];
if(d.ports[0] != b.serial)
{
return "no connection";
}
var add = d.get_mac_address();
//search in left
for(var i = 0; i < b.left_segment.length; i++)
{
if(b.left_segment[i] == add)
{
b.left_segment[i] = "-1";
d.ports[0] = "-1";
return "success";
}
}
// search in right
for(var i = 0; i < b.right_segment.length; i++)
{
if(b.right_segment[i] == add)
{
b.right_segment[i] = "-1";
d.ports[0] = "-1";
return "success";
}
}
}
// remove connection between hub and switch
export function remove_connection_hub_to_swt()
{
if(arguments[0] >= list_hubs.length || arguments[1] >= list_switches.length)
{
return "invalid";
}
var h = list_hubs[arguments[0]];
var s = list_switches[arguments[1]];
var check1 = 0,check2 = 0;
var m;
for(var i = 0; i < h.ports.length; i++)
{
if(h.ports[i] == s.serial)
{
check1=1;
m = i;
break;
}
}
for(var i = 0; i < s.ports.length; i++)
{
if(s.ports[i] == h.serial)
{
check2=1;
break;
}
}
if(!check1 || !check2)
{
return "no connection";
}
if(s.ports[0] == h.serial)
{
for(var i = 0; i < s.left_table.length; i++)
{
s.left_table[i] = "-1";
}
s.ports[0] = "-1";
}
else
{
for(var i = 0; i < s.right_table.length; i++)
{
s.right_table[i] = "-1";
}
s.ports[1] = "-1";
}
h.ports[m] = "-1";
return "success";
}
// remove connection between a device and a switch
export function remove_connection_switch_to_end()
{
if(arguments[0] >= list_switches.length || arguments[1] >= list_devices.length)
{
return "invalid";
}
var s = list_switches[arguments[0]];
var d = list_devices[arguments[1]];
if(d.port[0] != s.serial)
{
return "no connection";
}
var add = d.get_mac_address();
for(var i = 0; i < s.ports.length; i++)
{
if(s.ports[i] == add)
{
s.ports[i] = "-1";
s.table[i] = "-1";
break;
}
}
d.port[0] = "-1";
return "success";
}