-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
209 lines (188 loc) · 5.67 KB
/
db.js
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
const path = require("path");
const Server = require('socket.io');
const io_client = require('socket.io-client');
const crypto = require('crypto');
// Retourne l'empreinte de data.
const getHash = function getHash(data) {
return crypto.createHash('sha256').update(data, 'utf8').digest('hex');
}
const getConfig = function getConfig() {
if (process.argv.length < 3) {
console.error("Vous devez indiquer le fichier de configuration");
console.info(`Usage : node ${process.argv[1]} <fileName>`);
throw new Error("Need config filename");
} else {
return require(path.resolve(process.argv[2]));
}
}
const extractHorodatage = function(database) {
return Object.keys(database).reduce(function(result, key) {
result[key] = {
timestamp: database[key].timestamp,
hash: database[key].hash
};
return result;
}, {});
};
const last = function(database){
console.log("Last called");
lastIndex=-1;
Object.keys(database).map((key) => {
if(database[key]){
if(database[key].index > lastIndex){
console.log(database[key].index);
lastIndex= database[key].index;
}
}
});
console.log("Last returned: ",lastIndex);
return lastIndex;
}
const record = function(database,index){
console.log("Record called");
if(index == -1){
console.log("record returned: null");
return null;
}
console.log("Debut record");
return Object.keys(database).reduce(function(result, key){
console.log("Resultat: ",result);
if(result[key].index == index){
console.log("record returned: ",database[key]);
return database[key];
}
});
}
const actualisationDB = function(pairs) {
pairs.map((socket_elem) => {
console.log("On demande les clefs");
socket_elem.emit('keys', (response) => {
Object.keys(response).map((key) => {
if(key in db){
if(key.timestamp < db[key].timestamp){
socket_elem.emit('get', key, (val) => {
db[key] = {
value: val.value,
timestamp: val.timestamp,
hash: val.hash,
index: val.index,
previous: val.previous
};
});
} else if((key.timestamp == db[key].timestamp) && (key.hash != db[key].hash)){
socket_elem.emit('get', key, (val) => {
if(key.hash == getHash(val.value)){
db[key] = {
value: val.value,
timestamp: val.timestamp,
hash: val.hash,
index: val.index,
previous: val.previous
};
}
});
}
} else {
socket_elem.emit('get', key, (val) => {
db[key] = {
value: val.value,
timestamp: val.timestamp,
hash: val.hash,
index: val.index,
previous: val.previous
};
});
}
});
});
});
}
const config = getConfig();
console.log("PORT: ", config.port);
const PORT = config.port;
const io = new Server(PORT, {
path: '/dbyb',
serveClient: false,
});
const socket_pairs = config.pairs.map((port)=> {
return io_client(`http://localhost:${port}`, {
path: '/dbyb',
});
});
console.log(`Serveur lancé sur le port ${PORT}.`);
actualisationDB(socket_pairs);
const db = Object.create(null);
setInterval(() => {
actualisationDB(socket_pairs);
}, 10000); // 10000 millisecondes = 10 secondes
io.on('connect', (socket) => {
console.log('Nouvelle connexion');
socket.on('get', function(field, callback){
console.log(`get ${field}: ${db[field]}`);
callback(db[field]);
});
socket.on('set', function(field, value, callback){
if(value.timestamp){
console.log("On recoit un set avec un timestamp");
if(!(field in db)){
console.log("On ne connait pas le champ");
db[field] = {
index: last(db)+1,
previous: record(db,last(db)),
value: value.value,
timestamp: value.timestamp,
hash: value.hash
};
callback(true);
} else {
console.log(`Comparateur de timestamp : ${db[field].timestamp} and ${value.timestamp}`);
if(db[field].timestamp < value.timestamp) {
console.log("On recoit un set avec un timestamp superieur au notre, on jette");
console.log(`set error : Field ${field} exists.`);
//db[field] = value;
callback(false);
} else {
console.log("On recoit un set avec un timestamp inferieur au notre, on garde");
db[field] = {
value: value.value,
timestamp: value.timestamp,
hash: value.hash,
previous: value.previous,
index: value.index
};
callback(true);
}
}
} else {
if(field in db){
console.log("On recoit une ancienne valeur du client");
console.log(`set error : Field ${field} exists.`);
//db[field] = value;
callback(false);
} else {
console.log("On recoit une nouvelle valeur du client");
console.log(`set ${field} : ${value}`);
db[field] = {
index: last(db)+1,
previous: record(db,last(db)),
timestamp: new Date(),
hash: getHash(value),
value: value
};
callback(true);
setTimeout(() => {
socket_pairs.map((element) => {
element.emit('set',field, db[field], (response) => {
console.log(`la paired DB a repondu: ${response}`);
});
});
}, 5000);
}
}
});
socket.on('keys', function(callback){
if(db){
callback(extractHorodatage(db));
}
})
});