Comparing version 0.0.8 to 0.0.9
@@ -0,0 +0,0 @@ const AA = require('./libs/aa'); |
161
libs/aa.js
const EventEmitter = require('events'); | ||
const network = require('ocore/network'); | ||
const eventBus = require('ocore/event_bus'); | ||
const conf = require('ocore/conf'); | ||
const { getAllAuthorsAndOutputAddresses } = require('./utils'); | ||
@@ -16,43 +18,101 @@ class AA { | ||
eventBus.on('message_for_light', async (ws, subject, body) => { | ||
if (subject === 'light/aa_request' && body.aa_address === this.address) { | ||
const params = {address: body.aa_address, messages: body.unit.messages}; | ||
this.events.emit('new_request', params, body); | ||
this.arrRequestCBs.forEach(obj => { | ||
if (obj.func(params, body)) { | ||
obj.cb(params, body); | ||
} | ||
}); | ||
} else if (subject === 'light/aa_response' && body.aa_address === this.address) { | ||
const err = body.bounced ? body.response.error : false; | ||
let vars = !err ? await AA.getAAVars(this.address) : undefined; | ||
function aa_request(body) { | ||
const params = { address: body.aa_address, messages: body.unit.messages }; | ||
this.events.emit('new_request', params, body); | ||
this.arrRequestCBs.forEach(obj => { | ||
if (obj.func(params, body)) { | ||
obj.cb(params, body); | ||
} | ||
}); | ||
} | ||
const params = { address: body.aa_address, response: body.response }; | ||
this.events.emit('new_response', err, params, vars, body); | ||
this.arrResponseCBs.forEach(obj => { | ||
if (obj.func(err, params, vars, body)) { | ||
obj.cb(err, params, vars, body); | ||
} | ||
}); | ||
} else if (subject === 'light/aa_definition') { | ||
let def = body.messages.find(v => v.app === 'definition'); | ||
if(def) def = def.payload; | ||
async function aa_response(body) { | ||
const err = body.bounced ? body.response.error : false; | ||
let vars = !err ? await AA.getAAVars(this.address) : undefined; | ||
this.events.emit('new_aa_definition', def, body); | ||
this.arrDefinitionCBs.forEach(obj => { | ||
if (obj.func(def, body)) { | ||
obj.cb(def, body); | ||
const params = { address: body.aa_address, response: body.response }; | ||
this.events.emit('new_response', err, params, vars, body); | ||
this.arrResponseCBs.forEach(obj => { | ||
if (obj.func(err, params, vars, body)) { | ||
obj.cb(err, params, vars, body); | ||
} | ||
}); | ||
} | ||
function aa_definition(body) { | ||
let def = body.messages.find(v => v.app === 'definition'); | ||
if (def) def = def.payload; | ||
this.events.emit('new_aa_definition', def, body); | ||
this.arrDefinitionCBs.forEach(obj => { | ||
if (obj.func(def, body)) { | ||
obj.cb(def, body); | ||
} | ||
}); | ||
} | ||
function aa_definition_saved(body) { | ||
let def = body.messages.find(v => v.app === 'definition'); | ||
if (def) def = def.payload; | ||
this.events.emit('new_aa_definition_saved', def, body); | ||
this.arrDefinitionSavedCBs.forEach(obj => { | ||
if (obj.func(def, body)) { | ||
obj.cb(def, body); | ||
} | ||
}); | ||
} | ||
if (!conf.bLight) { | ||
const storage = require('ocore/storage'); | ||
const db = require('ocore/db'); | ||
eventBus.on('new_joint', objJoint => { | ||
const objUnit = objJoint.unit; | ||
const objAddresses = getAllAuthorsAndOutputAddresses(objUnit); | ||
if (!objAddresses) // voided unit | ||
return; | ||
const arrOutputAddresses = objAddresses.output_addresses; | ||
const arrBaseAAAddresses = objAddresses.base_aa_addresses; | ||
const arrAllAAAddresses = arrOutputAddresses.concat(arrBaseAAAddresses); | ||
if (arrAllAAAddresses.includes(this.address) && arrOutputAddresses.length > 0) { | ||
aa_request({ aa_address: this.address, unit: objUnit }); | ||
} | ||
if (arrBaseAAAddresses.includes(this.address)) { | ||
aa_definition(objUnit); | ||
} | ||
}); | ||
eventBus.on('aa_response', objAAResponse => { | ||
if(objAAResponse.aa_address === this.address) | ||
aa_response(objAAResponse) | ||
}); | ||
eventBus.on('aa_definition_saved', (payload, unit) => { | ||
const base_aa = payload.definition[1].base_aa; | ||
if (!base_aa) | ||
return; | ||
if(base_aa !== this.address) | ||
return; | ||
storage.readJoint(db, unit, { | ||
ifNotFound: function () { | ||
console.log('recently saved unit ' + unit + ' not found'); | ||
}, | ||
ifFound: function (objJoint) { | ||
aa_definition_saved(objJoint.unit); | ||
} | ||
}); | ||
} else if (subject === 'light/aa_definition_saved') { | ||
let def = body.messages.find(v => v.app === 'definition'); | ||
if (def) def = def.payload; | ||
this.events.emit('new_aa_definition_saved', def, body); | ||
this.arrDefinitionSavedCBs.forEach(obj => { | ||
if (obj.func(def, body)) { | ||
obj.cb(def, body); | ||
} | ||
}); | ||
} | ||
}); | ||
}) | ||
}) | ||
} else { | ||
eventBus.on('message_for_light', (ws, subject, body) => { | ||
if (subject === 'light/aa_request' && body.aa_address === this.address) { | ||
aa_request(body); | ||
} else if (subject === 'light/aa_response' && body.aa_address === this.address) { | ||
aa_response(body); | ||
} else if (subject === 'light/aa_definition') { | ||
aa_definition(body); | ||
} else if (subject === 'light/aa_definition_saved') { | ||
aa_definition_saved(body); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -77,6 +137,8 @@ | ||
__listen() { | ||
network.addLightWatchedAa(this.address, undefined, err => { | ||
if (err) | ||
throw new Error(err); | ||
}); | ||
if (conf.bLight) { | ||
network.addLightWatchedAa(this.address, undefined, err => { | ||
if (err) | ||
throw new Error(err); | ||
}); | ||
} | ||
} | ||
@@ -88,7 +150,14 @@ | ||
return new Promise(resolve => { | ||
network.requestFromLightVendor('light/get_aa_state_vars', | ||
_params, | ||
(ws, request, response) => { | ||
return resolve(response); | ||
if (conf.bLight) { | ||
network.requestFromLightVendor('light/get_aa_state_vars', | ||
_params, | ||
(ws, request, response) => { | ||
return resolve(response); | ||
}); | ||
} else { | ||
const storage = require('ocore/storage'); | ||
storage.readAAStateVars(_params.address, _params.var_prefix_from || '', _params.var_prefix_to || '', _params.limit || 2000, (objStateVars) => { | ||
return resolve(objStateVars); | ||
}); | ||
} | ||
}); | ||
@@ -95,0 +164,0 @@ } |
107
libs/aas.js
const EventEmitter = require('events'); | ||
const network = require('ocore/network'); | ||
const eventBus = require('ocore/event_bus'); | ||
const conf = require('ocore/conf'); | ||
const { getAllAuthorsAndOutputAddresses } = require('./utils'); | ||
@@ -16,24 +18,56 @@ class AAs { | ||
eventBus.on('message_for_light', async (ws, subject, body) => { | ||
if (subject === 'light/aa_request' && this.addresses.includes(body.aa_address)) { | ||
const params = { address: body.aa_address, messages: body.unit.messages }; | ||
this.events.emit('new_request', params, body); | ||
this.arrRequestCBs.forEach(obj => { | ||
if (obj.func(params, body)) { | ||
obj.cb(params, body); | ||
function aa_request(body){ | ||
const params = { address: body.aa_address, messages: body.unit.messages }; | ||
this.events.emit('new_request', params, body); | ||
this.arrRequestCBs.forEach(obj => { | ||
if (obj.func(params, body)) { | ||
obj.cb(params, body); | ||
} | ||
}); | ||
} | ||
async function aa_response(body){ | ||
const err = body.bounced ? body.response.error : false; | ||
let vars = !err ? await AAs.getAAVars(body.aa_address) : undefined; | ||
const params = { address: body.aa_address, response: body.response }; | ||
this.events.emit('new_response', err, params, vars, body); | ||
this.arrResponseCBs.forEach(obj => { | ||
if (obj.func(err, params, vars, body)) { | ||
obj.cb(err, params, vars, body); | ||
} | ||
}); | ||
} | ||
if(!conf.bLight) { | ||
eventBus.on('new_joint', objJoint => { | ||
const objUnit = objJoint.unit; | ||
const objAddresses = getAllAuthorsAndOutputAddresses(objUnit); | ||
if (!objAddresses) // voided unit | ||
return; | ||
const arrOutputAddresses = objAddresses.output_addresses; | ||
const arrBaseAAAddresses = objAddresses.base_aa_addresses; | ||
const arrAllAAAddresses = arrOutputAddresses.concat(arrBaseAAAddresses); | ||
this.addresses.forEach(address => { | ||
if (arrAllAAAddresses.includes(address) && arrOutputAddresses.length > 0) { | ||
aa_request({ aa_address: address, unit: objUnit }); | ||
} | ||
}); | ||
} else if (subject === 'light/aa_response' && this.addresses.includes(body.aa_address)) { | ||
const err = body.bounced ? body.response.error : false; | ||
let vars = !err ? await AAs.getAAVars(body.aa_address) : undefined; | ||
}); | ||
const params = { address: body.aa_address, response: body.response }; | ||
this.events.emit('new_response', err, params, vars, body); | ||
this.arrResponseCBs.forEach(obj => { | ||
if (obj.func(err, params, vars, body)) { | ||
obj.cb(err, params, vars, body); | ||
} | ||
eventBus.on('aa_response', objAAResponse => { | ||
this.addresses.forEach(address => { | ||
if (objAAResponse.aa_address === address) | ||
aa_response(objAAResponse) | ||
}); | ||
} | ||
}); | ||
}); | ||
}else { | ||
eventBus.on('message_for_light', async (ws, subject, body) => { | ||
if (subject === 'light/aa_request' && this.addresses.includes(body.aa_address)) { | ||
aa_request(body); | ||
} else if (subject === 'light/aa_response' && this.addresses.includes(body.aa_address)) { | ||
aa_response(body); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -55,10 +89,3 @@ | ||
__listen(address) { | ||
network.addLightWatchedAa(address, undefined, err => { | ||
if (err) | ||
throw new Error(err); | ||
}); | ||
} | ||
__listenAllAddresses() { | ||
this.addresses.forEach(address => { | ||
if(conf.bLight) { | ||
network.addLightWatchedAa(address, undefined, err => { | ||
@@ -68,14 +95,32 @@ if (err) | ||
}); | ||
}) | ||
} | ||
} | ||
__listenAllAddresses() { | ||
if(conf.bLight) { | ||
this.addresses.forEach(address => { | ||
network.addLightWatchedAa(address, undefined, err => { | ||
if (err) | ||
throw new Error(err); | ||
}); | ||
}) | ||
} | ||
} | ||
static getAAVars(address, params = {}) { | ||
const _params = Object.assign({ address }, params); | ||
return new Promise(resolve => { | ||
network.requestFromLightVendor('light/get_aa_state_vars', | ||
_params, | ||
(ws, request, response) => { | ||
return resolve(response); | ||
if (conf.bLight) { | ||
network.requestFromLightVendor('light/get_aa_state_vars', | ||
_params, | ||
(ws, request, response) => { | ||
return resolve(response); | ||
}); | ||
} else { | ||
const storage = require('ocore/storage'); | ||
storage.readAAStateVars(_params.address, _params.var_prefix_from || '', _params.var_prefix_to || '', _params.limit || 2000, (objStateVars) => { | ||
return resolve(objStateVars); | ||
}); | ||
} | ||
}); | ||
@@ -82,0 +127,0 @@ } |
{ | ||
"name": "aagent.js", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "It is library to work with Autonomous Agents on Obyte", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,0 +0,0 @@ # aagent.js |
55533
7
290