Created
August 28, 2013 10:15
-
-
Save Mons/6364489 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if avd == nil then | |
avd = { version = 1.0 }; | |
else | |
avd.version = 1.0 | |
end | |
-- work with sessions user | |
function avd.session () | |
box.fiber.detach() | |
box.fiber.name("avd.session") | |
box.fiber.sleep(1) | |
while true do | |
local i = box.space[2].index[1] | |
local x,v = i:next_equal() | |
while x ~= nil do | |
box.fiber.testcancel() | |
if box.unpack('i',v[1]) < os.time() then | |
print('<',v[0],'> ', os.date('%d.%m.%Y %H:%M',box.unpack('i',v[1])), ' ==>',box.delete(2,v[0]) ) | |
end | |
x,v = i:next(x) | |
if x == nil then break end | |
end | |
box.fiber.sleep(60) | |
end | |
end | |
local sfiber = box.fiber.create(avd.session) | |
box.fiber.resume(sfiber) | |
-- work with resolving favicons | |
-- clear: T to R | |
--print('init: ', box.session.id()) | |
while true do | |
local x,v = box.space[0].index[1]:next_equal('T') | |
if v == nil then | |
break | |
else | |
print("update ",v[0]," from ",v[1]," to R") | |
local r,e = pcall(box.update,0,v[0],'=p',1,'R') | |
if (not r) then | |
print("pcall ",v[0]," failed: ",e) | |
box.fiber.sleep(0.01) | |
end | |
end | |
end | |
-- save current all T into session | |
avd.current = {} | |
function avd.watcher () | |
box.fiber.detach() | |
box.fiber.name("avd.watcher") | |
box.fiber.sleep(1) | |
while true do | |
-- print('1') | |
box.fiber.testcancel() | |
for sid,x in pairs(avd.current) do | |
-- print('avd.watcher: ',sid, " exists: ", box.session.exists(sid)) | |
if box.session.exists(sid) == 0 then | |
-- print('avd.clear: ',sid) | |
avd.clear(sid) | |
end | |
end | |
box.fiber.sleep(1) | |
end | |
end | |
local fiber = box.fiber.create(avd.watcher) | |
box.fiber.resume(fiber) | |
function avd.come(e) | |
local sid = box.session.id() | |
-- print('come: ', sid) | |
if avd.current[sid] == nil then | |
avd.current[sid] = {} | |
end | |
avd.current[sid][e] = 1 | |
end | |
function avd.gone (e,sid) | |
if sid == nil then | |
sid = box.session.id() | |
end | |
-- print('gone: ', sid ) | |
if avd.current[sid] ~= nil then | |
if avd.current[sid][e] ~= nil then | |
avd.current[sid][e] = nil | |
-- print('gone done: avd.current ',sid,' <',e,'> ') | |
else | |
-- print('gone err: avd.current ',sid,' <',e,'> is nil') | |
end | |
local cc = 0 | |
for x,y in pairs(avd.current[sid]) do | |
cc = cc + 1 | |
end | |
if cc == 0 then | |
avd.current[sid] = nil | |
end | |
else | |
-- print('gone err: avd.current ',sid,' is nil') | |
end | |
return 0 | |
end | |
function avd.take(s) | |
local sid = box.session.id() | |
-- print('take: ', sid) | |
local x,o = box.space[0].index[1]:next_equal('R',s) | |
-- print('taken: ', x,' - ',o) | |
if o == nil then | |
-- print('return nil') | |
return | |
else | |
-- print('return update') | |
avd.come(o[0]) | |
return box.update(0,o[0],'=p',1,'T') | |
end | |
end | |
function avd.clear(sid) | |
if avd.current[sid] ~= nil then | |
-- look for T in all T into session | |
for e,x in pairs(avd.current[sid]) do | |
local v = box.select(0,0,e) | |
if v ~= nil and v[1] == 'T' then | |
avd.gone(v[0],sid) | |
local r,e = pcall(box.update,0,v[0],'=p',1,'R') | |
if (not r) then | |
print("pcall ",v[0]," failed: ",e) | |
end | |
-- local t = box.update(0,v[0],'=p',1,'R') | |
-- print('clr:upd: ', t[0],t[1]) | |
end | |
end | |
avd.current[sid] = nil | |
else | |
-- print('avd.current ',sid,' is nil') | |
end | |
end | |
-- clear on disconnect | |
box.session.on_connect( function() | |
print('client connected: id:',box.session.id(), '; remote:', box.session.peer()) | |
end) | |
box.session.on_disconnect( function() | |
print('client disconnected: id:',box.session.id(), '; remote:', box.session.peer()) | |
-- avd.clear(box.session.id()) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment