~thopiekar/zypper/libzypp-manual-import

« back to all changes in this revision

Viewing changes to devel/devel.ma/PluginTest.cc

  • Committer: Thomas-Karl Pietrowski
  • Date: 2014-01-29 22:44:28 UTC
  • Revision ID: thopiekar@googlemail.com-20140129224428-gpcqnsdakby362n8
firstĀ import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "Tools.h"
 
2
 
 
3
#include <signal.h>
 
4
#include <errno.h>
 
5
#include <unistd.h>
 
6
#include <sys/wait.h>
 
7
#include <fcntl.h>
 
8
#include <iostream>
 
9
#include <stdlib.h>
 
10
#include <string.h>
 
11
 
 
12
#include <zypp/base/PtrTypes.h>
 
13
#include <zypp/base/Exception.h>
 
14
#include <zypp/base/Gettext.h>
 
15
#include <zypp/base/LogTools.h>
 
16
#include <zypp/base/Debug.h>
 
17
#include <zypp/base/Functional.h>
 
18
#include <zypp/base/IOStream.h>
 
19
#include <zypp/base/InputStream.h>
 
20
#include <zypp/base/ProvideNumericId.h>
 
21
#include <zypp/base/Flags.h>
 
22
#include <zypp/AutoDispose.h>
 
23
 
 
24
#include <zypp/PluginScript.h>
 
25
#include <zypp/PathInfo.h>
 
26
 
 
27
 
 
28
using namespace std;
 
29
using namespace zypp;
 
30
using namespace zypp::functor;
 
31
using namespace zypp::ui;
 
32
 
 
33
///////////////////////////////////////////////////////////////////
 
34
 
 
35
static const Pathname sysRoot( getenv("SYSROOT") ? getenv("SYSROOT") : "/Local/ROOT" );
 
36
 
 
37
///////////////////////////////////////////////////////////////////
 
38
namespace zypp
 
39
{ /////////////////////////////////////////////////////////////////
 
40
 
 
41
static PluginScript scr( "/bin/cat" );
 
42
 
 
43
void repeat( const PluginFrame & f )
 
44
{
 
45
  if ( ! scr.isOpen() )
 
46
    scr.open();
 
47
  MIL << "--> " << f << endl;
 
48
  scr.send( f );
 
49
  PluginFrame r( scr.receive() );
 
50
  MIL << "<-- " << r << endl;
 
51
  if ( r != f )
 
52
    ERR << "send/receive does not match." << endl;
 
53
}
 
54
 
 
55
void send( const PluginFrame & f )
 
56
{
 
57
  if ( ! scr.isOpen() )
 
58
    scr.open();
 
59
  MIL << "--> " << f << endl;
 
60
  scr.send( f );
 
61
}
 
62
 
 
63
 
 
64
PluginFrame receive()
 
65
{
 
66
  if ( ! scr.isOpen() )
 
67
    scr.open();
 
68
  PluginFrame r( scr.receive() );
 
69
  MIL << "<-- " << r << endl;
 
70
  return r;
 
71
}
 
72
 
 
73
  /////////////////////////////////////////////////////////////////
 
74
} // namespace zypp
 
75
///////////////////////////////////////////////////////////////////
 
76
 
 
77
#define DOLOG(C) USR << #C << ": " << endl; C;
 
78
 
 
79
namespace zypp {
 
80
  namespace target {
 
81
    void testCommitPlugins( const Pathname & path_r );
 
82
  }
 
83
}
 
84
 
 
85
/******************************************************************
 
86
**
 
87
**      FUNCTION NAME : main
 
88
**      FUNCTION TYPE : int
 
89
*/
 
90
int main( int argc, char * argv[] )
 
