-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoutingMapTree.java
407 lines (405 loc) · 9.33 KB
/
RoutingMapTree.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
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
import java.io.*;
import java.util.*;
public class RoutingMapTree{
Exchange root;
public RoutingMapTree() {
root =new Exchange(0);
root.parent=null;
}
public RoutingMapTree(Exchange Root)
{
root=Root;
}
public void switchOn(MobilePhone a,Exchange b)throws Exception
{
boolean k=a.status();
if(k==false)
{
if(!this.root.registeredMobiles.findMobileAlter(a.id))
{
a.setBase(b);
a.switchOn();
while(b.isRoot() !=true){
b.registeredMobiles.insertmobile(a);
b=b.parent();
}
b.registeredMobiles.insertmobile(a);
}
else
{
a.switchOn();
Exchange g=a.location();
while(g.isRoot() !=true){
g.registeredMobiles.deletemobile(a);
g=g.parent();
}
g.registeredMobiles.deletemobile(a);
a.setBase(b);
while(b.isRoot() !=true){
b.registeredMobiles.insertmobile(a);
b=b.parent();
}
b.registeredMobiles.insertmobile(a);
}
}
else
{
throw new Exception("Mobile Phone "+a.id+"already switched on");
}
}
public void switchOff(MobilePhone a)throws Exception
{
boolean k=a.status();
if(k==true)
{
//Exchange g=a.location();
a.switchOff();
/*while(g.isRoot() !=true){
g.registeredMobiles.deletemobile(a);
g=g.parent();
}
g.registeredMobiles.deletemobile(a);*/
}
else
{
throw new Exception("Mobile Phone "+a.id+"already switched off");
}
}
public Exchange getNode(RoutingMapTree d,int ide)throws Exception
{
try{
Exchange p=new Exchange(-1);
p=d.root;int i=0;
int h=p.numChildren();
for(i=0;i<h;i++)
{
p=getNode(p.subtree(i),ide);
if(p == d.root)
continue;
else
return p;
}
if(p.id==ide)
return p;
else
return d.root.parent();
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
}
public Exchange findPhone(MobilePhone m)throws Exception
{
boolean k=m.status();
if(k==true)
{
return m.location();
}
else
throw new Exception("Error - Mobile phone with identifier "+m.id+" is currently switched off");
}
public Exchange lowestRouter(Exchange a, Exchange b)
{
while(a!=b){
a=a.parent();
b=b.parent();
}
return a;
}
public ExchangeList routeCall(MobilePhone a, MobilePhone b)throws Exception
{
try{
Exchange a1=findPhone(a);
Exchange a2=findPhone(b);
Exchange cmn=lowestRouter(a1,a2);
ExchangeList rc1=new ExchangeList();
ExchangeList rc2=new ExchangeList();
while(a1!=cmn)
{
rc1.insertExchange(a1);
a1=a1.parent();
}
rc1.insertExchange(a1);
while(a2!=cmn)
{
rc2.insertExchangeFront(a2);
a2=a2.parent();
}
rc1.eset.ptr=rc1.eset.ptr.next;
rc1.eset.ptr.next=rc2.eset.head.next;
return rc1;
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
}
public void movePhone(MobilePhone a, Exchange b)throws Exception
{
int h=b.numChildren();
boolean k=a.status();
if(h==0)
{
if(k==true)
{
this.switchOff(a);
this.switchOn(a,b);
}
else
throw new Exception("Mobile Phone "+a.id+" is switched off");
}
else
throw new Exception(b.id+" is not a base station");
}
public String performAction(String actionMessage){
String s1[]=actionMessage.split("\\s+");
String result="";
if(s1[0].equals("addExchange"))
{
int a1=Integer.parseInt(s1[1]);
int a2=Integer.parseInt(s1[2]);
try{
Exchange d=getNode(this,a1);
if(d==null)
System.out.println("Error- No exchange with identifier "+a1);
else
{
Exchange newn=new Exchange(a2);
d.children.insertExchange(newn);
newn.setParent(d);
return result;
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
else if(s1[0].equals("switchOnMobile"))
{
int a1=Integer.parseInt(s1[1]);
int a2=Integer.parseInt(s1[2]);
try{
Exchange d=getNode(this,a2);
if(d==null)
System.out.println("Error- No exchange with identifier "+a2);
else
{
if(this.root.registeredMobiles.findMobileAlter(a1))
this.switchOn(this.root.registeredMobiles.findMobile(a1),d);
else
{
MobilePhone newn=new MobilePhone(a1);
this.switchOn(newn,d);
return result;
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
else if(s1[0].equals("switchOffMobile"))
{
int a1=Integer.parseInt(s1[1]);
try{
this.switchOff(this.root.registeredMobiles.findMobile(a1));
return result;
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
else if(s1[0].equals("queryNthChild"))
{
int a1=Integer.parseInt(s1[1]);
int a2=Integer.parseInt(s1[2]);
try{
Exchange d=getNode(this,a1);
if(d==null)
System.out.println("Error- No exchange with identifier "+a1);
else{
result="queryNthChild "+a1+" "+a2+": "+d.child(a2).id;
System.out.println(result);
return result;
}
}
catch(Exception e)
{
System.out.println("Error - No "+a2+" child of Exchange "+a1);
}
}
else if(s1[0].equals("queryMobilePhoneSet"))
{
int a1=Integer.parseInt(s1[1]);
try{
Exchange d=getNode(this,a1);
if(d==null)
System.out.println("Error - No exchange with identifier "+a1);
else
{
MobilePhoneSet a=d.registeredMobiles;
result="queryMobilePhoneSet "+a1+": ";
a.setofmobile.aset.ptr=a.setofmobile.aset.head;
a.setofmobile.aset.ptr=a.setofmobile.aset.ptr.next;
while(a.setofmobile.aset.ptr.next!=null)
{
MobilePhone r=(MobilePhone)a.setofmobile.aset.ptr.data;
if(r.status() == false)
{
a.setofmobile.aset.ptr=a.setofmobile.aset.ptr.next;
continue;
}
result=result+r.id+", ";
a.setofmobile.aset.ptr=a.setofmobile.aset.ptr.next;
}
MobilePhone r=(MobilePhone)a.setofmobile.aset.ptr.data;
if(r.status() !=false)
{
result=result+r.id;
}
System.out.println(result);
return result;
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
else if(s1[0].equals("findPhone"))
{
int a1=Integer.parseInt(s1[1]);
result="queryFindPhone "+a1+": ";
try
{
MobilePhone p1=this.root.registeredMobiles.findMobile(a1);
Exchange b=findPhone(p1);
result=result+b.id;
System.out.println(result);
return result;
}
catch(Exception e)
{
result=result+e.getMessage();
System.out.println(result);
return result;
}
}
else if(s1[0].equals("lowestRouter"))
{
int a1=Integer.parseInt(s1[1]);
int a2=Integer.parseInt(s1[2]);
try
{
Exchange e1=getNode(this,a1);
Exchange e2=getNode(this,a2);
if(e1==null)
System.out.println("Error - No exchange with identifier "+a1);
else if(e2==null)
System.out.println("Error - No exchange with identifier "+a2);
else
{
Exchange cmn=lowestRouter(e1,e2);
result="queryLowestRouter "+a1+" "+a2+": "+cmn.id;
System.out.println(result);
return result;
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
else if(s1[0].equals("findCallPath"))
{
int a1=Integer.parseInt(s1[1]);
int a2=Integer.parseInt(s1[2]);
result="queryFindCallPath "+a1+" "+a2+": ";
try
{
ExchangeList listcall=routeCall(this.root.registeredMobiles.findMobile(a1),this.root.registeredMobiles.findMobile(a2));
listcall.eset.ptr=listcall.eset.head;
listcall.eset.ptr=listcall.eset.ptr.next;
while(listcall.eset.ptr.next!=null)
{
Exchange r=(Exchange)listcall.eset.ptr.data;
result=result+r.id+", ";
listcall.eset.ptr=listcall.eset.ptr.next;
}
Exchange r=(Exchange)listcall.eset.ptr.data;
result=result+r.id;
System.out.println(result);
return result;
}
catch(Exception e)
{
result=result+e.getMessage();
System.out.println(result);
return result;
}
}
else if(s1[0].equals("movePhone"))
{
int a1=Integer.parseInt(s1[1]);
int a2=Integer.parseInt(s1[2]);
try{
MobilePhone p1=this.root.registeredMobiles.findMobile(a1);
Exchange d=getNode(this,a2);
if(d==null)
System.out.println("Error - No exchange with identifier "+a2);
else
{
this.movePhone(p1,d);
}
return result;
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
return result;
}
/*public static void main(String args[]){
RoutingMapTree s=new RoutingMapTree();
Exchange p=new Exchange(45);
p.setParent(s.root);
MobilePhone k1=new MobilePhone(1234);
MobilePhone k2=new MobilePhone(2345);
MobilePhone k3=new MobilePhone(3456);
MobilePhone k4=new MobilePhone(4567);
MobilePhone k5=new MobilePhone(5678);
p.registeredMobiles.insertmobile(k1);
p.registeredMobiles.insertmobile(k2);
Exchange q=new Exchange(46);
q.setParent(s.root);
Exchange r=new Exchange(47);
r.setParent(s.root);
r.registeredMobiles.insertmobile(k5);
Exchange k=new Exchange(23);
Exchange pq=new Exchange(24);
pq.registeredMobiles.insertmobile(k3);
pq.registeredMobiles.insertmobile(k4);
Exchange pw=new Exchange(25);
s.root.children.insertExchange(p);
s.root.children.insertExchange(q);
s.root.children.insertExchange(r);
p.children.insertExchange(k);
k.setParent(p);
p.children.insertExchange(pq);
pq.setParent(p);
q.children.insertExchange(pw);
pw.setParent(q);
s.performAction("queryMobilePhoneSet 45");
System.out.println("");
s.performAction("queryMobilePhoneSet 47");
System.out.println("");
s.performAction("queryMobilePhoneSet 24");
System.out.println("");
s.performAction("queryNthChild 45 0");
}*/
}