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)
connectToAllBackupSupernode().then(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.addEventListener("send_stored_backup", function (data) {
reactor.addEventListener("send_stored_backup", function (event) {
console.log("send_stored_backup");
//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{
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) => {
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){
var sendData = {
from: myFloID,
backupMsg: {
type: "backupData",
snfloID: data.backupMsg.snfloID,
snfloID: event.snfloID,
key: k,
value: result[k]
},
@ -6334,8 +6334,8 @@
})
}
requestorWS.onmessage = (evt) => console.log(evt.data);
requestorWS.onclose = (evt) => console.log("Disconnected from " + data.from);
requestorWS.onerror = (evt) => console.log("Error connecting to " + data.from);
requestorWS.onclose = (evt) => console.log("Disconnected from " + event.from);
requestorWS.onerror = (evt) => console.log("Error connecting to " + event.from);
}catch(error){
console.log(error.message)
}
@ -6362,13 +6362,13 @@
reactor.dispatchEvent("stop_backup_serve", data.backupMsg.snfloID)
break;
case "startBackupStore":
reactor.dispatchEvent("start_backup_store", data.backupMsg.snfloID)
reactor.dispatchEvent("start_backup_store", {from:data.from, snfloID:data.backupMsg.snfloID})
break;
case "stopBackupStore":
reactor.dispatchEvent("stop_backup_store", data.backupMsg.snfloID)
break;
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;
default:
console.log(data.backupMsg)
@ -6382,7 +6382,7 @@
//request the backup data
var promises = []
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 => {
var lowerKey = {}
for(var i=0; i < results.length; i++)
@ -6390,7 +6390,7 @@
lowerKey[floGlobals.storageList[i]] = key
var backupMsg = {
type: "dataRequest",
snfloID: event.floID,
snfloID: event.snfloID,
lowerKey: lowerKey,
time: Date.now()
}
@ -6413,13 +6413,14 @@
reactor.registerEvent("indicate_supernode_up");
reactor.addEventListener("indicate_supernode_up", function (snfloID) {
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 = {
type: "supernodeUp",
snfloID: snfloID,
time: Date.now()
}
reactor.dispatchEvent("send_message_to_backup_nodes", backupMsg)
reactor.dispatchEvent("request_data", {holder:floGlobals.backupNodes[0].floID, snfloID:myFloID})
})
reactor.registerEvent("supernode_back_online");
@ -6518,11 +6519,13 @@
})
reactor.registerEvent("start_backup_store");
reactor.addEventListener("start_backup_store", function (floID) {
console.log("start_backup_store :"+floID);
if(!floGlobals.backupStoredList.includes(floID))
floGlobals.backupStoredList.push(floID)
reactor.addEventListener("start_backup_store", function (event) {
console.log("start_backup_store :"+event.snfloID);
if(!floGlobals.backupStoredList.includes(event.snfloID))
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");