Python1320

Http query queue thingy

Jul 21st, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1.  
  2. local Tag="hdata"
  3.  
  4.  
  5.  
  6. /*              STACK IMPLEMENTATION                */
  7. local insert=table.insert
  8. local remove=table.remove
  9. local Empty=table.Empty
  10. local setmetatable=setmetatable
  11.     local class = {}
  12.     local mt = {__index = class}
  13.  
  14.     local function FIFO()      
  15.         return setmetatable( {} , mt )
  16.     end
  17.  
  18.     local function LILO()
  19.         return setmetatable( {lilo=true} , mt )
  20.     end
  21.  
  22.  
  23.     -- Push
  24.     function mt.__add(a,b) 
  25.         insert( a , b )
  26.         return a
  27.     end
  28.  
  29.     -- Pop
  30.     function mt:__unm()
  31.         return remove( self , self.lilo and #self or 1 )
  32.     end
  33.  
  34.     -- Pop
  35.     function class:pop()
  36.         return remove( self , self.lilo and #self or 1 )
  37.     end
  38.  
  39.     function class.push(a,b)
  40.         insert( a , b )
  41.         return a
  42.     end
  43.  
  44.     function class:len()
  45.         return #self
  46.     end
  47.  
  48.     function class:length()
  49.         return #self
  50.     end
  51.  
  52.     function class:clear()
  53.         return Empty( self )
  54.     end
  55.    
  56. /*                  END OF STACK IMPLEMENTATION             */
  57.  
  58.  
  59.  
  60.  
  61.  
  62. /*                THINK IMPLEMENTATION                      */
  63. local funcs={}
  64. function funcs:Add(obj,value)
  65.     self[obj]=value or true
  66. end
  67. function funcs:Remove(obj)
  68.     self[obj]=nil
  69. end
  70.  
  71. function funcs:Think()
  72.     local done=true
  73.     for k,v in pairs(self) do
  74.         done=false
  75.         local bOk, strReturn = pcall( k,v )
  76.         if !bOk then
  77.             ErrorNoHalt( "Think failure:"+strReturn )
  78.         end
  79.     end
  80.     if done then
  81.         --print("Removing thinking")
  82.         hook.Remove('Think',Tag)
  83.     end
  84. end
  85.  
  86. local meta={ __mode = 'k', __index=funcs }
  87. function meta:__newindex(k,v)
  88.     rawset(self,k,v)
  89.     if not hook:GetTable().Think[Tag] then
  90.         --print("Adding think")
  91.         hook.Add('Think',Tag,function() self:Think() end)
  92.     end
  93. end
  94.  
  95. thinks = {}
  96. setmetatable(thinks, meta )
  97.  
  98.  
  99. /*
  100. thinks:Add(function(self) Msg(self) end,"!")
  101. local afunc=function(self) Msg(self) end
  102. thinks:Add(afunc,"!")
  103. thinks:Remove(afunc,"_")
  104.  
  105. do return end
  106.   */
  107.  
  108.  
  109. /*                END OF THINK IMPLEMENTATION                      */
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. /*              HTTP QUERY QUEUE IMPLEMENTATION             */
  118. local queryqueue={}
  119. local meta={}
  120. local object={}
  121.  
  122. meta.__index = object
  123. meta.name="Query Queue"
  124.  
  125. function meta:__call()
  126.     return self:__init()
  127. end
  128. function meta:__tostring() 
  129.     return "< "..meta.name..">"
  130. end
  131.  
  132. function object:test(callback)
  133.     http.Get(self.def_host,"",function(_,b)
  134.         callback(b>0)
  135.     end)
  136. end
  137.  
  138. function object:GetProcess()
  139.     return self.__proc
  140. end
  141. function object:SetProcess(proc)
  142.     self.__proc = proc
  143. end
  144.  
  145. function object:Process(proc)
  146.    
  147.     local http = proc.http
  148.     if not http then
  149.        
  150.         print("processing new HTTPGet",proc)
  151.         http = HTTPGet()
  152.         proc.http = http
  153.         http:Download( self:gethost(), proc[1] )
  154.         proc.started=SysTime()
  155.     end
  156.    
  157.    
  158.     if http:Finished() then
  159.         self:SetProcess( nil )
  160.        
  161.         local buf = http:GetBuffer()
  162.         local len = http:DownloadSize()
  163.        
  164.         local bOk, strReturn = pcall( proc[2], buf, len )
  165.         if ( !bOk ) then
  166.             ErrorNoHalt( "Hyperdata callback error: "+strReturn+"\n" )
  167.         end
  168.  
  169.         return true
  170.     end
  171.    
  172. end
  173.  
  174. function object:Think()
  175.     local proc=self:GetProcess()
  176.    
  177.     if proc then
  178.         self:Process(proc)
  179.         return
  180.     end
  181.     if self:GetProcess() then return end
  182.     local newdata=self.stack:pop()
  183.     print("grabbing new process",newdata)
  184.     if newdata then
  185.         self:SetProcess( newdata )
  186.        
  187.         return
  188.     end
  189.     print("stopped thinking",self)
  190.     thinks:Remove(self.Think)
  191. end
  192.  
  193. function object:Wake()
  194.     if #self.stack:len() > 0 then
  195.         thinks:Add(self.Think,self)
  196.     end
  197. end
  198.  
  199. function object:query(...)
  200.    
  201.     local data = {...}
  202.    
  203.     self.stack:push(data)
  204.     self:Wake()
  205. end
  206. function object:gethost()
  207.     return self.def_host
  208. end
  209. function object:sethost(host)
  210.     self.def_host = host
  211. end
  212.  
  213.  
  214. function object:__init(host)
  215.     local tbl={}
  216.     self:sethost(host or "http://gmod.iriz.org:20090")
  217.     tbl.stack=FIFO()
  218.    
  219.     setmetatable(tbl,meta)
  220.     return tbl
  221. end
  222.  
  223. setmetatable(queryqueue,meta)
  224. /*              END OF HTTP QUERY QUEUE IMPLEMENTATION              */
Advertisement
Add Comment
Please, Sign In to add comment