annotate Comm.lua @ 86:22b37c800bc4

Database and comm versioning
author John@Yosemite-PC
date Sun, 15 Apr 2012 14:42:14 -0400
parents 9e5b0a2368ad
children 5ade79caeece
rev   line source
John@72 1 local bsk=bsk
John@72 2 local _G=_G
John@72 3 local table=table
John@72 4 local pairs=pairs
John@72 5 local setmetatable=setmetatable
John@72 6 local ipairs=ipairs
John@72 7 local unpack=unpack
John@72 8 local string=string
John@72 9 local sformat=string.format
John@72 10 local tostring=tostring
John@72 11 local type=type
John@72 12 local getn=getn
John@72 13
John@72 14 local commlib = LibStub("AceComm-3.0") or _G.error("Couldn't load up AceComm")
John@72 15 local s = LibStub("AceSerializer-3.0") or _G.error("Couldn't load up AceSerializer")
John@72 16
John@72 17 setfenv(1,bsk)
John@72 18
John@72 19 local function BuildPacket(handler,message)
John@72 20 local p = {handler,message}
John@72 21 local str = s:Serialize({handler,message})
John@72 22 print("sending",str)
John@72 23 return p,str
John@72 24 end
John@72 25
John@72 26 local function SendMessage(str)
John@72 27 --commlib:SendCommMessage("BSKADDON",str,"GUILD")
John@86 28 commlib:SendCommMessage("BSKADDON"..commversion,str,"RAID")
John@72 29 end
John@72 30
John@72 31 local function Send(handler,message)
John@72 32 local p,str = BuildPacket(handler,message)
John@72 33 SendMessage(str)
John@72 34 end
John@72 35
John@72 36 -- todo: ActivateList and AddReserve -> state
John@72 37 Comm =
John@72 38 {
John@72 39 ["SS"] = function(self,packet,sender,isloop)
John@72 40 print("isloop",isloop)
John@72 41 if not isloop then DispatchState(packet) end
John@72 42 end,
John@72 43 ["SendStateChange"] = function(self,...)
John@72 44 local p,str = BuildPacket("SS",{...})
John@72 45 DispatchState(p[2])
John@72 46 SendMessage(str)
John@72 47 end,
John@72 48 ["AR"] = function(self,packet,sender,isloop)
John@72 49 if isloop then return end
John@72 50 PersonList:AddReserve(packet)
John@72 51 changeListener:DataEvent()
John@72 52 end,
John@72 53 ["AddReserve"] = function(self,packet)
John@72 54 if changeListener then
John@72 55 changeListener:DataEvent(change)
John@72 56 end
John@72 57 Send("AR",packet)
John@72 58 end,
John@72 59 ["SendChange"] = function(self,change)
John@72 60 if changeListener then
John@72 61 changeListener:DataEvent(change)
John@72 62 end
John@72 63 Send("CC",change)
John@72 64 end,
John@72 65 ["CC"] = function(self,change,sender,isloop)
John@72 66 if isloop then return end
John@72 67 ProcessChange(change)
John@72 68 changeListener:DataEvent()
John@72 69 end,
John@72 70 ["Push"] = function(self)
John@72 71 Send("PU",{db.profile.lists,db.profile.persons,db.profile.changes})
John@72 72 end,
John@72 73 ["PU"] = function(self,packet,sender,isloop)
John@72 74 if isloop then return end
John@72 75 db.profile.lists,db.profile.persons,db.profile.changes = unpack(packet)
John@72 76 CreateWorkingStateFromChanges(db.profile.changes)
John@72 77 if changeListener then
John@72 78 changeListener:DataEvent()
John@72 79 end
John@72 80 end,
John@72 81 }
John@72 82
John@72 83 local function OnCommReceived(prefix, message, distribution, sender)
John@72 84 print("Received on", distribution)
John@72 85 local success,packet = s:Deserialize(message)
John@72 86 local isloop = _G.UnitName("player") == sender
John@72 87
John@72 88 print("received",message)
John@72 89
John@72 90 Comm[packet[1]](Comm,packet[2],sender,isloop)
John@72 91 end
John@72 92
John@86 93 alertlist = {}
John@86 94 local function OnOlderCommReceived(prefix, message, distribution, sender)
John@86 95 if not alertlist[sender] then
John@86 96 printf("Received communication from %s, who is using an older version of the addon; ignoring",sender)
John@86 97 alertlist[sender]=true
John@86 98 end
John@86 99 end
John@86 100
John@86 101 local function OnNewerCommReceived(prefix, message, distribution, sender)
John@86 102 if not alertlist[sender] then
John@86 103 printf("Received communication from %s, who is using a newer version of the addon; ignoring",sender)
John@86 104 alertlist[sender]=true
John@86 105 end
John@86 106 end
John@86 107
John@72 108 function InitializeComm()
John@86 109 for i = 0,commversion-1 do
John@86 110 commlib:RegisterComm("BSKADDON"..i,OnOlderCommReceived)
John@86 111 end
John@86 112 commlib:RegisterComm("BSKADDON"..commversion,OnCommReceived)
John@86 113 for i = commversion+1,commversion+5 do -- some sensible number
John@86 114 commlib:RegisterComm("BSKADDON"..i,OnNewerCommReceived)
John@86 115 end
John@72 116 end
John@72 117
John@72 118 function DeinitializeComm()
John@72 119
John@72 120 end