Skip to content

Commit

Permalink
fixes for seneca act
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceaalexandru committed Dec 3, 2015
1 parent 92cf7be commit c9d71a8
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 26 deletions.
10 changes: 6 additions & 4 deletions controller/protocol/base/identify.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ module.exports = function( options ) {
var protocol_version = seneca.export( 'constants/protocol_version' )

function identify( args, response ) {
seneca.act( "role: 'crypt', decrypt: 'message'", {message: args.command}, function(err, decrypt){
var that = this

that.act( "role: 'crypt', decrypt: 'message'", {message: args.command}, function(err, decrypt){
var message
try {
message = JSON.parse( decrypt.message )
} catch ( err ) {
seneca.log.debug('Cannot parse message', decrypt.message)
that.log.debug('Cannot parse message', decrypt.message)
return response( null, {err: true, msg: 'Received unexpected response: ' + decrypt.message} )
}

seneca.act( "role:'protocol', execute_command:'identify'",
that.act( "role:'protocol', execute_command:'identify'",
{
command: message.command
},
function(err, data){
if (err){
return response(err)
}
seneca.act( "role: 'crypt', encrypt: 'message'", {message: JSON.stringify(data)}, function(err, encrypt){
that.act( "role: 'crypt', encrypt: 'message'", {message: JSON.stringify(data)}, function(err, encrypt){
response(err, {response: encrypt.message})
})
}
Expand Down
9 changes: 5 additions & 4 deletions controller/protocol/v1/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ module.exports = function( options ) {
var protocol_version = seneca.export( 'constants/protocol_version' )

function command( args, response ) {
var that = this
var command = args.command
seneca.act( "role: 'crypt', decrypt: 'message'", {message: command}, function(err, decrypt){
that.act( "role: 'crypt', decrypt: 'message'", {message: command}, function(err, decrypt){

var message
try {
message = JSON.parse( decrypt.message )
} catch ( err ) {
seneca.log.debug("Cannot parse message", decrypt.message)
that.log.debug("Cannot parse message", decrypt.message)
return response( null, {err: true, msg: 'Received unexpected response: ' + decrypt.message} )
}

seneca.act( "role:'protocol_v1',generate:'response'",
that.act( "role:'protocol_v1',generate:'response'",
{
command: message.command
},
function(err, data){
if (err){
return response(err)
}
seneca.act( "role: 'crypt', encrypt: 'message'", {message: JSON.stringify(data)}, function(err, encrypt){
that.act( "role: 'crypt', encrypt: 'message'", {message: JSON.stringify(data)}, function(err, encrypt){
response(err, {response: encrypt.message})
})
}
Expand Down
2 changes: 2 additions & 0 deletions mite.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var fs = require( 'fs' )
module.exports = function( options ) {
var seneca = this;

options.key = options.key || '1233212##$#2131'

function loadModules( done ) {
processInjectedFolders( ['constants', 'controller', 'protocol', 'service'], done )
}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "mite",
"name": "seneca-mite",
"description": "Mite plugin for seneca-sentinel.",
"version": "0.0.1",
"keywords": [
Expand Down Expand Up @@ -32,7 +32,9 @@
"lodash": "^3.10.1",
"macaddress": "^0.2.8",
"node-uuid": "^1.4.3",
"seneca-crypt": "~0.0.1"
"seneca-auth": "0.4.0",
"seneca-crypt": "~0.0.1",
"seneca-user": "0.2.10"
},
"repository": {
"type": "git",
Expand Down
5 changes: 3 additions & 2 deletions protocol/base/identify.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ module.exports = function( options ) {
var error_codes = seneca.export( 'protocol_v1/error_codes' )

function execute_identify( args, done ) {
var that = this

seneca.log.debug('Received command identify')
that.log.debug('Received command identify')

seneca.act( "role: 'mite', create:'auth_token'", function( err, data ) {
that.act( "role: 'mite', create:'auth_token'", function( err, data ) {
if( err ) {
return done( null, {
authorize: {
Expand Down
4 changes: 3 additions & 1 deletion protocol/v1/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module.exports = function( options ) {
var seneca = this;

function execute_configuration( args, done ) {
var that = this

seneca.log.debug( 'Command configuration', args.command )
var cmd = args.command

var command_payload = cmd.payload
seneca.act( "role: 'mite', set:'configuration'", { configuration: command_payload}, done )
that.act( "role: 'mite', set:'configuration'", { configuration: command_payload}, done )
}

seneca
Expand Down
18 changes: 12 additions & 6 deletions protocol/v1/get_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ module.exports = function( options ) {


function execute_get_status( args, done ) {
seneca.log.debug( 'Command get_status', args.command )
var that = this

seneca.act( "role: 'mite', get: 'os_status'", function( err, system ) {
that.log.debug( 'Command get_status', args.command )

that.act( "role: 'mite', get: 'os_status'", function( err, system ) {
if( err ) {
return done( err, {ok: false} )
}
var payload = {
os: system
}

seneca.act( "role: 'status', get: 'seneca'", function( err, stats ) {
that.act( "role: 'status', get: 'seneca'", function( err, stats ) {
if( err ) {
return done( err, {ok: false} )
}

payload.seneca_stats = stats

seneca.act( "role: 'status', get: 'web'", function( err, webstats ) {
that.act( "role: 'status', get: 'web'", function( err, webstats ) {
if( err ) {
return done( err, {ok: false} )
}
Expand All @@ -38,15 +40,19 @@ module.exports = function( options ) {


function cmd_stats( done ) {
seneca.act( 'role:seneca,stats:true', function( err, senstats ) {
var that = this

that.act( 'role:seneca,stats:true', function( err, senstats ) {
senstats.date = new Date()
done(err, senstats)
} )
}


function cmd_webstats( done ) {
seneca.act( 'role:web,stats:true', function( err, webstats ) {
var that = this

that.act( 'role:web,stats:true', function( err, webstats ) {
webstats.date = new Date()
done(err, webstats)
} )
Expand Down
8 changes: 5 additions & 3 deletions protocol/v1/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module.exports = function( options ) {
var seneca = this;

function generate_response( args, done ) {
var that = this

var cmd = args.command
var error_codes = seneca.export( 'protocol_v1/error_codes' )

Expand All @@ -28,7 +30,7 @@ module.exports = function( options ) {
)
}

seneca.act( "role: 'mite', get:'auth_token'", function( err, data ) {
that.act( "role: 'mite', get:'auth_token'", function( err, data ) {
if( err ) {
return done(
null,
Expand All @@ -50,7 +52,7 @@ module.exports = function( options ) {
}

var token = data.token
seneca.act( "role: 'mite', authorize:'command'", cmd, function( err, auth ) {
that.act( "role: 'mite', authorize:'command'", cmd, function( err, auth ) {
if( err || !auth.ok ) {
return done(
null,
Expand All @@ -72,7 +74,7 @@ module.exports = function( options ) {
}

// execute command
seneca.act(
that.act(
"protocol:1,execute_command:'" + cmd.command.type + "'",
{
command: cmd,
Expand Down
5 changes: 3 additions & 2 deletions service/monitor/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = function () {
}

function set_configuration( args, done ) {

if ( !args.configuration.max_samples || !args.configuration.rate_interval ) {
return done( null, {ok: false, msg: 'Cannot start monitoring, invalid configuration data'} )
}
Expand Down Expand Up @@ -49,7 +48,9 @@ module.exports = function () {


function monitorOS() {
seneca.act( "role: 'status', get: 'os'", function ( err, statusData ) {
var that = this

that.act( "role: 'status', get: 'os'", function ( err, statusData ) {
if ( err ) {
seneca.log.debug( 'Error receiving os status', err )
return
Expand Down
4 changes: 3 additions & 1 deletion service/status/seneca.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module.exports = function( options ) {
var seneca = this;

function cmd_stats( args, done ) {
seneca.act( 'role:seneca,stats:true', function( err, senstats ) {
var that = this

that.act( 'role:seneca,stats:true', function( err, senstats ) {
senstats.date = new Date()
done(err, senstats)
} )
Expand Down
4 changes: 3 additions & 1 deletion service/status/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module.exports = function( options ) {


function cmd_webstats( args, done ) {
seneca.act( 'role:web,stats:true', function( err, webstats ) {
var that = this

that.act( 'role:web,stats:true', function( err, webstats ) {
webstats.date = new Date()
done(err, webstats)
} )
Expand Down

0 comments on commit c9d71a8

Please sign in to comment.