add api for changing PluginScript timeouts
authorMichael Andres <ma@suse.de>
Fri, 3 Jun 2011 09:12:10 +0000 (11:12 +0200)
committerMichael Andres <ma@suse.de>
Fri, 3 Jun 2011 09:12:10 +0000 (11:12 +0200)
tests/zypp/PluginFrame_test.cc
zypp/PluginScript.cc
zypp/PluginScript.h

index 2f0862d..c64cf43 100644 (file)
@@ -7,6 +7,11 @@
 #include "TestSetup.h"
 #include "zypp/PluginScript.h"
 
+BOOST_AUTO_TEST_CASE(InitialSettings)
+{
+  PluginScript::defaultTimeout( 3 );
+}
+
 BOOST_AUTO_TEST_CASE(PluginFrameDefaultCtor)
 {
   PluginFrame f;
index 4ffc230..f012f22 100644 (file)
@@ -31,13 +31,9 @@ using std::endl;
 namespace zypp
 { /////////////////////////////////////////////////////////////////
 
-
   namespace
   {
     const char * PLUGIN_DEBUG = getenv( "ZYPP_PLUGIN_DEBUG" );
-    const long PLUGIN_TIMEOUT = str::strtonum<long>( getenv( "ZYPP_PLUGIN_TIMEOUT" ) );
-    const long PLUGIN_SEND_TIMEOUT = str::strtonum<long>( getenv( "ZYPP_PLUGIN_SEND_TIMEOUT" ) );
-    const long PLUGIN_RECEIVE_TIMEOUT = str::strtonum<long>( getenv( "ZYPP_PLUGIN_RECEIVE_TIMEOUT" ) );
 
     /** Dump buffer string if PLUGIN_DEBUG is on.
      * \ingroup g_RAII
@@ -63,7 +59,7 @@ namespace zypp
       const std::string & _buffer;
     };
 
-    /** Dump buffer string if PLUGIN_DEBUG is on.
+    /** Dump programms stderr.
      * \ingroup g_RAII
      */
     struct PluginDumpStderr
@@ -114,7 +110,9 @@ namespace zypp
   {
     public:
       Impl( const Pathname & script_r = Pathname(), const Arguments & args_r = Arguments() )
-        : _script( script_r )
+        : _sendTimeout( _defaultSendTimeout )
+        , _receiveTimeout( _defaultReceiveTimeout )
+        , _script( script_r )
         , _args( args_r )
       {}
 
@@ -122,17 +120,18 @@ namespace zypp
       { try { close(); } catch(...) {} }
 
     public:
-      /** Timeout (sec.) when sending data. */
-      static const long send_timeout;
-      /** Timeout (sec.) when receiving data. */
-      static const long receive_timeout;
+      static long _defaultSendTimeout;
+      static long _defaultReceiveTimeout;
 
-    public:
+      long _sendTimeout;
+      long _receiveTimeout;
+
+   public:
       const Pathname & script() const
       { return _script; }
 
       const Arguments & args() const
-      { return _args; }
+   { return _args; }
 
       pid_t getPid() const
       { return _cmd ? _cmd->getpid() : NotConnected; }
@@ -173,10 +172,17 @@ namespace zypp
 
   ///////////////////////////////////////////////////////////////////
 
-  const long PluginScript::Impl::send_timeout = ( PLUGIN_SEND_TIMEOUT > 0 ? PLUGIN_SEND_TIMEOUT
-                                                                         : ( PLUGIN_TIMEOUT > 0 ? PLUGIN_TIMEOUT : 30 ) );
-  const long PluginScript::Impl::receive_timeout = ( PLUGIN_RECEIVE_TIMEOUT > 0 ? PLUGIN_RECEIVE_TIMEOUT
-                                                                                : ( PLUGIN_TIMEOUT > 0 ? PLUGIN_TIMEOUT : 30 ) );
+  namespace
+  {
+    const long PLUGIN_TIMEOUT =        str::strtonum<long>( getenv( "ZYPP_PLUGIN_TIMEOUT" ) );
+    const long PLUGIN_SEND_TIMEOUT =   str::strtonum<long>( getenv( "ZYPP_PLUGIN_SEND_TIMEOUT" ) );
+    const long PLUGIN_RECEIVE_TIMEOUT =        str::strtonum<long>( getenv( "ZYPP_PLUGIN_RECEIVE_TIMEOUT" ) );
+  }
+
+  long PluginScript::Impl::_defaultSendTimeout =    ( PLUGIN_SEND_TIMEOUT > 0    ? PLUGIN_SEND_TIMEOUT
+                                                                                : ( PLUGIN_TIMEOUT > 0 ? PLUGIN_TIMEOUT : 30 ) );
+  long PluginScript::Impl::_defaultReceiveTimeout = ( PLUGIN_RECEIVE_TIMEOUT > 0 ? PLUGIN_RECEIVE_TIMEOUT
+                                                                                : ( PLUGIN_TIMEOUT > 0 ? PLUGIN_TIMEOUT : 30 ) );
 
   ///////////////////////////////////////////////////////////////////
 
@@ -219,6 +225,9 @@ namespace zypp
     if ( _cmd )
     {
       DBG << "Close:" << *this << endl;
+      {
+       PluginDumpStderr _dump( *_cmd ); // dump scripts stderr before leaving
+      }
       _cmd->kill();
       _lastReturn = _cmd->close();
       _lastExecError = _cmd->execError();
@@ -272,7 +281,7 @@ namespace zypp
        FD_SET( fd, &wfds );
 
        struct timeval tv;
-       tv.tv_sec = send_timeout;
+       tv.tv_sec = _sendTimeout;
        tv.tv_usec = 0;
 
        int retval = select( fd+1, NULL, &wfds, NULL, &tv );
@@ -364,7 +373,7 @@ namespace zypp
            FD_SET( fd, &rfds );
 
            struct timeval tv;
-           tv.tv_sec = receive_timeout;
+           tv.tv_sec = _receiveTimeout;
            tv.tv_usec = 0;
 
            int retval = select( fd+1, &rfds, NULL, NULL, &tv );
@@ -409,6 +418,30 @@ namespace zypp
 
   const pid_t PluginScript::NotConnected( -1 );
 
+  long PluginScript::defaultSendTimeout()
+  { return Impl::_defaultSendTimeout; }
+
+  long PluginScript::defaultReceiveTimeout()
+  { return Impl::_defaultReceiveTimeout; }
+
+  void PluginScript::defaultSendTimeout( long newval_r )
+  { Impl::_defaultSendTimeout = newval_r > 0 ? newval_r : 0; }
+
+  void PluginScript::defaultReceiveTimeout( long newval_r )
+  { Impl::_defaultReceiveTimeout = newval_r > 0 ? newval_r : 0; }
+
+  long PluginScript::sendTimeout() const
+  { return _pimpl->_sendTimeout; }
+
+  long PluginScript::receiveTimeout() const
+  { return _pimpl->_receiveTimeout; }
+
+  void PluginScript::sendTimeout( long newval_r )
+  { _pimpl->_sendTimeout = newval_r > 0 ? newval_r : 0; }
+
+  void PluginScript::receiveTimeout( long newval_r )
+  { _pimpl->_receiveTimeout = newval_r > 0 ? newval_r : 0; }
+
   PluginScript::PluginScript()
     : _pimpl( new Impl )
   {}
index 588b7da..2f1e4d9 100644 (file)
@@ -71,6 +71,30 @@ namespace zypp
       static const pid_t NotConnected;
 
     public:
+      /** \name Get/set the global timeout settings.
+       * Timeout when sending/receiving data to/from a plugin default to 30 sec. The value
+       * (in seconds) my be changed via the environment variables \c ZYPP_PLUGIN_SEND_TIMEOUT,
+       * \c ZYPP_PLUGIN_RECEIVE_TIMEOUT or \c ZYPP_PLUGIN_TIMEOUT (both: send and receive).
+       */
+      //@{
+       /** Global default timeout (sec.) when sending data. */
+       static long defaultSendTimeout();
+
+       /** Global default timeout (sec.) when receiving data. */
+       static long defaultReceiveTimeout();
+
+       /** Set global default timeout (sec.) when sending data. */
+       static void defaultSendTimeout( long newval_r );
+
+       /** Set global default timeout (sec.) when receiving data. */
+       static void defaultReceiveTimeout( long newval_r );
+
+       /** Set global default timeout (sec.) (both: send and receive).*/
+       static void defaultTimeout( long newval_r )
+       { defaultSendTimeout( newval_r ); defaultReceiveTimeout( newval_r ); }
+      //@}
+
+    public:
       /** Default ctor. */
       PluginScript();
 
@@ -101,7 +125,27 @@ namespace zypp
        */
       const std::string & lastExecError() const;
 
-    public:
+     public:
+      /** \name Get/set local timeout settings. */
+      //@{
+       /** Local default timeout (sec.) when sending data. */
+       long sendTimeout() const;
+
+       /** Local default timeout (sec.) when receiving data. */
+       long receiveTimeout() const;
+
+       /** Set local default timeout (sec.) when sending data. */
+       void sendTimeout( long newval_r );
+
+       /** Set local default timeout (sec.) when receiving data. */
+       void receiveTimeout( long newval_r );
+
+       /** Set local default timeout (sec.) (both: send and receive).*/
+       void timeout( long newval_r )
+       { sendTimeout( newval_r ); receiveTimeout( newval_r ); }
+      //@}
+
+   public:
       /** Setup connection and execute script.
        * \throw PluginScriptException if already connected to a script
        * \throw PluginScriptException if script does not exist or is not executable