Commit 229d1634 authored by Virgile JARRY's avatar Virgile JARRY
Browse files

Merge branch 'plusMojette' into dev

Conflicts:
	code/mojette.js
	code/testsNode/passport-local/public/js/clientCloudfrag.js
parents 05277c18 ef031181
......@@ -4,14 +4,17 @@ function matrixChecker(matrix1, matrix2)
{
var matrixWidth = matrix1.length;
var matrixHeight = matrix1[0].length;
for (var x=0; x<matrixWidth; x++)
for (var y=0; y<matrixHeight; y++)
if (matrix1[x][y] != matrix2[x][y])
for (var x=0; x<matrixWidth; x++) {
for (var y=0; y<matrixHeight; y++) {
if (matrix1[x][y] != matrix2[x][y]) {
return false;
}
}
}
return true;
}
//[int8array, int8array, int8array, int8array]
function project(matrix, p, q)
{
var matrixWidth = matrix[0].length;
......@@ -115,6 +118,7 @@ function generateRandomMatrix(width, height) {
return matrix;
}
function generateProjectionSet(matrix, projectionCount) {
var projections = [];
......
......@@ -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
var log = true;
Date.prototype.today = function ()
{
......@@ -10,6 +11,75 @@ Date.prototype.timeNow = function ()
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds()+":"+ ((this.getMilliseconds() < 10)?"0":"") + this.getMilliseconds();
}
var ajax = {};
ajax.x = function () {
if (typeof XMLHttpRequest !== 'undefined') {
return new XMLHttpRequest();
}
var versions = [
"MSXML2.XmlHttp.6.0",
"MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"
];
var xhr;
for (var i = 0; i < versions.length; i++) {
try {
xhr = new ActiveXObject(versions[i]);
break;
} catch (e) {
}
}
return xhr;
};
ajax.send = function (url, callback, method, data, async) {
if (async === undefined) {
async = true;
}
var x = ajax.x();
x.open(method, url, async);
x.onreadystatechange = function () {
if (x.readyState == 4) {
callback(x.responseText)
}
};
if (method == 'POST') {
x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
}
x.send(data)
};
ajax.get = function (url, data, callback, async) {
var query = [];
for (var key in data) {
query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
}
ajax.send(url + (query.length ? '?' + query.join('&') : ''), callback, 'GET', null, async)
};
ajax.post = function (url, data, callback, async) {
var query = [];
data = JSON.parse(data);
for (var key in data) {
console.log("key : "+key+" : "+data[key]);
query.push(key + '=' + data[key]);
}
console.log(query);
ajax.send(url, callback, 'POST', query.join('&'), async)
};
function startReadFromDrag(evt, nbServersChoisis)
{
evt.stopPropagation();
......@@ -37,12 +107,11 @@ function handleDragOver(evt)
}
function loadMetadata(div)
{
var coMetadata = new WebSocket('ws://'+metadataServerIp+':'+metadataServerPort+'/getMetadata');
coMetadata.onerror = function(err) console.log(err);
coMetadata.onmessage = function(msg)
{
this.close();
var metadata = JSON.parse(msg.data);
ajax.get('/getMetadata', {}, function(res){
var metadata = JSON.parse(res);
var html = div.innerHTML;
for(var i=0;i<metadata.length;i++)
{
......@@ -63,7 +132,8 @@ function loadMetadata(div)
html = html+"</a></p>";
}
div.innerHTML = html;
}
});
}
function process(file, name, nbFrags)
......@@ -87,7 +157,6 @@ function process(file, name, nbFrags)
metadata.metadataId = SHA256(s);
nbMetaFrags = 0;
nbServers = nbFrags;
//nbServers = storageServers.length;
// ####################################
// FRAGMENTATION ICI
......@@ -96,22 +165,58 @@ function process(file, name, nbFrags)
var begin = 0;
var end = 0;
for(var i=0;i<nbServers;i++)
var matrix = [];
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<nbFrags;i++)
{
connections[i] = new WebSocket('ws://'+storageServers[i].ip+':'+storageServers[i].port+'/sendFragment');
if(i+1 < nbServers)
end = Math.floor(begin + (file.byteLength/nbServers));
else if(i == nbServers-1)
if(i+1 < nbFrags)
end = Math.ceil(begin + (file.byteLength/nbFrags));
else if(i == 3)
end = file.byteLength;
if(log)
console.log("fragmentation : ["+begin+" -> "+end+"]");
connections[i].cloudFragFrag = file.slice(begin,end);
connections[i].cloudFragFragStart = begin;
connections[i].cloudFragFragEnd = end;
console.log("Ligne de matrice : ["+begin+" -> "+end+"] : "+(end-begin)+" octets.");
if(end-begin < fragLength)
{
console.log("fichier pas multiple de nbfrags donc zeropadding");
//on est ici dans le dernier fragment, qui est plus petit que les précédents, il faut le zero-padder !
var buf = new ArrayBuffer(fragLength);
var view = new Int8Array(buf);
var leslice = file.slice(begin,end);
var leslicev = new Int8Array(leslice);
for(var x=0;x<leslicev.length;x++)
view[x] = leslicev[x];
matrix.push(view);
}
else
{
var aAjouter = new Int8Array(file.slice(begin,end));
matrix.push(aAjouter);
}
begin = end;
}
projections = [];
for (var i=0; i<nbServers; i++)
{
connections[i] = new WebSocket('ws://'+storageServers[i].ip+':'+storageServers[i].port+'/sendFragment');
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;});
// ####################################
// ###############ENVOI DES PROJECTIONS
// ####################################
for(var i=0;i<nbServers;i++)
{
......@@ -141,15 +246,17 @@ 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,
p : this.cloudFragFrag.p,
q : this.cloudFragFrag.q,
offset : this.cloudFragFrag.offset/*,
start: this.cloudFragFragStart,
end : this.cloudFragFragEnd
end : this.cloudFragFragEnd*/
});
this.close();
nbMetaFrags = nbMetaFrags + 1;
......@@ -171,32 +278,25 @@ function process(file, name, nbFrags)
function sendMetadata(metadata, server)
{
var co = new WebSocket('ws://'+server.ip+':'+server.port+'/sendMetadata');
co.onerror = function(err)
console.log(err);
co.onopen = function()
{
if(log)
console.log("connexion au serveur de métadonnées successful");
this.send(JSON.stringify(metadata));
}
co.onmessage = function(msg)
{
if(msg.data == "ok")
console.log("sending metadata");
var frags = JSON.stringify(metadata.fragments);
delete metadata["fragments"];
ajax.post('/sendMetadata', JSON.stringify({"meta":JSON.stringify(metadata), "frags":frags}), function(res) {
if(res == "ok")
{
if(log)
console.log("metadata bien enregistrée");
this.close();
console.log("metadata correctement enregistree");
location.reload(true);
}
else if(msg.data == "err")
console.log("erreur sending metadata");
}
else
console.log("erreur d'enregistrement de la metadonnée");
});
}
function delFile(metadata)
{
console.log(metadata);
metadata = window.atob(metadata);
metadata = JSON.parse(metadata);
var fragments = metadata.fragments;
......@@ -233,7 +333,6 @@ function delFile(metadata)
};
}
var metaCo = new WebSocket('ws://'+metadataServerIp+':'+metadataServerPort+'/delMetadata');
console.log(metadata);
metaCo.cloudFragMetaId = metadata.metadataId;
......@@ -274,7 +373,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;
......@@ -289,28 +390,64 @@ function getFile(metadata)
};
connections[j].onmessage = function(msg)
{
donnees.push(msg.data);
nbFragsRecus = nbFragsRecus + 1;
if(nbFragsRecus == fragments.length)
{
var finalFile = new Blob(donnees);
//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();
};
}
......
This diff is collapsed.
"use strict";
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])
return false;
return true;
}
function project(matrix, p, q)
{
var matrixWidth = matrix[0].length;
var matrixHeight = matrix.length;
var binsCount = (matrixWidth-1)*Math.abs(q)+(matrixHeight-1)*Math.abs(p)+1;
var projection = new Int8Array(binsCount);
var binCoordOffset = 0;
if (p<0) {
binCoordOffset = -(p*(matrixHeight-1)-q*(matrixWidth-1));
} else {
binCoordOffset = q*(matrixWidth-1);
}
//for (var i=0; i<binsCount; i++) projection.push(0);
projection.p = p;
projection.q = q;
projection.offset = binCoordOffset;
for (var x=0; x<matrixWidth; x++) {
for (var y=0; y<matrixHeight; y++) {
var b = p*y-q*x;
projection[b+binCoordOffset] += matrix[y][x];
}
}
return projection;
}
function reverseMoj(projections, matrixWidth, matrixHeight)
{
var projectionsCount = projections.length;
var matrix = [];
for (var i=0; i<matrixHeight; i++) {
matrix.push(new Int8Array(matrixWidth));
}
projections.sort(function(projection1, projection2) {return projection1.p > projection2.p;});
var patternCoord = [];
patternCoord.push(projections[0].p);
for (var i=1; i<projections.length; i++) { patternCoord.push(patternCoord[i-1]+projections[i].p); }
var maximum = Math.abs(patternCoord[0]);
for (var i in patternCoord)
if (Math.abs(patternCoord[i]) > maximum)
maximum = Math.abs(patternCoord[i]);
if (patternCoord[patternCoord.length-1] > 0) {
for (var i in patternCoord) {
patternCoord[i] -= patternCoord[patternCoord.length-1];
}
}
var maxWidth = matrixWidth+maximum;
// on parcourt toute la matrice
for (var k=0; k<=maxWidth; k++) {
for (var l=0; l<matrixHeight; l++) {
var cK = k+patternCoord[l];
if (cK > -1 && cK < matrixWidth) {
/*
et on résoud le pattern dans pour les cases valides càd les cases dont
les coordonnées sont bien dans la matrice ce qui n'est pas toujours
vrai si le pattern dépasse au début ou à la fin du parcourt.
*/
var currentProjection = projections[l];
var currentP = currentProjection.p;
var currentQ = currentProjection.q;
var currentBin = l*currentP-cK*currentQ;
var newPixel = currentProjection[currentBin+currentProjection.offset];
matrix[l][cK] = newPixel;
/*
màj des projections dès qu'un pixel est reconstruit
*/
for (var i=0; i<projectionsCount; i++) {
var p = projections[i].p;
var q = projections[i].q;
var b = l*p-cK*q;
projections[i][b+projections[i].offset] -= newPixel;
}
}
}
}
return matrix;
}
function generateRandomMatrix(width, height) {
var matrix = [];
for (var i=0; i<height; i++) {
matrix.push(new Int8Array(width));
for (var j=0; j<width; j++) {
matrix[i][j] = Math.floor((Math.random()*128));
}
}
return matrix;
}
function generateProjectionSet(matrix, projectionCount) {
var projections = [];
for (var i=0; i<projectionCount; i++) {
projections.push(project(matrix, i-Math.floor(projectionCount/2), 1));
}
return projections;
}
/*
function tester(testCount) {
var err = 0;
var totalSize = 0;
var fs = require('fs');
var util = require('util');
var logFile = fs.createWriteStream('log', { flags: 'w' });
var finalLog = "";
for (var i=0; i<testCount; i++) {
var width = Math.floor(Math.random()*5000000);
var height = 4;
var size = width*height/1000000;
var log;
log = "\nTest "+i+" matrix "+width+" x "+height+" ("+size+" MB)";
var matrix = generateRandomMatrix(width, height);
var projections = generateProjectionSet(matrix, height);
var matrix2 = reverseMoj(projections, width, height);
if (!matrixChecker(matrix, matrix2)) {
err++;
log += " [ERROR]";
} else {
log += " [OK]";
}
totalSize += size;
console.log(log+"\nTotal size : "+Math.floor(totalSize)+"MB total err : "+err+" ("+err/(i+1)*100+"%)");
finalLog += log;
}
fs.writeFileSync("./log", finalLog+"\n"+testCount+" tests | total size : "+Math.floor(totalSize)+"MB | total err : "+err+" ("+err/(i+1)*100+"%)");
}
tester(1000);*/
\ No newline at end of file
......@@ -64,7 +64,6 @@ module.exports = function (app) {
req.logIn(user, function(err){
if(err)
return next(err);
app.set('user',user);