-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApplication.java
197 lines (173 loc) · 5.51 KB
/
Application.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
import java.util.Random;
import java.io.*;
import java.net.*;
import com.sun.nio.sctp.*;
import java.nio.*;
import java.util.*;
import java.util.concurrent.Semaphore;
public enum Application{
Poisson {
@Override
public double getRandom(Random r, double lambda) {
double L = Math.exp(-lambda);
int k = 0;
double p = 1.0;
do {
k++;
p = p * r.nextDouble();
} while (p > L);
return k - 1;
}
},
Exponential {
@Override
public double getRandom(Random r, double p) {
return -(Math.log(r.nextDouble()) / p);
}
},
Geometric {
@Override
public double getRandom(Random r, double geoSeed) {
double p = 1.0 / ((double) geoSeed);
return (int)(Math.ceil(Math.log(r.nextDouble())/Math.log(1.0-p)));
}
},
Pareto {
@Override
public double getRandom(Random r, double alpha, double xM) {
double v = r.nextDouble();
while (v == 0){
v = r.nextDouble();
}
return xM / Math.pow(v, 1.0/alpha);
}
},
ParetoBounded {
@Override
public double getRandom(Random r, double alpha, double L, double H) {
double u = r.nextDouble();
while (u == 0){
u = r.nextDouble();
}
double x = -(u*Math.pow(H,alpha)-u*Math.pow(L,alpha)-Math.pow(H,alpha)) /
(Math.pow(H*L,alpha));
return Math.pow(x, -1.0/alpha);
}
},
Uniform {
@Override
public double getRandom(Random r, double p) {
return r.nextDouble() * p;
}
},
Constant {
@Override
public double getRandom(Random r, double N) {
return N;
}
};
public double getRandom(double p) throws IllegalArgumentException {
return getRandom(defaultR, p);
}
public double getRandom(double a, double b) throws IllegalArgumentException {
return getRandom(defaultR, a, b);
}
public double getRandom(double a, double b, double c) throws IllegalArgumentException {
return getRandom(defaultR, a, b, c);
}
public double getRandom(Random r, double p) throws IllegalArgumentException {
throw new IllegalArgumentException();
}
public double getRandom(Random r, double a, double b) throws IllegalArgumentException{
throw new IllegalArgumentException();
}
public double getRandom(Random r, double a, double b, double c) throws IllegalArgumentException{
throw new IllegalArgumentException();
}
public static final Random defaultR = new Random();
//Modification for Programming Assignment 2
public static final void launch(String args){
//Testing
int nocsreq=0,csduration=0,delaybtwcs=0;
//Reading information from the configuration file
try{
String s;
//Object to read the configuration file
FileReader readConfig=new FileReader("config.txt");
BufferedReader brReader=new BufferedReader(readConfig);
while((s=brReader.readLine())!=null){
String[] str=s.split("\\s+");
//finding out who is the starting node
if(str[0].equals("start")){
//startNode=Integer.parseInt(str[1]);
}
//get the total number of nodes count in the graph
else if(str[0].equals("total")){
//totalNodes=Integer.parseInt(str[1]);
}
else if(str[0].equals("nocsreq")){
nocsreq=Integer.parseInt(str[1]);
}
else if(str[0].equals("csduration")){
csduration=Integer.parseInt(str[1]);
}
else if(str[0].equals("delaybtwcs")){
delaybtwcs=Integer.parseInt(str[1]);
}
//Collecting the neighbouring nodes list, port number and server name and map it to a node ID
else{
}
}
//close the file here after getting all the necessary information
brReader.close();
}
catch(IOException e){
System.err.println(e.getMessage());
}
try{
Thread.sleep(10000);
}
catch(Exception ex){
ex.printStackTrace();
}
Application testStat = Exponential;
Application testStatdelaybtwcs = Exponential;
int myNodeID=Integer.parseInt(args);
double lambda = 1.0 / csduration;
double lambdadelaybtwcs = 1.0 / delaybtwcs;
double temp=0,temp1=0;
// System.out.println("Testing stat: " + testStat+ " with lambda: " + lambda);
for (int i = 0; i < nocsreq; i++){
try{
Launcher.enter_cs();
}
catch(Exception ex){
ex.printStackTrace();
}
temp=testStat.getRandom(lambda);
//System.out.println(temp);
try{
System.out.println(myNodeID+": csduration = "+(int)Math.round(temp));
Thread.sleep((int)Math.round(temp));
}
catch(InterruptedException e){
e.printStackTrace();
}
try{
Launcher.leave_cs();
}
catch(Exception ex){
ex.printStackTrace();
}
temp1=testStatdelaybtwcs.getRandom(lambdadelaybtwcs);
try{
System.out.println(myNodeID+": durationbtwcs = "+(int)Math.round(temp1));
Thread.sleep((int)Math.round(temp1));
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
//Modification for Programming Assignment 2
}