- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticipante.java
More file actions
Latest commit
206 lines (165 loc) · 7.78 KB
/
Copy pathParticipante.java
File metadata and controls
206 lines (165 loc) · 7.78 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
importjava.io.*;
importjava.net.*;
importjava.util.ArrayList;
publicclassParticipante {
// Variables for IP MultiCast
MensajemensajeActual = null;
InetAddressgroup = InetAddress.getByName("225.4.5.6");
ArrayList<Mensaje> msgsDelivered = newArrayList<Mensaje>();
ArrayList<Mensaje> msgs_hold_back_queue = newArrayList<Mensaje>();
ArrayList<Integer> listaprueba = newArrayList<Integer>();
//
//Request request = new Request(5);
//
IntegerRIndex = 0;
//
//Variables for socket tranference
booleanrequest_needed = false;
InetAddresshost = InetAddress.getLocalHost();
//
privateStringid;
privateStringname;
publicParticipante(Stringid, Stringname) throwsIOException {
this.id = id;
this.name = name;
}
publicStringgetId() {
returnthis.id;
}
publicvoidsendMessageToServer(){
}
publicvoidreceiverFromServers(){
}
publicvoidreceiverFromGroup(){
try{
for(inti=0;i<100000;i++){
// The message is received
MulticastSocketmulticastSock = newMulticastSocket(3456);
multicastSock.joinGroup(group);
byte[] buffer = newbyte[1000];
DatagramPacketpacket = newDatagramPacket(buffer, buffer.length);
multicastSock.receive(packet);
//String msg=new String(buffer);
//this set of code turns a incoming set of bytes to its corresponding object, **Check wether you have to set o to Object instead
//of Mensaje as by now I don't know how the application will be tested
ByteArrayInputStreambis = newByteArrayInputStream(buffer);
ObjectInputin = null;
try {
in = newObjectInputStream(bis);
mensajeActual = (Mensaje) in.readObject();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOExceptionex) {
// ignore close exception
}
}
//
intflag= 0;
// Se ve si se puede entregar
for(Mensajee: msgsDelivered){
if(mensajeActual.getId()==(e.getId())){
flag=1;
System.out.println("Ya se ha recibido este mensaje");
}
}
// Si se puede entregar se califica segun su id y segun RIndex
if(flag==0){
// Check if even if it is not delivered there should be a received form of storage
if(mensajeActual.getId()==RIndex+1){
synchronized(this){
this.deliver(mensajeActual,multicastSock);
}
}elseif(mensajeActual.getId()<=RIndex){
System.out.println("Se ignora el mensaje");
}elseif(mensajeActual.getId()>RIndex+1){
this.request_needed = true;
System.out.println("Se debe ponerlo en la hold back queue");
msgs_hold_back_queue.add(mensajeActual);
IntegerindexEntregar = mensajeActual.getId();
ThreadthreadNuevo1 = newThread(newRunnable() {
@Override
publicvoidrun() {
try {
standForDelivery(indexEntregar,mensajeActual,multicastSock);
} catch (IOExceptione) {
e.printStackTrace();
}
}
});
IntegerindexRequest=RIndex;
ThreadthreadNuevo2 = newThread(newRunnable() {
@Override
publicvoidrun() {
sendRequest(mensajeActual,indexRequest);
}
});
threadNuevo1.start();
threadNuevo2.start();
//ask if when meesages get requested, the ones that got them should send directly to the ones in need or to everyone. Solved: it should request just to the server not to others participants.
}
// Aqui se deberia de manejar el caso de que el que esta mandando sea el mismo proceso del que acaba de recibir
// pero dado a que el metodo send solo manda un DatagramPacket se me hace imposible poder mandar algun tipo de id
// del proceso que esta enviando y asi manejar este caso --------->Se logro manejar este problema
}
multicastSock.close();
}
}catch(Exceptione ){
e.printStackTrace();
}
}
publicsynchronizedvoidstandForDelivery(IntegerindexEntregar, MensajemensajeActual,MulticastSocketmulticastSock) throwsIOException {
while (indexEntregar != this.RIndex + 1) {
try {
System.out.println("El Rindex es: " + this.RIndex);
wait();
} catch (InterruptedExceptione) {
e.printStackTrace();
}
}
System.out.println("-------------------------");
System.out.println("Se entregara el mesnaje con id: " + mensajeActual.getId());
this.request_needed = false;
this.RIndex++;
System.out.println("RIndex aumento a: " + RIndex);
notify();
System.out.println("-------------------------");
}
publicvoidsendRequest(MensajemensajeActual, IntegerindexRequest){
//System.out.println("holaaaaa");
ArrayList<Integer> arrayRequests = newArrayList<Integer>();
for (inti = indexRequest + 1; i < mensajeActual.getId(); i++) {
arrayRequests.add(i);
//System.out.println("hhh: " + i);
}
try {
Socketsocket = newSocket(host.getHostName(), 8001);
System.out.println("Poooooort used: " + socket.getPort());
ObjectOutputStreamobjectOutputStream = newObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(arrayRequests);
socket.close();
} catch (IOExceptione) {
e.printStackTrace();
}
//System.out.println("holaaaaa 22222222222222");
}
publicvoiddeliver(MensajemensajeActual,MulticastSocketmulticastSock) throwsIOException {
System.out.println("-------------------------");
this.msgsDelivered.add(mensajeActual); // se entrega
System.out.println("Se ha recibido y almacenado un mensaje");
System.out.println("Mensaje :" + mensajeActual.getMensaje());
System.out.println("Se reenviara a todos los agentes del grupo");
this.sendToServer(mensajeActual, multicastSock);
RIndex++;
System.out.println("RIndex aumento a: " + RIndex);
notify();
System.out.println("-------------------------");
}
publicvoidsendToServer(MensajemensajeActual, MulticastSocketmulticastSock) throwsIOException { //this whole method needs to change, the PARTICICPANT always sends just a String, the server is the one that turns it into a MESSAGE given the actual RIndex of the given GROUP
byte[] yourBytes = mensajeActual.getInSendingForm(); //turns object to bytes to send
DatagramPacketpacket_out = newDatagramPacket(yourBytes, yourBytes.length, group, 3456);
multicastSock.send(packet_out);
}
}