Reforming and invoke request data

Reformed the request data event
Trigger the request data event when a node comes back online and when a node is asked to store backup of a node
This commit is contained in:
sairajzero 2019-11-15 17:30:23 +05:30
parent b56f90d292
commit f123cf611d

View File

@ -6046,7 +6046,7 @@
console.log(result) console.log(result)
connectToAllBackupSupernode().then(result => { connectToAllBackupSupernode().then(result => {
console.log(result) console.log(result)
reactor.dispatchEvent("supernode_back_online",myFloID) reactor.dispatchEvent("indicate_supernode_up",myFloID)
}) })
}) })
}) })
@ -6308,21 +6308,21 @@
}) })
reactor.registerEvent("send_stored_backup"); reactor.registerEvent("send_stored_backup");
reactor.addEventListener("send_stored_backup", function (data) { reactor.addEventListener("send_stored_backup", function (event) {
console.log("send_stored_backup"); console.log("send_stored_backup");
//send stored backuped data to the requestor node //send stored backuped data to the requestor node
if(data.backupMsg.snfloID === myFloID || floGlobals.backupStoredList.includes(data.backupMsg.snfloID)){ if(event.snfloID === myFloID || floGlobals.backupStoredList.includes(event.snfloID)){
try{ try{
var requestorWS = new WebSocket("wss://" + floGlobals.supernodes[data.from].uri + "/ws") var requestorWS = new WebSocket("wss://" + floGlobals.supernodes[event.from].uri + "/ws")
requestorWS.onopen = (evt) => { requestorWS.onopen = (evt) => {
floGlobals.storageList.forEach(obs => { floGlobals.storageList.forEach(obs => {
compactIDB.searchData(obs, {lowerKey: data.backupMsg.lowerKey[obs]}, `SN_${data.backupMsg.snfloID}`).then(result => { compactIDB.searchData(obs, {lowerKey: event.lowerKey[obs]}, `SN_${data.backupMsg.snfloID}`).then(result => {
for(k in result){ for(k in result){
var sendData = { var sendData = {
from: myFloID, from: myFloID,
backupMsg: { backupMsg: {
type: "backupData", type: "backupData",
snfloID: data.backupMsg.snfloID, snfloID: event.snfloID,
key: k, key: k,
value: result[k] value: result[k]
}, },
@ -6334,8 +6334,8 @@
}) })
} }
requestorWS.onmessage = (evt) => console.log(evt.data); requestorWS.onmessage = (evt) => console.log(evt.data);
requestorWS.onclose = (evt) => console.log("Disconnected from " + data.from); requestorWS.onclose = (evt) => console.log("Disconnected from " + event.from);
requestorWS.onerror = (evt) => console.log("Error connecting to " + data.from); requestorWS.onerror = (evt) => console.log("Error connecting to " + event.from);
}catch(error){ }catch(error){
console.log(error.message) console.log(error.message)
} }
@ -6362,13 +6362,13 @@
reactor.dispatchEvent("stop_backup_serve", data.backupMsg.snfloID) reactor.dispatchEvent("stop_backup_serve", data.backupMsg.snfloID)
break; break;
case "startBackupStore": case "startBackupStore":
reactor.dispatchEvent("start_backup_store", data.backupMsg.snfloID) reactor.dispatchEvent("start_backup_store", {from:data.from, snfloID:data.backupMsg.snfloID})
break; break;
case "stopBackupStore": case "stopBackupStore":
reactor.dispatchEvent("stop_backup_store", data.backupMsg.snfloID) reactor.dispatchEvent("stop_backup_store", data.backupMsg.snfloID)
break; break;
case "dataRequest": case "dataRequest":
reactor.dispatchEvent("send_stored_backup", data) reactor.dispatchEvent("send_stored_backup", {from:data.from, snfloID:data.backupMsg.snfloID, lowerKey:data.backupMsg.lowerKey})
break; break;
default: default:
console.log(data.backupMsg) console.log(data.backupMsg)
@ -6382,7 +6382,7 @@
//request the backup data //request the backup data
var promises = [] var promises = []
for(var i=0; i < floGlobals.storageList.length; i++) for(var i=0; i < floGlobals.storageList.length; i++)
promises[i] = compactIDB.searchData(floGlobals.storageList[i], {lastOnly: true},`SN_${event.floID}`) promises[i] = compactIDB.searchData(floGlobals.storageList[i], {lastOnly: true},`SN_${event.snfloID}`)
Promise.all(promises).then(results => { Promise.all(promises).then(results => {
var lowerKey = {} var lowerKey = {}
for(var i=0; i < results.length; i++) for(var i=0; i < results.length; i++)
@ -6390,7 +6390,7 @@
lowerKey[floGlobals.storageList[i]] = key lowerKey[floGlobals.storageList[i]] = key
var backupMsg = { var backupMsg = {
type: "dataRequest", type: "dataRequest",
snfloID: event.floID, snfloID: event.snfloID,
lowerKey: lowerKey, lowerKey: lowerKey,
time: Date.now() time: Date.now()
} }
@ -6413,13 +6413,14 @@
reactor.registerEvent("indicate_supernode_up"); reactor.registerEvent("indicate_supernode_up");
reactor.addEventListener("indicate_supernode_up", function (snfloID) { reactor.addEventListener("indicate_supernode_up", function (snfloID) {
console.log("indicate_supernode_up"); console.log("indicate_supernode_up");
//send message to backup's backup to server for backup node (*to be rectified*) //send message to backup's backup to server for backup node
var backupMsg = { var backupMsg = {
type: "supernodeUp", type: "supernodeUp",
snfloID: snfloID, snfloID: snfloID,
time: Date.now() time: Date.now()
} }
reactor.dispatchEvent("send_message_to_backup_nodes", backupMsg) reactor.dispatchEvent("send_message_to_backup_nodes", backupMsg)
reactor.dispatchEvent("request_data", {holder:floGlobals.backupNodes[0].floID, snfloID:myFloID})
}) })
reactor.registerEvent("supernode_back_online"); reactor.registerEvent("supernode_back_online");
@ -6518,11 +6519,13 @@
}) })
reactor.registerEvent("start_backup_store"); reactor.registerEvent("start_backup_store");
reactor.addEventListener("start_backup_store", function (floID) { reactor.addEventListener("start_backup_store", function (event) {
console.log("start_backup_store :"+floID); console.log("start_backup_store :"+event.snfloID);
if(!floGlobals.backupStoredList.includes(floID)) if(!floGlobals.backupStoredList.includes(event.snfloID))
floGlobals.backupStoredList.push(floID) floGlobals.backupStoredList.push(event.snfloID)
initIndexedDBforSupernodeDataStorage(event.snfloID).then(result => {
reactor.dispatchEvent("request_data",{holder:event.from, snfloID:event.snfloID})
}).catch(error => console.log(error))
}) })
reactor.registerEvent("stop_backup_store"); reactor.registerEvent("stop_backup_store");