-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoFreeNode.h
More file actions
171 lines (151 loc) · 6.65 KB
/
NoFreeNode.h
File metadata and controls
171 lines (151 loc) · 6.65 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
//
// Copyright (c) 2013 Daniel Iñigo, Efren Suarez, Yuriy Batrakov, José Sklatz
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions of
// the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//
// file: NoFreeNode.h
// author: Daniel Iñigo, Efrén Suárez
//
#ifndef __NOFREENODE_H_
#define __NOFREENODE_H_
#include <stdio.h>
#include <string.h>
#include <map>
#include <set>
#include <omnetpp.h>
#include "NoFreeMessage_m.h"
using namespace std;
/**
* Clase muy básica para almacenar la reputación
*/
struct PeerReputation {
int acceptedRequest; // Peticiones aceptadas.
int totalRequest; // Peticiones aceptadas.
/** Constructor por defecto */
PeerReputation() {acceptedRequest=0; totalRequest=0;}
/** Constructor con parámetros */
PeerReputation(int a, int t) {acceptedRequest=a; totalRequest=t;}
};
class NoFreeNode : public cSimpleModule
{
protected:
const int NOBODY; // Cuando no atendemos a un nodo ponemos este valor
// en los nodeRequested y nodeServed.
PeerReputation tempReputation; // Almacena temporalmente la reputación
// de un nodo para decidir si servirle o no.
double requiredShareRate; // Ratio necesario de reputación para compartir (p.ej: 0.8);
int nodeRequested; // Nodo al que se ha pedido el archivo.
int nodeServed; // Nodo al que se va a enviar el archivo.
int nodeServedGate; // Id de la puerta por la que servir el archivo.
set <int> nodeContributed; // Lista de nodos que han aportado su reputación.
double downloadFileTimeout; // Tiempo hasta que se realize peticion
double reputationRequestTimeout; // Tiempo que se esperan msg tipo R.
double fileRequestTimeout; // Tiempo que se espera a que se sirva
// un archivo (después se considera al servidor un freerider)
double freeriderRate; // Probabilidad de enviar el archivo
// <0.8 freerider, >0.8 buen peer.
cMessage *reputationRequestTimer; // Timer para esperar mensajes de reputación.
// Funciona con reputationRequestTimeout.
cMessage *fileRequestTimer; // Timer para esperar a que me sirvan un archivo.
// Funciona con fileRequestTimeout.
cMessage *downloadFileTimer; // Timer para encolar nuevas peticiones de archivo.
map <int, PeerReputation> nodeMap; // Lista de nodos adyacentes y su reputación.
bool isFreerider; // Almacena si es freerider o no.
//Contadores de paquetes
int countF; //numero de archivos recibidos
int countR; //numero de reputaciones recividas
int countRR; //peticion de reputacion
int countFR; //peticion de archivo
//defino los vectores que se utilizaran para loguear los datos
cOutVector vectorF, vectorR, vectorRR, vectorFR;
public:
/**
* Constructor por defecto.
*/
NoFreeNode ();
/**
* Cancela y Elimina los selfmessage que se usaban como timer para que no
* queden por ahí ejecutándose deattached de forma que consuman memoria pero
* no puedan borrarse (imporante para redes grandes).
*/
~NoFreeNode ();
/**
* Lee las variables por parámetro que se declaran en la topología o en la
* definición de la simulación. Instancia los mensajes que serán timers y
* lanza el primer timer para descargar un archivo que desencadenará el
* resto de eventos.
*/
virtual void initialize ();
/**
* Recibe un mensaje, mira del tipo que es (RR, FR, R o timer) y
* delega su procesado a la función correspondiente.
*/
virtual void handleMessage ( cMessage *msg );
/**
* Maneja los 'selfmessage', que son timers. En función de cuál haya
* expirado se invoca a una función u otra.
*/
virtual void handleTimerEvent ( cMessage *msg );
/**
* Recibe una petición para servir un archivo que desencadena el proceso
* de decision. Habrá que mirar si se tiene la reputación del nodo
*
* trabajar con mapas en c++:
* http://www.yolinux.com/TUTORIALS/CppStlMultiMap.html
*/
virtual void handleFileRequest ( FileRequest *msg );
/**
* Recibe el archivo que había pedido así que incrementa 1 el contador de
* "acceptedRequests".
*/
virtual void handleFileResponse ( File *msg );
/**
* Recibe una petición de reputación para un nodo. Mira si la tiene, si no
* lo tiene reenvía el mensaje.
*/
virtual void handleReputationRequest ( ReputationRequest *msg );
/**
* Recibe la reputación que había pedido y decide si servir o no el archivo
* en función de lo que haya recibido y de un parámetro de "kindness"
* decide enviar el archivo o no.
*/
virtual void handleReputationResponse ( Reputation *msg );
/**
* Transcurrido un tiempo aleatorio pide un archivo a otro nodo.
* Aumenta en uno el contador de peticiones totales a ese nodo.
* Se elije un nodo aleatoriamente entre los nodos conectados (recorrer
* el array de puertas y elegir uno).
*/
virtual void fileRequest ( );
/**
* Petición de reputación: al principio se hará empleando el array de nodos
* directamente y buscando pero luego se usará un algoritmo de flooding.
*/
virtual void reputationRequest ( );
/**
* Graba estadísticas y logs.
*/
virtual void finish ( );
};
#endif