Commit b9cb24ea authored by Virgile JARRY's avatar Virgile JARRY
Browse files

MOJETTE OK

parent dd4e78f2
......@@ -7,13 +7,17 @@ var Metadata = new Schema({
owner : String,
filename : String,
filesize : Number,
fragLength : Number,
fragments : [{
ip: String,
port: Number,
hashGet : String,
hashDel : String,
p : Number,
q : Number,
offset : Number/*
start : Number,
end : Number
end : Number*/
}]
});
module.exports = mongoose.model('Metadata', Metadata);
\ No newline at end of file
......@@ -87,7 +87,6 @@ function process(file, name, nbFrags)
metadata.metadataId = SHA256(s);
nbMetaFrags = 0;
nbServers = nbFrags;
//nbServers = storageServers.length;
// ####################################
// FRAGMENTATION ICI
......@@ -97,14 +96,16 @@ function process(file, name, nbFrags)
var begin = 0;
var end = 0;
var matrix = [];
var fragLength = Math.ceil(begin + (file.byteLength / 4));
var fragLength = Math.ceil(begin + (file.byteLength / nbFrags));
metadata.fragLength = fragLength;
console.log("Taille d'une ligne de matrice : "+fragLength+" octets.");
for(var i=0;i<4;i++)
for(var i=0;i<nbFrags;i++)
{
if(i+1 < 4)
end = Math.ceil(begin + (file.byteLength/4));
if(i+1 < nbFrags)
end = Math.ceil(begin + (file.byteLength/nbFrags));
else if(i == 3)
end = file.byteLength;
if(log)
......@@ -121,37 +122,31 @@ function process(file, name, nbFrags)
for(var x=0;x<leslicev.length;x++)
view[x] = leslicev[x];
console.log("fin recopie - "+x+"/"+(fragLength));
for(var y=0;y<fragLength-x;y++)
view[y] = 0;
console.log(view[y]);
matrix.push(view);
}
else
{
matrix.push(new Int8Array(file.slice(begin,end)));
var aAjouter = new Int8Array(file.slice(begin,end));
matrix.push(aAjouter);
}
/*connections[i].cloudFragFragStart = begin;
connections[i].cloudFragFragEnd = end;*/
begin = end;
}
var projections = [];
projections = [];
for (var i=0; i<nbServers; i++)
{
connections[i] = new WebSocket('ws://'+storageServers[i].ip+':'+storageServers[i].port+'/sendFragment');
projections.push(project(matrix, i, 1));
connections[i].cloudFragFrag = project(matrix, i, 1);
var laProjection = project(matrix, i-Math.floor(nbServers/2), 1);
projections.push(laProjection);
connections[i].cloudFragFrag = laProjection;
}
projections.sort(function(projection1, projection2) {return projection1.p > projection2.p;});
/*console.log(nbServers);
if(!matrixChecker(matrix, reverse(projections, fragLength, nbServers)))
{
//erreur de mojette
return;
}*/
// ####################################
// ###############ENVOI DES PROJECTIONS
// ####################################
for(var i=0;i<nbServers;i++)
{
......@@ -164,7 +159,6 @@ function process(file, name, nbFrags)
if(log)
console.log("connection successful to "+this.url);
this.binaryType = "arraybuffer";
console.log(this.cloudFragFrag.length);
this.send(this.cloudFragFrag);
if(log)
console.log("data sent to "+this.url);
......@@ -182,13 +176,15 @@ function process(file, name, nbFrags)
var servport = a.split(":");
servport = servport[1].split("/");
servport = servport[0];
metadata.fragments.push(
{
ip : servip,
port : servport,
hashGet : hashes.get,
hashDel : hashes.del/*,
hashDel : hashes.del,
p : this.cloudFragFrag.p,
q : this.cloudFragFrag.q,
offset : this.cloudFragFrag.offset/*,
start: this.cloudFragFragStart,
end : this.cloudFragFragEnd*/
});
......@@ -228,7 +224,7 @@ function sendMetadata(metadata, server)
if(log)
console.log("metadata bien enregistrée");
this.close();
//location.reload(true);
location.reload(true);
}
else if(msg.data == "err")
console.log("erreur sending metadata");
......@@ -237,7 +233,6 @@ function sendMetadata(metadata, server)
function delFile(metadata)
{
console.log(metadata);
metadata = window.atob(metadata);
metadata = JSON.parse(metadata);
var fragments = metadata.fragments;
......@@ -274,7 +269,6 @@ function delFile(metadata)
};
}
var metaCo = new WebSocket('ws://'+metadataServerIp+':'+metadataServerPort+'/delMetadata');
console.log(metadata);
metaCo.cloudFragMetaId = metadata.metadataId;
......@@ -315,7 +309,9 @@ function getFile(metadata)
{
connections[i] = new WebSocket('ws://'+fragments[i].ip+':'+fragments[i].port+'/getFragment');
connections[i].cloudFragIp = fragments[i].ip;
connections[i].cloudFragP = fragments[i].p;
connections[i].cloudFragQ = fragments[i].q;
connections[i].cloudFragBinOffset = fragments[i].offset;
connections[i].cloudFragPort = fragments[i].port;
connections[i].cloudFragHash = fragments[i].hashGet;
connections[i].cloudFragMetadata = metadata;
......@@ -330,32 +326,64 @@ function getFile(metadata)
};
connections[j].onmessage = function(msg)
{
var temp = new ArrayBuffer(msg.data);
donnees.push(msg.data);
nbFragsRecus = nbFragsRecus + 1;
if(nbFragsRecus == fragments.length)
{
console.log(donnees);
/*var finalFile = new Blob(donnees);
console.log(donnees[0].byteLength);
//var reconstruit = reverse(finalFile, final)
return;
//tous les fragments sont reçus
var filename = this.cloudFragMetadata.filename;
var objurl = window.URL.createObjectURL(finalFile);
var a = document.createElement('a');
a.style = "display:none;";
a.id = filename+"cloudFragDownloadLink";
var arrayBuffer;
var fileReader = new FileReader();
fileReader.cloudFragP = this.cloudFragP;
fileReader.cloudFragQ = this.cloudFragQ;
fileReader.cloudFragBinOffset = this.cloudFragBinOffset;
fileReader.cloudFragMetadata = this.cloudFragMetadata;
fileReader.onload = function() {
arrayBuffer = this.result;
var temp = new Int8Array(arrayBuffer);
temp.p = this.cloudFragP;
temp.q = this.cloudFragQ;
temp.offset = this.cloudFragBinOffset;
donnees.push(temp);
nbFragsRecus = nbFragsRecus + 1;
if(nbFragsRecus == fragments.length)
{
donnees.sort(function(projection1, projection2) {return projection1.p > projection2.p;});
donnees.forEach(function(ligne){
console.log("ligne");
var w = "";
for(var x=0;x<10;x++)
w = w + ligne[x]+" ";
console.log(w+"("+ligne.p+","+ligne.q+")"+ligne.offset);
});
var matriceR = reverseMoj(donnees, metadata.fragLength, this.cloudFragMetadata.fragments.length);
var toCut = (this.cloudFragMetadata.fragments.length*this.cloudFragMetadata.fragLength)- this.cloudFragMetadata.filesize;
var tmp = matriceR[matriceR.length-1].slice(0,(matriceR[matriceR.length-1].byteLength)-toCut);
matriceR[matriceR.length-1] = tmp;
var finalFile = new Blob(matriceR);
var filename = this.cloudFragMetadata.filename;
var objurl = window.URL.createObjectURL(finalFile);
var a = document.createElement('a');
a.style = "display:none;";
a.id = filename+"cloudFragDownloadLink";
a.href = objurl;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function(){
window.URL.revokeObjectURL(objurl);
a.parentNode.removeChild(a);
}, 10);*/
}
a.href = objurl;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function(){
window.URL.revokeObjectURL(objurl);
a.parentNode.removeChild(a);
}, 10);
}
};
fileReader.readAsArrayBuffer(msg.data);
this.close();
};
}
......
......@@ -4,6 +4,11 @@ function matrixChecker(matrix1, matrix2)
{
var matrixWidth = matrix1.length;
var matrixHeight = matrix1[0].length;
var matrix2Width = matrix2.length;
var matrix2Height = matrix2[0].length;
if(matrix2Width != matrixWidth || matrix2Height != matrixHeight)
return false;
for (var x=0; x<matrixWidth; x++)
for (var y=0; y<matrixHeight; y++)
if (matrix1[x][y] != matrix2[x][y])
......@@ -41,7 +46,7 @@ function project(matrix, p, q)
return projection;
}
function reverse(projections, matrixWidth, matrixHeight)
function reverseMoj(projections, matrixWidth, matrixHeight)
{
var projectionsCount = projections.length;
......@@ -147,7 +152,7 @@ function tester(testCount) {
var matrix = generateRandomMatrix(width, height);
var projections = generateProjectionSet(matrix, height);
var matrix2 = reverse(projections, width, height);
var matrix2 = reverseMoj(projections, width, height);
if (!matrixChecker(matrix, matrix2)) {
err++;
......
......@@ -121,7 +121,8 @@ module.exports = function (app) {
owner: app.get('user').username,
fragments: lametadonnee.fragments,
filename: lametadonnee.filename,
filesize: lametadonnee.filesize
filesize: lametadonnee.filesize,
fragLength: lametadonnee.fragLength
});
newMetadata.save(function(err, newMetadata){
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment