-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpGrad.java
More file actions
315 lines (186 loc) · 6.43 KB
/
SpGrad.java
File metadata and controls
315 lines (186 loc) · 6.43 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package example.progetto;
import peersim.util.*;
import peersim.core.*;
import peersim.config.*;
import peersim.cdsim.*;
import java.util.Random;
import peersim.transport.Transport;
import peersim.cdsim.CDProtocol;
import peersim.edsim.EDProtocol;
import peersim.vector.SingleValueHolder;
import java.util.*;
import java.lang.Math;
public class SpGrad implements CDProtocol, EDProtocol {
//Members
String n;
/** config parameter name for the cache size */
public static final String PAR_CACHE = "cache";
private int maxdegree = 20;
//Costruttore
public SpGrad(String n){
maxdegree = Configuration.getInt(n+"."+PAR_CACHE);
}
//=============================== METODI =============================================================
//Trasforma un linkable in un Vector
public void linkToVector(RemoveProtocol a, Vector v) {
for (int i =0; i<a.degree();i++)
{
v.add(a.getNeighbor(i));
}
}
//Trasforma un vector in un Linkable
public void vectorToLink(RemoveProtocol a, Vector v){
for (int i =0; i<v.size();i++)
{
Node b = a.getNeighbor(i);
b =(Node)v.get(i);
}
}
//Trova la massima utilità in un vettore di nodi
public Node maxNodeVector (RemoveProtocol spnew){
int max = 0;
for (int i= 0; i< spnew.degree(); i++)
{
Node x = spnew.getNeighbor(i);
Node maxnode = spnew.getNeighbor(max);
DinElec mx = (DinElec)maxnode.getProtocol(2);
DinElec bx = (DinElec)x.getProtocol(2);
if (mx.showutility() < bx.showutility()) //Protocol 2 = DinElec
{
max = i;
}
}
return spnew.getNeighbor(max);
}
//Trova la minima utilità in un vettore di nodi
public Node minNodeVector (RemoveProtocol spnew){
int max = 0;
for (int i= 0; i< spnew.degree(); i++)
{
Node x = spnew.getNeighbor(i);
Node maxnode = (Node)spnew.getNeighbor(max);
DinElec mx = (DinElec)maxnode.getProtocol(2);
DinElec bx = (DinElec)x.getProtocol(2);
if (mx.showutility() > bx.showutility()) //Protocol 2 = DinElec
{
max = i;
}
}
return spnew.getNeighbor(max);
}
//Calcola la similitudine tra il peer e un peer di sp
public Node simil(RemoveProtocol spnew, Node p){
//DinElec del nodo P
DinElec px = (DinElec)p.getProtocol(2);
int max = 0;
for (int i= 0; i< spnew.degree(); i++)
{
Node x = spnew.getNeighbor(i);
Node maxnode = (Node)spnew.getNeighbor(max);
DinElec mx = (DinElec)maxnode.getProtocol(2);
DinElec bx = (DinElec)x.getProtocol(2);
if (bx.showutility() > px.showutility() && mx.showutility()<px.showutility() ||
Math.abs(bx.showutility()-px.showutility()) < Math.abs(mx.showutility()-px.showutility()) && bx.showutility() > px.showutility() && mx.showutility() > px.showutility() || bx.showutility() < px.showutility() && mx.showutility() < px.showutility() )
{
max = i;
}
}
return spnew.getNeighbor(max);
}
//====================== PROCESS EVENT =======================================================================================
/* OVERWIEV:
Processa i messaggi di tipo SpMessage.
*Se il messaggio è di tipo 0:
-ceglie due nodi nei protocolli Sp (0) e Rp (1) secondo politiche stabilite
-risponde al mittente con un messaggio di tipo=1 inviando i due nodi joinsp e joinrp
*Se il messaggio é di tipo 1:
-Se il protocollo Sp del nodo non ha raggiunto il limite, aggiunge il nodo joinsp
-Se ha raggiunto il limite sceglie il nodo di sp con l'utilità minore e lo sostituisce solo se
la sua utilità é minore del nodo del vicino
-Sceglie un nodo casuale nel vettore rp del messaggio
-Se il protocollo Rp del nodo non ha raggiunto il limite, lo aggiunge
-Se ha raggiunto il limite sceglie un nodo casuale di rp e lo sostituisce con il nodo del vicino
*/
public void processEvent(Node node, int pid, Object event ) {
SpMessage mex = (SpMessage) event;
//Messaggio di tipo 0
if (mex.type == 0)
{
Transport t = (Transport)node.getProtocol(FastConfig.getTransport(pid));
RemoveProtocol sp = (RemoveProtocol) node.getProtocol(0); //Sp del nodo attuale
RemoveProtocol rp = (RemoveProtocol) node.getProtocol(1); //Rp del nodo attuale
//Il nodo con max Similitudine dal protocollo Sp del vicino da aggiungere
Node joinsp = simil(sp,mex.mitt);
//il nodo casuale da sostituire in Rp
Node joinrp;
Random generator = new Random();
int a = rp.degree();
int r = generator.nextInt(a);
if (rp.degree() ==0) joinrp=null;
else{ joinrp = rp.getNeighbor(r);}
t.send(node,mex.mitt,new SpMessage(joinsp,joinrp,1,node),pid);
return;
}
else
{
RemoveProtocol spdest = (RemoveProtocol)node.getProtocol(0); //Sp del nodo attuale
RemoveProtocol rpdest = (RemoveProtocol)node.getProtocol(1); //Rp del nodo attuale
//I Nodi contenuti nel messaggio
Node join = mex.linkSp;
Node joinrp = mex.linkRp;
//---MODIFICA DI Sp---
if (spdest.degree() < maxdegree)
{
spdest.addNeighbor(join);
}
else
{
//si rimpiazza il nodo con min Utility di Spdest solo se la sua utilità é minore del nuovo arrivato
Node replace = minNodeVector(spdest);
DinElec mx = (DinElec)replace.getProtocol(2);
DinElec bx = (DinElec)join.getProtocol(2);
if (mx.showutility() < bx.showutility()) //protocol 2 = DinElec
{
spdest.remNeighbor(replace);
//Sostituisce spdest con la nuova versione
spdest.addNeighbor(join);
}
}
//---MODIFICA DI Rp---
//il nodo che verrà aggiunto scelto a caso tra i nodi in Rp del vicino
//Se Rp non ha ancora raggiunto il limite lo aggiunge
if (rpdest.degree() < maxdegree)
{
rpdest.addNeighbor(joinrp);
}
else
{
//Rimpiazza un nodo in Rp scelto in modo casuale
Random generator = new Random();
int r = generator.nextInt(rpdest.degree());
Node replace2 = rpdest.getNeighbor(r);
rpdest.remNeighbor(replace2);
rpdest.addNeighbor(joinrp);
}
}
}
//============================================NEXT CYCLE =============================================================
public void nextCycle(Node node,int pid) //Invia un messaggio ad un vicino casuale in sp con la richiesta di farsi inviare Sp e Rp
{
Linkable linkablesp = (Linkable) node.getProtocol(0); // 0 é sp l'insieme dei vicini Sp
//sceglie un vicino casuale in sp
Random generator = new Random();
int a = linkablesp.degree();
int r = generator.nextInt(a);
if (linkablesp.degree()>0)
{
Node dest = linkablesp.getNeighbor(r);
if(!dest.isUp()) return;
Transport t = (Transport)node.getProtocol(FastConfig.getTransport(pid));
t.send(node,dest,new SpMessage(null,null,0,node),pid);
}
}
public SpGrad clone(){
return this;
}
}