1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PluginScript.h
12 #ifndef ZYPP_PLUGINSCRIPT_H
13 #define ZYPP_PLUGINSCRIPT_H
19 #include "zypp/base/PtrTypes.h"
20 #include "zypp/Pathname.h"
22 #include "zypp/PluginFrame.h"
23 #include "zypp/PluginScriptException.h"
25 ///////////////////////////////////////////////////////////////////
27 { /////////////////////////////////////////////////////////////////
30 * \brief Interface to pluigin scripts using a \c Stomp inspired communication protocol.
32 * \note \ref PluginScript is copyable and assignable, but the connection is shared
33 * among multiple copies. It gets automatically closed if the last copy goes out of
36 * Timeout when sending/receiving data to/from a plugin default to 30 sec. The value
37 * (in seconds) my be changed via the environment variables \c ZYPP_PLUGIN_SEND_TIMEOUT,
38 * \c ZYPP_PLUGIN_RECEIVE_TIMEOUT or \c ZYPP_PLUGIN_TIMEOUT (both: send and receive).
41 * // Setup comnnection to plugin script
43 * PluginScript::Arguments args;
44 * args.push_back( "-v" );
45 * scr.open( "/soem/testplugin", args );
47 * // send frame to plugin
48 * PluginFrame f( "COMMAND" );
49 * f.setHeader( "key", "value" );
50 * f.setBody( "some\ndata" );
53 * // receive frame from plugin
54 * PluginFrame r( scr.receive() );
56 * // explicitly close or let PluginScript go out of scope
60 * \see http://stomp.codehaus.org/
64 friend std::ostream & operator<<( std::ostream & str, const PluginScript & obj );
67 /** Commandline arguments passed to a script on \ref open. */
68 typedef std::vector<std::string> Arguments;
70 /** \c pid_t(-1) constant indicating no connection. */
71 static const pid_t NotConnected;
77 /** Ctor taking script path and no arguments. */
78 PluginScript( const Pathname & script_r );
80 /** Ctor taking script path and script arguments. */
81 PluginScript( const Pathname & script_r, const Arguments & args_r );
84 /** Return the script path if set. */
85 const Pathname & script() const;
87 /** Return the script arguments if set. */
88 const Arguments & args() const;
90 /** Whether we are connected to a script. */
93 /** Return a connected scripts pid or \ref NotConnected. */
96 /** Remembers a scripts return value after \ref close until next \ref open. */
97 int lastReturn() const;
99 /** Remembers a scripts execError string after \ref close until next \ref open.
100 * \see \ref ExternalProgram::execError.
102 const std::string & lastExecError() const;
105 /** Setup connection and execute script.
106 * \throw PluginScriptException if already connected to a script
107 * \throw PluginScriptException if script does not exist or is not executable
108 * \throw PluginScriptException on error
112 /** \overload taking script path and no arguments. */
113 void open( const Pathname & script_r );
115 /** \overload taking script path and script arguments. */
116 void open( const Pathname & script_r, const Arguments & args_r );
118 /** Close any open connection. */
122 /** Send a \ref PluginFrame.
123 * \throw PluginScriptNotConnected
124 * \throw PluginScriptSendTimeout
125 * \throw PluginScriptDiedUnexpectedly (does not \ref close)
126 * \throw PluginScriptException on error
129 void send( const PluginFrame & frame_r ) const;
131 /** Receive a \ref PluginFrame.
132 * \throw PluginScriptNotConnected
133 * \throw PluginScriptReceiveTimeout
134 * \throw PluginScriptDiedUnexpectedly (does not \ref close)
135 * \throw PluginScriptException on error
137 PluginFrame receive() const;
140 /** Implementation. */
143 /** Pointer to implementation. */
144 RW_pointer<Impl> _pimpl;
147 /** \relates PluginScript Stream output */
148 std::ostream & operator<<( std::ostream & str, const PluginScript & obj );
150 /////////////////////////////////////////////////////////////////
152 ///////////////////////////////////////////////////////////////////
153 #endif // ZYPP_PLUGINSCRIPT_H