Author: titmuss
Date: Tue Jan 15 04:38:04 2008
New Revision: 1440

URL: http://svn.slimdevices.com?rev=1440&root=Jive&view=rev
Log:
Bug: N/A
Description:

Added coxpcall from Xavante project. This is allows coroutines to yield inside 
the 
pcall/xpcall call. Added an interface to Task to access these calls. By default 
Task:pcall() will return a full stack trace with the error.

Added error handling in Socket. The socket will be closed if a task errors 
while 
reading/writing from the Socket. This catches code errors in the network layer.

Added protection against application errors in RequestHttp. The source/sink 
protected 
from application errors using Task:pcall().


Added:
    trunk/jive/src/pkg/jive/share/jive/utils/coxpcall.lua
Modified:
    trunk/jive/src/pkg/jive/share/jive/net/NetworkThread.lua
    trunk/jive/src/pkg/jive/share/jive/net/RequestHttp.lua
    trunk/jive/src/pkg/jive/share/jive/net/Socket.lua
    trunk/jive/src/pkg/jive/share/jive/ui/Task.lua

Modified: trunk/jive/src/pkg/jive/share/jive/net/NetworkThread.lua
URL: 
http://svn.slimdevices.com/trunk/jive/src/pkg/jive/share/jive/net/NetworkThread.lua?rev=1440&root=Jive&r1=1439&r2=1440&view=diff
==============================================================================
--- trunk/jive/src/pkg/jive/share/jive/net/NetworkThread.lua (original)
+++ trunk/jive/src/pkg/jive/share/jive/net/NetworkThread.lua Tue Jan 15 
04:38:04 2008
@@ -193,7 +193,7 @@
 
                ok, err = pcall(_t_select, self, timeoutSecs)
                if not ok then
-                       log:warn("error in _t_select: " .. err)
+                       log:error("error in _t_select: " .. err)
                end
 
                _, timeout = Task:yield(true)

Modified: trunk/jive/src/pkg/jive/share/jive/net/RequestHttp.lua
URL: 
http://svn.slimdevices.com/trunk/jive/src/pkg/jive/share/jive/net/RequestHttp.lua?rev=1440&root=Jive&r1=1439&r2=1440&view=diff
==============================================================================
--- trunk/jive/src/pkg/jive/share/jive/net/RequestHttp.lua (original)
+++ trunk/jive/src/pkg/jive/share/jive/net/RequestHttp.lua Tue Jan 15 04:38:04 
2008
@@ -39,16 +39,36 @@
 
 
 -- stuff we use
-local _assert, tostring, type, pairs = _assert, tostring, type, pairs
-
-local oo    = require("loop.base")
-local url   = require("socket.url")
-local table = require("jive.utils.table")
-
-local log   = require("jive.utils.log").logger("net.http")
+local _assert, pairs, tostring, type = _assert, pairs, tostring, type
+
+local oo        = require("loop.base")
+local url       = require("socket.url")
+local table     = require("jive.utils.table")
+local Task      = require("jive.ui.Task")
+
+local log       = require("jive.utils.log").logger("net.http")
 
 -- our class
 module(..., oo.class)
