-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.java
More file actions
69 lines (45 loc) · 1.97 KB
/
Router.java
File metadata and controls
69 lines (45 loc) · 1.97 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
package network;
import static java.lang.Thread.sleep;
public class Router {
public boolean[] connectionArray;
public int nRouterConnections;
public Semaphore connectsem;
public int nOccupied;
int wait=1;
Router (int nRouterConnections ){
this.nRouterConnections=nRouterConnections;
connectsem=new Semaphore (this.nRouterConnections);
connectionArray=new boolean[this.nRouterConnections];
for (int i=0 ; i<connectionArray.length ; i++){
connectionArray[i] = false;
}
}
public synchronized String occupy(Device d)throws InterruptedException{
System.out.println("----->"+d.getname());
for (int i = 0; i < nRouterConnections; i++) {
if (connectionArray[i] == false ) {
connectsem.P();
//System.out.println(d.getname());
// nOccupied++;
d.assign=i+1;
//System.out.println("assignBefore-->"+d.assign);
//System.out.println("connection " + d.assign + ": " + d.getname() + " occupied");
//d.connect() ;
connectionArray[i] = true;
return d.connect();
}
}//d.assign=wait;
return ( d.getname() + " arrived and is waiting");
}
public String serve(Device d) throws InterruptedException {
return d.performActivity();
}
public synchronized String release(Device d) throws InterruptedException {
//wait =d.getassign();//1
//System.out.println("assign--->"+wait);
connectsem.V();
//connectionArray[d.assign-1] = false;
// System.out.println("assignAfter-->"+d.assign);
return d.logout();
}
}