-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMain.java
More file actions
248 lines (227 loc) · 7.8 KB
/
Main.java
File metadata and controls
248 lines (227 loc) · 7.8 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
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main implements Runnable {
//Instance variables
int iGraph[][],iPID[];//edgeGraph and uid of nodes
int processFinished;
public Node[] nodeI;//oproc
boolean replyAll, waitState;//boolean variable to check whether process replied or in wait condition
int noOfNodes=0; //number of nodes for simplicity or number of processes in distributed scenario
int noOfProcess=0;
static String[] sProcessRoundStatus;
;//for simplicity
int uidOfNodes[] = null;
double edgeGraph[][]=null;
Thread check;
ThreadGroup threads;
Main obj;
String[] Rounds={"RoundOver","RoundNotOver","ProcessOver"};//put it as enum
String state;
//static Node[] nodeArray;
//Fragment[] fragments;
//MSTGraph mstGraph;
//*********MAIN**********METHOD//
public static void main(String[] args) throws IOException {
Main ghs= new Main();
ghs.readNoadDetails();
}
//Constructor of Main class assigning default values
public Main(){
state=Rounds[0];//sStatus==sStatusValue initially rounds are set to be over that is starting point
processFinished=0;//all the processes are done
replyAll=false;//received reply from all set to false
waitState=false;//none of my process is in wait state so chill
}
public void setMainController(Main obj){
this.obj=obj; //assign the value to main object created
}
//taking input value and extracting important stuff like number of processes neighbors etc.
public void readNoadDetails() throws IOException
{
System.out.println("****** Input Provided ****** " );
Scanner sc = new Scanner(new File("a.txt"));//reading file from input
noOfNodes = sc.nextInt();//total number of processes/nodes assigned to the variable
noOfProcess=noOfNodes;
uidOfNodes= new int[noOfNodes];//is array allocation for 8 different process in our input case
sProcessRoundStatus = new String[noOfNodes];
for(int i=0;i<noOfNodes;i++)
{
uidOfNodes[i]= sc.nextInt();
System.out.println("UID of Processes ----> "+i+" is:" + uidOfNodes[i]);
}
System.out.println("****** Weights Provided ****** " );
edgeGraph = new double[noOfNodes][noOfNodes];
for (int i = 0; i < noOfNodes; i++) {
for (int j = 0; j < noOfNodes; j++) {
edgeGraph[i][j] = sc.nextDouble();
System.out.println("Edge weights between Processes----> " + i+" and "+j+" is "+edgeGraph[i][j]);
}
}
sc.close();
nodeInitialization();//after input is set lets assign the values to all the nodes or in distributed sense processes :D
}
//initializing nodes and its connection to other nodes in short idea about neighbors :P
public void nodeInitialization(){
Node[] n;
nodeI = new Node [noOfNodes];
int process=0, NumberOfNbrs=0, iRow, prs=0;
int neighbours[][];
for(process=0;process<noOfNodes;process++){
NumberOfNbrs=0;
//counting the number of neighbors as negative edge implies no connection thus dropping anything below 0
for(prs=0;prs<noOfNodes;prs++)
{
if(edgeGraph[process][prs]>=0){
NumberOfNbrs++;
}
}
System.out.println("Number of eligible neighbour of node: "+uidOfNodes[process]+" is " + NumberOfNbrs);
neighbours= new int[NumberOfNbrs][2];//creates an array with row = no of neighbor and two columns
iRow=0;
//array assigned in previous step will be used to fill the details of neighbor id
for(prs=0;prs<noOfNodes;prs++)
{
if(edgeGraph[process][prs]>0){
neighbours[iRow][0]=prs;
neighbours[iRow][1]=uidOfNodes[prs];
iRow++;
//System.out.println(neighbours[iRow][prs]);//array out of bound error need to fix it *****************
}
//System.out.println(neighbours[iRow][prs]);
}
//creating the (nodes or process) and passing it to the constructor defined in Node class
nodeI[process]=new Node(uidOfNodes[process],process,neighbours);
}
// send each nodes information about how to communicate to their neighbors
// send an array of neighbor objects to the nodes
// first count, how many number of neighbors each node has. Then add each neighbor object to the array and send it to the Nodes.
NumberOfNbrs=0;
for(process=0;process<noOfNodes;process++){
NumberOfNbrs=0;
for(prs=0;prs<noOfNodes;prs++)
{
if(edgeGraph[process][prs]>0){
NumberOfNbrs++;
}
}
n=new Node[NumberOfNbrs];
iRow=0;
for(prs=0;prs<noOfNodes;prs++){
if(edgeGraph[process][prs]>0){
n[iRow]=nodeI[prs];
iRow++;
}
}
nodeI[process].setNeighborObjects(n);//sending array of neighbor to the nodes
//sending the reference of the main thread to each process in order to control later
nodeI[process].setMain(obj);
}
}
//overriding run method of the interface runnable
@SuppressWarnings("static-access")
public void run(){
try {
check.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean controlSwitch = true;
int pNo;
while(controlSwitch){
switch(state){
case "RoundOver": //if round over then reset the entire process and continue
resetProcessRoundStatus();
for(pNo=0;pNo<noOfNodes;pNo++){
nodeI[pNo].processThread("T"+uidOfNodes[pNo]);
}
state=Rounds[1];
break;
case "RoundNotOver"://if round not over then prepare for next round
readyForNextRound();
for(pNo=0;pNo<noOfNodes;pNo++){
nodeI[pNo].reset("parent");
nodeI[pNo].reset("convergeCast");
}
break;
case "ProcessOver"://when the process is over
for(pNo=0;pNo<=noOfNodes;pNo++){
nodeI[pNo].printYourTree();
}
controlSwitch=false;
}
}
System.out.println("\n---PROCESS TERMINATED---");
}
private synchronized void readyForNextRound(){
// System.out.println("start function: readyForNextRound()");
boolean bReadyForNextRound=true, bProcessDone=true;
int iCount;
// System.out.println("In readyForNextROund()");
if(!replyAll){
try {
// System.out.println("In Master, entering wait state");
waitState=true;
wait();
waitState=false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// if any of the process has not completed their process, break from the loop
// and set bReadyForNextRound=False
// if any of the process is true, set the bProcessDone=false
for(iCount=0;iCount<noOfNodes;iCount++){
if(sProcessRoundStatus[iCount].compareToIgnoreCase("false")==0){
bReadyForNextRound=false;
bProcessDone=false;
break;
}
else if(sProcessRoundStatus[iCount].compareToIgnoreCase("true")==0){
bProcessDone=false;
}
}
// assign the status of the round completed in sStatus private variable
if(bProcessDone){
state=Rounds[2];
}
else if(bReadyForNextRound){
state=Rounds[0];
resetProcessRoundStatus();
}
else{
state=Rounds[1];
}
// System.out.println("End function: readyForNextRound()");
}
public synchronized void setProcessRoundStatus(int iIndex, String sStatus_temp){
// System.out.println("Start of function: setProcessRoundStatus");
sProcessRoundStatus[iIndex]=sStatus_temp;
processFinished++;
if(processFinished==noOfNodes){
if(waitState)
notify();
}
// System.out.println("End of function: setProcessRoundStatus");
}
// reset the sProcessRoundStatus to False at the end of each round
// make the static variable to zero
private void resetProcessRoundStatus() {
// TODO Auto-generated method stub
int iCount=0;
for(iCount=0;iCount<noOfNodes;iCount++){
sProcessRoundStatus[iCount]="false";
}
processFinished=0;
}
public void masterThread(String sThreadName){
check=new Thread(this, sThreadName);
check.start();
}
}