+
+
+-- catch errors in a sink or source
+local function _makeSafe(sourceOrSink, errstr)
+       if sourceOrSink == nil then
+               return nil
+       end
+
+       return function(...)
+                      local status, chunk, sink = Task:pcall(sourceOrSink, ...)
+
+                      if not status then
+                              log:error(errstr, chunk)
+                              return nil, chunk
+                      end
+
+                      return chunk, sink
+              end
+end
 
 
 --[[
@@ -218,8 +238,7 @@
 -- returns the body source
 function t_getBodySource(self)
        --log:debug("RequestHttp:t_getBodySource()")
-
-       return self.t_httpRequest.src
+       return _makeSafe(self.t_httpRequest.src, "Body source:")
 end
 
 
@@ -284,8 +303,8 @@
 -- returns the sink mode
 function t_getResponseSink(self)
 --     log:debug("RequestHttp:t_getResponseSink()")
-       
-       return self.t_httpResponse.sink
+
+       return _makeSafe(self.t_httpResponse.sink, "Response sink:")
 end
 
 

Modified: trunk/jive/src/pkg/jive/share/jive/net/Socket.lua
URL: 
http://svn.slimdevices.com/trunk/jive/src/pkg/jive/share/jive/net/Socket.lua?rev=1440&root=Jive&r1=1439&r2=1440&view=diff
==============================================================================
--- trunk/jive/src/pkg/jive/share/jive/net/Socket.lua (original)
+++ trunk/jive/src/pkg/jive/share/jive/net/Socket.lua Tue Jan 15 04:38:04 2008
@@ -127,20 +127,25 @@
 
 -- Proxy functions for NetworkThread, for convenience of subclasses
 
+local function _taskError(self)
+       self:close("task error")
+end
+
+
 -- t_add/remove/read/write
 function t_addRead(self, pump, timeout)
        if not self.readPump then
                -- task to iterate over all read pumps
                local task = Task(tostring(self) .. "(R)",
-                                 nil,
-                                 function(_, networkErr)
+                                 self,
+                                 function(self, networkErr)
                                          while self.readPump do
                                                  if not 
self.readPump(networkErr) then
-                                                         _, networkErr = 
Task:yield(false)
+                                                         self, networkErr = 
Task:yield(false)
                                                  end
                                          end
                                  end,
-                                 nil, -- FIXME err function
+                                 _taskError,
                                  self.priority)
                self.jnt:t_addRead(self.t_sock, task, timeout)
        end
@@ -159,15 +164,15 @@
        if not self.writePump then
                -- task to iterate over all write pumps
                local task = Task(tostring(self) .. "(W)",
-                                 nil,
-                                 function(_, networkErr)
+                                 self,
+                                 function(self, networkErr)
                                          while self.writePump do
                                                  if not 
self.writePump(networkErr) then
-                                                         networkErr = 
Task:yield(false)
+                                                         self, networkErr = 
Task:yield(false)
                                                  end
                                          end
                                  end,
-                                 nil, -- FIXME err function
+                                 _taskError,
                                  self.priority)
                self.jnt:t_addWrite(self.t_sock, task, timeout)
        end

Modified: trunk/jive/src/pkg/jive/share/jive/ui/Task.lua
URL: 
http://svn.slimdevices.com/trunk/jive/src/pkg/jive/share/jive/ui/Task.lua?rev=1440&root=Jive&r1=1439&r2=1440&view=diff
==============================================================================
--- trunk/jive/src/pkg/jive/share/jive/ui/Task.lua (original)
+++ trunk/jive/src/pkg/jive/share/jive/ui/Task.lua Tue Jan 15 04:38:04 2008
@@ -7,6 +7,7 @@
 local oo               = require("loop.base")
 local debug            = require("jive.utils.debug")
 local table            = require("jive.utils.table")
+local coxpcall         = require("jive.utils.coxpcall")
 
 local log              = require("jive.utils.log").logger("ui.task")
 
@@ -160,6 +161,16 @@
 end
 
 
+function pcall(class, f, ...)
+       return coxpcall.coxpcall(f, debug.traceback, ...)
+end
+
+
+function xpcall(class, ...)
+       return coxpcall.coxpcall(...)
+end
+
+
 function running(class)
        return taskRunning
 end

Added: trunk/jive/src/pkg/jive/share/jive/utils/coxpcall.lua
URL: 
http://svn.slimdevices.com/trunk/jive/src/pkg/jive/share/jive/utils/coxpcall.lua?rev=1440&root=Jive&view=auto
==============================================================================
--- trunk/jive/src/pkg/jive/share/jive/utils/coxpcall.lua (added)
+++ trunk/jive/src/pkg/jive/share/jive/utils/coxpcall.lua Tue Jan 15 04:38:04 
2008
@@ -1,0 +1,60 @@
+-------------------------------------------------------------------------------
+-- Coroutine safe xpcall and pcall versions
+--
+-- Encapsulates the protected calls with a coroutine based loop, so errors can
+-- be dealed without the usual Lua 5.0 pcall/xpcall issues with coroutines
+-- yielding inside the call to pcall or xpcall.
+--
+-- Authors: Roberto Ierusalimschy and Andre Carregal 
+-- Contributors: Thomas Harning Jr., Ignacio BurgueƱo 
+--
+-- Copyright 2005-2007 - Kepler Project (www.keplerproject.org)
+--
+-- $Id: coxpcall.lua,v 1.11 2007/12/14 20:34:46 mascarenhas Exp $
+-------------------------------------------------------------------------------
+
+local oldpcall, oldxpcall, unpack = pcall, xpcall, unpack
+local coroutine = require("coroutine")
+
+module(...)
+
+-------------------------------------------------------------------------------
+-- Implements xpcall with coroutines
+-------------------------------------------------------------------------------
+local performResume, handleReturnValue
+
+function handleReturnValue(err, co, status, ...)
+    if not status then
+        return false, err(...)
+    end
+    if coroutine.status(co) == 'suspended' then
+        return performResume(err, co, coroutine.yield(...))
+    else
+        return true, ...
+    end
+end
+
+function performResume(err, co, ...)
+    return handleReturnValue(err, co, coroutine.resume(co, ...))
+end    
+
+function coxpcall(f, err, ...)
+    local res, co = oldpcall(coroutine.create, f)
+    if not res then
+        local params = {...}
+        local newf = function() return f(unpack(params)) end
+        co = coroutine.create(newf)
+    end
+    return performResume(err, co, ...)
+end
+
+local function id(...)
+  return ...
+end
+
+-------------------------------------------------------------------------------
+-- Implements pcall with coroutines
+-------------------------------------------------------------------------------
+function copcall(f, ...)
+    return coxpcall(f, id, ...)
+end

_______________________________________________
Jive-checkins mailing list
[email protected]
http://lists.slimdevices.com/cgi-bin/mailman/listinfo/jive-checkins

Reply via email to