-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeServant.java
169 lines (154 loc) · 4.01 KB
/
NodeServant.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
import Eleccion.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.util.*;
import java.util.Properties;
import java.math.*;
/**
*
* @author Miguel Alejandro
*/
public class NodeServant extends NodePOA {
private Vector clients = new Vector();
private int uid, vencedor,pid;
private int uids[];
private String mode, status;
private SimpleQueue send = new SimpleQueue();
private SimpleQueue receive = new SimpleQueue();
public NodeServant(int uid){
this.uid = uid;
this.mode = "active";
this.status = "unknown";
this.uids = new int[3];
this.uids[0] = uid;
this.uids[1] = -1;
this.uids[2] = -1;
this.send.put(uid);
}
public int getLeader(){
return this.vencedor;
}
public int getSencondUID() {
if ( mode.equals("active") && receive.isEmpty() == false && uids[1] == -1 ) {
uids[1] = (Integer)receive.get();
send.put(uids[1]);
//send(uids[1]);
System.out.println("Se obtuvo second UID");
if ( uids[1] == uids[0] ){
status = "chosen";
System.out.println("Status: "+status);
}
return 1;
} else {
return 0;
}
}
public void getThirdUID() {
if ( mode.equals("active") && receive.isEmpty() == false && uids[1] != -1 && uids[2] == -1 ) {
uids[2] = (Integer)receive.get();
System.out.println("Se obtuvo third UID");
}
}
public int advancePhase() {
if( mode.equals("active") && uids[2] != -1 && uids[1] > Math.max(uids[0], uids[2]) ) {
uids[0] = uids[1];
uids[1] = -1;
uids[2] = -1;
System.out.println("avanza fase");
send.put(uids[0]);
return 1;
} else {
return 0;
}
}
public void becomeRelay() {
if( mode.equals("active") && uids[2] != -1 && uids[1] <= Math.max(uids[0], uids[2]) ) {
mode = "relay";
System.out.println("mode: relay, ahora. sorry mate! :(");
}
}
public void relay() {
if( mode.equals("relay") && receive.isEmpty() == false ) {
int x = (Integer)receive.get();
send.put(x);
System.out.println("Mandando mensaje relay");
send(x);
}
}
public void send(int v){
if (send.isEmpty() == false) {
if(v == send.peek() ){
int x = (Integer)send.get();
sendmessage("election:"+x);
}
}
}
public int leader(){
if(status.equals("chosen")) {
status = "reported";
System.out.println("Status: "+status);
return 1;
} else {
return 0;
}
}
public void election() {
//System.out.println("Proceso de eleccion iniciado");
//System.out.println("Mandando mensaje de eleccion "+uids[0]);
while (!mode.equals("relay")) {
send(uids[0]);
if (getSencondUID() == 1) {
send(uids[1]);
}
getThirdUID();
if( advancePhase() == 1){
send(uids[0]);
}
becomeRelay();
}
while(!status.equals("reported") ){
relay();
if ( leader() == 1){
System.out.println("Mandando mensaje de lider " + uids[1]);
sendmessage("leader:"+uids[0]);
vencedor = uids[0];
System.out.println("Gane, soy el proceso " + uid + " con uid virtual " + uids[1]);
}
}
}
public void register(Node nodo) {
clients.add(nodo);
}
public void removeNode(Node nodo) {
clients.remove(nodo);
}
public void sendmessage(String msg) {
Iterator it = clients.iterator();
while (it.hasNext()) {
Node ms = (Node)it.next();
ms.message(msg);
}
}
public void message(String msg) {
String[] temp;
String delimiter = ":";
temp = msg.split(delimiter);
if( temp[0].equals("election") ){
pid = Integer.parseInt(temp[1]);
System.out.println("Se recibe mensaje en "+uid+" pid= "+pid);
receive.put(pid);
election();
} else if( temp[0].equals("leader") ){
pid = Integer.parseInt(temp[1]);
if ( uids[1] != pid ){
status = "reported";
System.out.println("Status: "+status);
vencedor = pid;
sendmessage(msg);
}
}
}
}