Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Tag="hdata"
- local setmetatable=setmetatable
- local class do -- http://lua-users.org/wiki/SimpleLuaClasses
- -- class.lua
- -- Compatible with Lua 5.1 (not 5.0).
- class = function(base, init)
- local c = {} -- a new class instance
- if not init and type(base) == 'function' then
- init = base
- base = nil
- elseif type(base) == 'table' then
- -- our new class is a shallow copy of the base class!
- for i,v in pairs(base) do
- c[i] = v
- end
- c._base = base
- end
- -- the class will be the metatable for all its objects,
- -- and they will look up their methods in it.
- c.__index = c
- -- expose a constructor which can be called by <classname>(<args>)
- local mt = {}
- mt.__call = function(class_tbl, ...)
- local obj = {}
- setmetatable(obj,c)
- if init then
- init(obj,...)
- else
- -- make sure that any stuff from the base class is initialized!
- if base and base.init then
- base.init(obj, ...)
- end
- end
- return obj
- end
- c.init = init
- c.is_a = function(self, klass)
- local m = getmetatable(self)
- while m do
- if m == klass then return true end
- m = m._base
- end
- return false
- end
- setmetatable(c, mt)
- return c
- end
- end -- http://lua-users.org/wiki/SimpleLuaClasses
- --TEST
- local _class = class(function(a,name)
- a.name = name
- end)
- --_class.__newindex=function(a,b,c) print("asserttest","newindex",type(a),type(b),type(c)) rawset(a,b,c) end
- _class.__tostring=function(s)return s.name end
- local name="test"
- local _=_class(name)
- assert(tostring(_)==name)
- _.testkey="testvalue"
- _=nil
- --ETEST
- local insert=table.insert
- local remove=table.remove
- local Empty=table.Empty
- -------------------------------------------------------------
- local FIFO do -- STACK IMPLEMENTATION
- FIFO=class()
- function FIFO:__tostring() return "< Stack "..tostring(self:len()).." >" end
- -- Pop
- function FIFO:pop()
- return remove( self , self.lilo and #self or 1 )
- end
- -- push
- function FIFO.push(a,b)
- insert( a , b )
- return a
- end
- function FIFO:len()
- return #self
- end
- function FIFO:length()
- return #self
- end
- function FIFO:clear()
- return Empty( self )
- end
- end -- STACK IMPLEMENTATION
- --TEST
- local _=FIFO()
- assert(_:push("a")==_)
- assert(_:push("b")==_)
- assert(_:pop()=="a")
- print("asserttest",_)
- assert(_:pop()=="b")
- assert(!_:pop())
- _=nil
- --ETEST
- local thinks do -- THINK IMPLEMENTATION
- thinks=class()
- meta.name="Think Queue"
- function thinks:__tostring() return "< ".."Thinks".." - "..table.Count(self).." thinks >" end
- function thinks:Add(obj,thinkfunc)
- self[obj]=thinkfunc or true
- if not hook:GetTable().Think[Tag] then
- print("Adding think")
- hook.Add('Think',Tag,function() self:Think() end)
- end
- end
- function thinks:Remove(obj)
- if self[obj] then
- self[obj]=nil
- return true
- end
- return false
- end
- function thinks:Think()
- local done=true
- for obj,thinkfunc in pairs(self) do
- done=false
- local bOk, strReturn = pcall( thinkfunc,obj )
- if !bOk then
- ErrorNoHalt( "Think failure:"+strReturn )
- end
- end
- if done then
- print("Removing thinking")
- hook.Remove('Think',Tag)
- end
- end
- end -- THINK IMPLEMENTATION
- --TEST
- local _=thinks()
- local boo=false
- _:Add("!",function() boo=true _:Remove("!") end)
- _:Add("_",function() end)
- _:Think()_:Think()_:Think()_:Think()_:Think()
- assert(boo==true)
- print("asserttest",_)
- assert(_:Remove("_")==true)
- print("asserttest",_)
- _:Think()
- --ETEST
- local thinks=thinks()
- local HTTPQueryObject do -- HTTP QUERY OBJECT HELPERS
- HTTPQueryObject=class()
- function HTTPQueryObject:__tostring()
- return "< ".."HTTPQueryObject".." - Timeout "..tostring(self:GetTimeout() or 0).." - Started "..tostring(self:GetStarted()).." >"
- end
- AccessorFunc(HTTPQueryObject,"__headers","Header",FORCE_STRING)
- AccessorFunc(HTTPQueryObject,"__callback","Callback")
- AccessorFunc(HTTPQueryObject,"__timeout","Timeout",FORCE_NUMBER)
- AccessorFunc(HTTPQueryObject,"__started","Started",FORCE_BOOL)
- end -- HTTP QUERY OBJECT HELPERS
- --TEST
- local _=HTTPQueryObject()
- _:SetStarted(true)
- assert(_:GetStarted()==true)
- print("asserttest",_)
- assert(tostring(_)!="")
- _=nil
- --ETEST
- local queryqueue do -- QUERY QUEUE IMPLEMENTATION
- queryqueue = class(function( tbl, host )
- local http = tbl:GetHTTPGet()
- assert(http:Finished()==false)
- tbl:__sethost(host or "http://gmod.iriz.org:20090")
- tbl.stack=FIFO()
- return tbl
- end)
- function queryqueue:__tostring()
- return "< ".."Query Object".." >"
- end
- function queryqueue:GetProcess()
- return self.__proc
- end
- function queryqueue:SetProcess(proc)
- self.__proc = proc
- end
- function queryqueue:GetHTTPGet(new)
- if new or not self.__HTTPGet then
- self.__HTTPGet=HTTPGet()
- end
- return self.__HTTPGet
- end
- -- not finished if not even started!!
- function queryqueue:HTTPIsFinished()
- return self:GetHTTPGet():Finished()
- end
- function queryqueue:GetCurrentData()
- local buf = self:GetHTTPGet():GetBuffer()
- local len = self:GetHTTPGet():DownloadSize()
- return buf,len
- end
- function queryqueue:Download(host,header)
- if header and header:len() > 0 then -- hack
- header="Accept: */*\n"..header
- else
- header = ""
- end
- self:GetHTTPGet():Download(host,header)
- end
- function queryqueue:Process(proc)
- local started = proc:GetStarted()
- if not started then
- proc:SetStarted(true)
- self:GetHTTPGet(true) -- We need to reset it :\
- print("Started processing new",proc,"to",self:gethost())
- --proc:GetTimeout()
- self:Download( self:gethost(), proc:GetHeader())
- end
- if proc:GetStarted() and self:HTTPIsFinished() then
- local proc = self:GetProcess()
- self:SetProcess( nil )
- local callback = proc:GetCallback()
- if not callback then
- print("processed",proc,"without callbacks?")
- return
- end
- local buf,len=self:GetCurrentData()
- local ok, ret = pcall( callback, buf, len )
- if not ok then
- ErrorNoHalt( meta.name.." callback error: "+ret+"\n" )
- end
- end
- end
- function queryqueue:Think()
- local proc=self:GetProcess()
- if proc then
- self:Process(proc)
- return
- end
- if self:GetProcess() then return end
- local newdata=self.stack:pop()
- if newdata then
- --print("grabbing new process",newdata)
- self:SetProcess( newdata )
- return
- end
- print("stopped thinking",self)
- thinks:Remove(self)
- end
- function queryqueue:Wake()
- if self.stack:len() > 0 then
- print("waking up")
- thinks:Add(self,self.Think)
- end
- end
- function queryqueue:query(header,callback,timeout)
- local proc = HTTPQueryObject()
- if header then
- proc:SetHeader(header)
- end
- if callback then
- proc:SetCallback(callback)
- end
- if timeout then
- proc:SetTimeout(timeout)
- end
- self.stack:push(proc)
- self:Wake()
- end
- function queryqueue:gethost()
- return self.def_host
- end
- function queryqueue:__sethost(host)
- self.def_host = host
- end
- function queryqueue:GetQueueLength()
- return self.stack:len()
- end
- end -- QUERY QUEUE IMPLEMENTATION
- --TEST
- local host="http://iriz.org"
- _G._=queryqueue(host)
- local _=_G._
- assert(_:HTTPIsFinished()==false)
- _:query(nil,function(a,b)print("callback:",a:len()) assert(a:len()>0) end,0)
- _:query(nil,function(a,b)print("callback:",a:len()) assert(a:len()>0) end,0)
- _:query(nil,function(a,b)print("callback:",a:len()) assert(a:len()>0) end,0)
- assert(_:GetQueueLength()==3)
- print("host:",_:gethost())
- assert(_:gethost()==host)
- do return end
- --ETEST
- local hyperdata do -- HYPERDATA IMPLEMENTATION
- local meta = { name = "Hyperdata" ,__index={},__call = function(self) return self.__init and self:__init() end}
- hyperdata = setmetatable({},meta)
- function meta:__tostring()
- return "< "..meta.name.." - "..tostring(self:gethost()).." >"
- end
- local object=meta.__index
- -- "export"
- _G[Tag]=hyperdata
- function object:gethost()
- return self.httpqueue:gethost()
- end
- function object:test(callback)
- http.Get(self:gethost(),"",function(_,b)
- callback(b>0)
- end)
- end
- function object:GetQueryQueue()
- return self.httpqueue
- end
- function object:__init(host)
- local tbl=setmetatable({},meta)
- tbl.httpqueue=queryqueue(host or "http://gmod.iriz.org:20090") or "HURR??"
- tbl.derp="herp"
- print(tbl.httpqueue)
- print("New",self)
- return tbl
- end
- end -- HYPERDATA IMPLEMENTATION
- _G.asd=hyperdata()
- print("created",_G.asd)
- do return end
- local a=hdata()
- print(a)
- a:test(function(...)print("res",...)end)
- do return end
- def_host="http://gmod.iriz.org:20090"
- function Create(host)
- local data={}
- data.host=host or def_host
- return data
- end
- function Request(data,req,values,callback)
- local payload="Accept: */*\n"
- payload=payload.."type: "..req
- if values then
- for k,v in pairs(values) do
- local str="\n"..k..": "..v
- payload=payload..str
- end
- end
- http.Get(data.host,payload,function(resp,b)
- -- empty response, server error?
- if b==0 then
- callback(false)
- return
- end
- local pos=resp:find("\n",1,true)
- if not pos then
- print("resp fail")
- callback(false,resp)
- return
- end
- local hyperdata=resp:sub(1,pos-1)
- local resp_data=resp:sub(pos+1,-1)
- local hyperdata_resp=hyperdata:match("H%: (.*)")
- print("hyperdata response: ",hyperdata_resp)
- if !hyperdata_resp then
- print("hyperdata transfer fail. Received:",resp)
- callback(false,resp)
- return
- end
- callback(resp_data)
- end)
- end
- local dat=Create()
Advertisement
Add Comment
Please, Sign In to add comment