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