Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Tag="hdata"
- /* STACK IMPLEMENTATION */
- local insert=table.insert
- local remove=table.remove
- local Empty=table.Empty
- local setmetatable=setmetatable
- local class = {}
- local mt = {__index = class}
- local function FIFO()
- return setmetatable( {} , mt )
- end
- local function LILO()
- return setmetatable( {lilo=true} , mt )
- end
- -- Push
- function mt.__add(a,b)
- insert( a , b )
- return a
- end
- -- Pop
- function mt:__unm()
- return remove( self , self.lilo and #self or 1 )
- end
- -- Pop
- function class:pop()
- return remove( self , self.lilo and #self or 1 )
- end
- function class.push(a,b)
- insert( a , b )
- return a
- end
- function class:len()
- return #self
- end
- function class:length()
- return #self
- end
- function class:clear()
- return Empty( self )
- end
- /* END OF STACK IMPLEMENTATION */
- /* THINK IMPLEMENTATION */
- local funcs={}
- function funcs:Add(obj,value)
- self[obj]=value or true
- end
- function funcs:Remove(obj)
- self[obj]=nil
- end
- function funcs:Think()
- local done=true
- for k,v in pairs(self) do
- done=false
- local bOk, strReturn = pcall( k,v )
- if !bOk then
- ErrorNoHalt( "Think failure:"+strReturn )
- end
- end
- if done then
- --print("Removing thinking")
- hook.Remove('Think',Tag)
- end
- end
- local meta={ __mode = 'k', __index=funcs }
- function meta:__newindex(k,v)
- rawset(self,k,v)
- if not hook:GetTable().Think[Tag] then
- --print("Adding think")
- hook.Add('Think',Tag,function() self:Think() end)
- end
- end
- thinks = {}
- setmetatable(thinks, meta )
- /*
- thinks:Add(function(self) Msg(self) end,"!")
- local afunc=function(self) Msg(self) end
- thinks:Add(afunc,"!")
- thinks:Remove(afunc,"_")
- do return end
- */
- /* END OF THINK IMPLEMENTATION */
- /* HTTP QUERY QUEUE IMPLEMENTATION */
- local queryqueue={}
- local meta={}
- local object={}
- meta.__index = object
- meta.name="Query Queue"
- function meta:__call()
- return self:__init()
- end
- function meta:__tostring()
- return "< "..meta.name..">"
- end
- function object:test(callback)
- http.Get(self.def_host,"",function(_,b)
- callback(b>0)
- end)
- end
- function object:GetProcess()
- return self.__proc
- end
- function object:SetProcess(proc)
- self.__proc = proc
- end
- function object:Process(proc)
- local http = proc.http
- if not http then
- print("processing new HTTPGet",proc)
- http = HTTPGet()
- proc.http = http
- http:Download( self:gethost(), proc[1] )
- proc.started=SysTime()
- end
- if http:Finished() then
- self:SetProcess( nil )
- local buf = http:GetBuffer()
- local len = http:DownloadSize()
- local bOk, strReturn = pcall( proc[2], buf, len )
- if ( !bOk ) then
- ErrorNoHalt( "Hyperdata callback error: "+strReturn+"\n" )
- end
- return true
- end
- end
- function object:Think()
- local proc=self:GetProcess()
- if proc then
- self:Process(proc)
- return
- end
- if self:GetProcess() then return end
- local newdata=self.stack:pop()
- print("grabbing new process",newdata)
- if newdata then
- self:SetProcess( newdata )
- return
- end
- print("stopped thinking",self)
- thinks:Remove(self.Think)
- end
- function object:Wake()
- if #self.stack:len() > 0 then
- thinks:Add(self.Think,self)
- end
- end
- function object:query(...)
- local data = {...}
- self.stack:push(data)
- self:Wake()
- end
- function object:gethost()
- return self.def_host
- end
- function object:sethost(host)
- self.def_host = host
- end
- function object:__init(host)
- local tbl={}
- self:sethost(host or "http://gmod.iriz.org:20090")
- tbl.stack=FIFO()
- setmetatable(tbl,meta)
- return tbl
- end
- setmetatable(queryqueue,meta)
- /* END OF HTTP QUERY QUEUE IMPLEMENTATION */
Advertisement
Add Comment
Please, Sign In to add comment