91
try {
 
92
  --argc,++argv;
 
93
  zypp::base::LogControl::instance().logToStdErr();
 
94
  INT << "===[START]==========================================" << endl;
 
95
  //////////////////////////////////////////////////////////////////
 
96
 
 
97
  zypp::target::testCommitPlugins( "/tmp/pltest" );
 
98
 
 
99
  if ( 0 )
 
100
  {
 
101
  Pathname script( "PluginTest.py" );
 
102
  PluginScript plugin( script );
 
103
  USR << plugin << endl;
 
104
 
 
105
  DOLOG( plugin.open() );
 
106
 
 
107
  DOLOG( plugin.send( PluginFrame( "PLUGINBEGIN" ) ) );
 
108
 
 
109
  PluginFrame ret;
 
110
  DOLOG( ret = plugin.receive() );
 
111
  MIL << ret << endl;
 
112
 
 
113
  DOLOG( plugin.send( PluginFrame( "PLUGINEND" ) ) );
 
114
  DOLOG( ret = plugin.receive() );
 
115
  MIL << ret << endl;
 
116
 
 
117
  DOLOG( plugin.close() );
 
118
  }
 
119
 
 
120
  if ( 0 ) {
 
121
    Pathname script( ZConfig::instance().pluginsPath()/"system/spacewalkx" );
 
122
    if ( PathInfo( script ).isX() )
 
123
      try {
 
124
        PluginScript spacewalk( script );
 
125
        spacewalk.open();
 
126
 
 
127
        PluginFrame notify( "PACKAGESETCHANGED" );
 
128
        spacewalk.send( notify );
 
129
 
 
130
        PluginFrame ret( spacewalk.receive() );
 
131
        MIL << ret << endl;
 
132
        if ( ret.command() == "ERROR" )
 
133
          ret.writeTo( WAR ) << endl;
 
134
      }
 
135
      catch ( const Exception & excpt )
 
136
      {
 
137
        WAR << excpt.asUserHistory() << endl;
 
138
      }
 
139
  }
 
140
 
 
141
  if ( 0 ) {
 
142
    Measure x( "" );
 
143
    PluginFrame f( "a" );
 
144
    f.setBody( std::string( 1020, '0' ) );
 
145
    if ( ! scr.isOpen() )
 
146
      scr.open();
 
147
    for ( unsigned i = 1; true; ++i )
 
148
    {
 
149
      try {
 
150
        MIL << "Receiving " << i << endl;
 
151
        PluginFrame ret( receive() );
 
152
      }
 
153
      catch ( const PluginScriptTimeout & excpt )
 
154
      {
 
155
        ERR << excpt << endl;
 
156
        scr.send( f );
 
157
      }
 
158
      catch ( const PluginScriptDiedUnexpectedly & excpt )
 
159
      {
 
160
        ERR << excpt << endl;
 
161
        ERR << scr << endl;
 
162
        scr.close();
 
163
        break;
 
164
      }
 
165
    }
 
166
  }
 
167
 
 
168
  if ( 0 ) {
 
169
    Measure x( "" );
 
170
    PluginFrame f( "a" );
 
171
    f.setBody( std::string( 10200, '0' ) );
 
172
    for ( unsigned i = 1; true; ++i )
 
173
    {
 
174
      try {
 
175
        MIL << "Sending " << i << endl;
 
176
        send( f );
 
177
      }
 
178
      catch ( const PluginScriptTimeout & excpt )
 
179
      {
 
180
        ERR << excpt << endl;
 
181
        ::kill( scr.getPid(), SIGKILL);
 
182
      }
 
183
      catch ( const PluginScriptDiedUnexpectedly & excpt )
 
184
      {
 
185
        ERR << excpt << endl;
 
186
        ERR << scr << endl;
 
187
        scr.close();
 
188
        break;
 
189
      }
 
190
    }
 
191
  }
 
192
 
 
193
  //////////////////////////////////////////////////////////////////
 
194
  INT << "===[END]============================================" << endl << endl;
 
195
  zypp::base::LogControl::instance().logNothing();
 
196
  return 0;
 
197
}
 
198
catch ( const Exception & exp )
 
199
{
 
200
  INT << exp << endl << exp.historyAsString();
 
201
  throw;
 
202
}
 
203
catch (...)
 
204
{
 
205
  throw;
 
206
}
 
207
 
 
208