add G_base variable
[curl-de-sac.git] / webtest / cds_test01.php
1 #!/usr/bin/php
2 <?php
3
4 define('WEB_URL', 'http://localhost/curl-de-sac');
5 define('DBG_LEVEL', 0);
6
7 $G_base = "./";
8
9 require_once($G_base . 'Obj/curl-de-sac.phh');
10
11 class short_cmd extends CDS_cmd {
12     var $short_data;
13
14     function short_cmd($cmd_cls, $ch, $short_data)
15     {
16         parent::__construct($cmd_cls, $ch);
17         $this->short_data = $short_data;
18     }
19 }
20
21 class short_cmd_cls extends CDS_cmd_cls {
22     function short_cmd_cls()
23     {
24         parent::__construct("short", 10);
25     }
26
27     function create($cds, $url)
28     {
29         if ($cds->dbg_get() > 0) {
30             printf("'short'::create url:[%s]\n", $url);
31         }
32
33         do {
34             if (($ch = parent::pre_create($cds, $url)) == FALSE)
35                 break;
36
37             if (parent::create($cds, $ch) == FALSE)
38                 break;
39
40             $cmd = new short_cmd($this, $ch, "none currently");
41
42             return $cmd;
43         } while (FALSE);
44         
45         return FALSE;
46     }
47
48     function process($cmd, $ret)
49     {
50         if ($this->dbg_get() > 2) { printf("CURL: 'short' process: curl_multi_getcontent\n"); }
51
52         $content = curl_multi_getcontent($cmd->ch_get());
53         if ($this->dbg_get() > 0) { printf("'short' process: [%s]\n", $content); }
54
55         return TRUE;
56     }
57
58     function timeout($cmd)
59     {
60         printf("'Short' timeout function reached\n");
61     }
62 }
63
64 class long_cmd extends CDS_cmd {
65     var $long_data;
66
67     function long_cmd($cmd_cls, $ch, $long_data)
68     {
69         parent::__construct($cmd_cls, $ch);
70         $this->long_data = $long_data;
71     }
72 }
73
74 class long_cmd_cls extends CDS_cmd_cls {
75     function long_cmd_cls()
76     {
77         parent::__construct("long", 5);
78     }
79
80     function create($cds, $url)
81     {
82         if ($cds->dbg_get() > 0) {
83             printf("'long'::create url:[%s]\n", $url);
84         }
85
86         do {
87             if (($ch = parent::pre_create($cds, $url)) == FALSE)
88                 break;
89
90             if (parent::create($cds, $ch) == FALSE)
91                 break;
92
93             $cmd = new long_cmd($this, $ch, "none currently");
94
95             return $cmd;
96         } while (FALSE);
97         
98         return FALSE;
99     }
100
101     function process($cmd, $ret)
102     {
103         if ($this->dbg_get() > 2) { printf("CURL: 'long' process: curl_multi_getcontent\n"); }
104
105         $content = curl_multi_getcontent($cmd->ch_get());
106         if ($this->dbg_get() > 0) { printf("'long' process: [%s]\n", $content); }
107
108         return TRUE;
109     }
110
111     function timeout($cmd)
112     {
113         printf("'Long' timeout function reached\n");
114     }
115 }
116
117
118 function main()
119 {
120     $debug = DBG_LEVEL;
121     // create cds
122     $cds = new Curl_de_sac($debug);
123
124     // create short_cls
125     $short_cls = new short_cmd_cls();
126
127     // registrer short_cls
128     printf("MAIN: Register 'short_cls'\n");
129     if (($cds->cmd_cls_register($short_cls)) == FALSE) {
130         fprintf(STDERR, "MAIN: 'short_cls' registration failed\n");
131         exit(1);
132     }
133
134     // create long_cls
135     $long_cls = new long_cmd_cls();
136
137     // register long_cls
138     printf("MAIN: Register 'long_cls'\n");
139     if (($cds->cmd_cls_register($long_cls)) == FALSE) {
140         fprintf(STDERR, "MAIN: 'long_cls' registration failed\n");
141         exit(2);
142     }
143
144     // register long_cls (retry)
145     printf("MAIN: Re-register 'long_cls' (must go wrong)\n");
146     if (($cds->cmd_cls_register($long_cls)) != FALSE) {
147         fprintf(STDERR, "MAIN: 'long_cls' re-registration success\n");
148         exit(3);
149     }
150
151     printf("MAIN: CDS:\n");
152     if (($debug & 1) == 1)
153         print_r($cds);
154     printf("MAIN: Deregister 'long_cls'\n");
155     if (($cds->cmd_cls_deregister($long_cls)) == FALSE) {
156         fprintf(STDERR, "MAIN: 'long_cls' deregistration failed\n");
157         exit(4);
158     }
159     printf("MAIN:");
160     if (($debug & 1) == 1) {
161         printf(" CDS:\n");
162         print_r($cds);
163     }
164     printf("\n");
165     // re-re-register long_cls
166     printf("MAIN: Re-re-register 'long_cls'\n");
167     if (($cds->cmd_cls_register($long_cls)) == FALSE) {
168         fprintf(STDERR, "MAIN: 'long_cls' re-re-registration failed\n");
169         exit(5);
170     }
171
172     printf("MAIN: Deregister all\n");
173     $cds->cmd_cls_deregister_all();
174
175     // registrer short_cls
176     printf("MAIN: register 'short_cls'\n");
177     if (($cds->cmd_cls_register($short_cls, 10)) == FALSE) {
178         fprintf(STDERR, "MAIN: 'short_cls' registration failed\n");
179         exit(1);
180     }
181
182     // register long_cls
183     printf("MAIN: register 'long_cls'\n");
184     if (($cds->cmd_cls_register($long_cls, 4)) == FALSE) {
185         fprintf(STDERR, "MAIN: 'long_cls' registration failed\n");
186         exit(2);
187     }
188     printf("MAIN:");
189     if (($debug & 1) == 1) {
190         printf(" CDS:\n");
191         print_r($cds);
192     }
193     printf("\n");
194
195     // for ($i = -15 ; $i < 30 ; $i++) {
196     for ($i = 0 ; $i < 20 ; $i++) {
197         printf("MAIN: START ITERATION %d\n", $i);
198
199          if ($i == 2) {
200             printf("MAIN: load 'short'\n");
201             if ($cds->execute("short", WEB_URL.'/test/short.php') == FALSE) {
202                 printf("MAIN: push 'short' command failed\n");
203                 exit(123);
204             }
205         }
206
207          if ($i == 3) {
208             printf("MAIN: load 'short'\n");
209             if ($cds->execute("short", WEB_URL.'/test/short.php') == FALSE) {
210                 printf("MAIN: push 'short' command failed\n");
211                 exit(123);
212             }
213         }
214
215         if ($i == 4) {
216             printf("MAIN: load 'long'\n");
217             if ($cds->execute("long", WEB_URL.'/test/long.php') == FALSE) {
218                 printf("MAIN: push 'long' command failed\n");
219                 exit(123);
220             }
221         }
222
223         printf("MAIN:");
224         if (($debug & 1) == 1) {
225             printf(" CDS:\n");
226             print_r($cds);
227         }
228         printf("\n");
229
230         printf("MAIN: Call process\n");
231         $cds->process();
232         sleep(1);
233     }
234     printf("MAIN: finished, dump cds:\n");
235     print_r($cds);
236     // start loop
237     //   print status
238     //   if input data execute some command
239     //   if end => clean exit
240     exit(0);
241 }
242
243 main();
244
245 ?>