Skip to content

Commit

Permalink
Basic alarms channel mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
dpino committed Aug 24, 2017
1 parent 6cdb03c commit 87bf337
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/apps/config/alarm_codec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module(...,package.seeall)

local S = require("syscall")
local channel = require("apps.config.channel")
local ffi = require("ffi")

local alarm_names = { 'raise_alarm', 'clear_alarm' }
Expand Down Expand Up @@ -81,6 +83,36 @@ function decode(buf, len)
return { name, assert(alarms[name], name)(codec) }
end

---

local alarms_channel

function get_channel()
if alarms_channel then return alarms_channel end
local name = '/'..S.getpid()..'/alarms-follower-channel'
local success, value = pcall(channel.open, name)
if success then
alarms_channel = value
end
return alarms_channel
end

function raise_alarm (key, args)
local channel = get_channel()
if channel then
local buf, len = encode({'raise_alarm', {key, args}})
channel:put_message(buf, len)
end
end

function clear_alarm (key)
local channel = get_channel()
if channel then
local buf, len = encode({'clear_alarm', {key, args}})
channel:put_message(buf, len)
end
end

function selftest ()
print('selftest: apps.config.alarm_codec')
local lib = require("core.lib")
Expand Down
1 change: 1 addition & 0 deletions src/apps/config/follower.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Follower:new (conf)
ret.period = 1/conf.Hz
ret.next_time = app.now()
ret.channel = channel.create('config-follower-channel', 1e6)
ret.alarms_channel = channel.create('alarms-follower-channel', 1e6)
ret.pending_actions = {}
return ret
end
Expand Down
36 changes: 36 additions & 0 deletions src/apps/config/leader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ local app = require("core.app")
local shm = require("core.shm")
local app_graph = require("core.config")
local action_codec = require("apps.config.action_codec")
local alarm_codec = require("apps.config.alarm_codec")
local support = require("apps.config.support")
local channel = require("apps.config.channel")
local alarms = require("lib.yang.alarms")

Leader = {
config = {
Expand Down Expand Up @@ -723,6 +725,40 @@ function Leader:pull ()
self.next_time = app.now() + self.period
self:handle_calls_from_peers()
self:send_messages_to_followers()
self:receive_alarms_from_followers()
end

function Leader:receive_alarms_from_followers ()
for _,follower in ipairs(self.followers) do
self:receive_alarms_from_follower(follower)
end
end

function Leader:receive_alarms_from_follower (follower)
if not follower.alarms_channel then
local name = '/'..tostring(follower.pid)..'/alarms-follower-channel'
local success, channel = pcall(channel.open, name)
if not success then return end
follower.alarms_channel = channel
end
local channel = follower.alarms_channel
while true do
local buf, len = channel:peek_message()
if not buf then break end
local alarm = alarm_codec.decode(buf, len)
self:handle_alarm(follower, alarm)
channel:discard_message(len)
end
end

function Leader:handle_alarm (follower, alarm)
local fn, args = unpack(alarm)
if fn == 'raise_alarm' then
alarms.raise_alarm(unpack(args))
end
if fn == 'clear_alarm' then
alarms.clear_alarm(unpack(args))
end
end

function Leader:stop ()
Expand Down
7 changes: 7 additions & 0 deletions src/lib/yang/alarms.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module(..., package.seeall)

function raise_alarm (key, args)
print('raise_alarm')
print('key: '..key)
print('args: '..args)
end

0 comments on commit 87bf337

Please sign in to comment.