Parallelise http transfers for trip planner tester
[busui.git] / lib / rolling-curl / .svn / text-base / RollingCurlGroup.php.svn-base
maxious 1 <?php
2 /*
3
4 Authored by Fabian Franz (www.lionsad.de)
5 Released under Apache License 2.0
6
7 $Id$
8 */
9
10 class RollingCurlGroupException extends Exception {}
11
12 /**
13 * @throws RollingCurlGroupException
14 */
15 abstract class RollingCurlGroupRequest extends RollingCurlRequest {
16 private $group = null;
17
18 /**
19 * Set group for this request
20 *
21 * @param group The group to be set
22 */
23 function setGroup($group) {
24 if (!($group instanceof RollingCurlGroup))
25 throw new RollingCurlGroupException("setGroup: group needs to be of instance RollingCurlGroup");
26
27 $this->group = $group;
28 }
29
30 /**
31 * Process the request
32 *
33 *
34 */
35 function process($output, $info) {
36 if ($this->group)
37 $this->group->process($output, $info, $this);
38 }
39
40 /**
41 * @return void
42 */
43 public function __destruct() {
44 unset($this->group);
45 parent::__destruct();
46 }
47
48 }
49
50 /**
51 * A group of curl requests.
52 *
53 * @throws RollingCurlGroupException *
54 */
55 class RollingCurlGroup {
56 /**
57 * @var string group name
58 */
59 protected $name;
60
61 /**
62 * @var int total number of requests in a group
63 */
64 protected $num_requests = 0;
65
66 /**
67 * @var int total number of finished requests in a group
68 */
69 protected $finished_requests = 0;
70
71 /**
72 * @var array requests array
73 */
74 private $requests = array();
75
76 /**
77 * @param string $name group name
78 * @return void
79 */
80 function __construct($name) {
81 $this->name = $name;
82 }
83
84 /**
85 * @return void
86 */
87 public function __destruct() {
88 unset($this->name, $this->num_requests, $this->finished_requests, $this->requests);
89 }
90
91 /**
92 * Adds request to a group
93 *
94 * @throws RollingCurlGroupException
95 * @param RollingCurlGroupRequest|array $request
96 * @return bool
97 */
98 function add($request) {
99 if ($request instanceof RollingCurlGroupRequest) {
100 $request->setGroup($this);
101 $this->num_requests++;
102 $this->requests[] = $request;
103 }
104 else if (is_array($request)) {
105 foreach ($request as $req)
106 $this->add($req);
107 }
108 else
109 throw new RollingCurlGroupException("add: Request needs to be of instance RollingCurlGroupRequest");
110
111 return true;
112 }
113
114 /**
115 * @throws RollingCurlGroupException
116 * @param RollingCurl $rc
117 * @return bool
118 */
119 function addToRC(RollingCurl $rc){
120 $ret = true;
121
122 while (count($this->requests) > 0){
123 $ret1 = $rc->add(array_shift($this->requests));
124 if (!$ret1)
125 $ret = false;
126 }
127
128 return $ret;
129 }
130
131 /**
132 * Override to implement custom response processing.
133 *
134 * Don't forget to call parent::process().
135 *
136 * @param string $output received page body
137 * @param array $info holds various information about response such as HTTP response code, content type, time taken to make request etc.
138 * @param RollingCurlRequest $request request used
139 * @return void
140 */
141 function process($output, $info, $request) {
142 $this->finished_requests++;
143
144 if ($this->finished_requests >= $this->num_requests)
145 $this->finished();
146 }
147
148 /**
149 * Override to execute code after all requests in a group are processed.
150 *
151 * @return void
152 */
153 function finished() {
154 }
155
156 }
157
158 /**
159 * Group version of rolling curl
160 */
161 class GroupRollingCurl extends RollingCurl {
162
163 /**
164 * @var mixed common callback for all groups
165 */
166 private $group_callback = null;
167
168 /**
169 * @param string $output received page body
170 * @param array $info holds various information about response such as HTTP response code, content type, time taken to make request etc.
171 * @param RollingCurlRequest $request request used
172 * @return void
173 */
174 protected function process($output, $info, $request) {
175 if ($request instanceof RollingCurlGroupRequest)
176 $request->process($output, $info);
177
178 if (is_callable($this->group_callback))
179 call_user_func($this->group_callback, $output, $info, $request);
180 }
181
182 /**
183 * @param mixed $callback common callback for all groups
184 * @return void
185 */
186 function __construct($callback = null) {
187 $this->group_callback = $callback;
188
189 parent::__construct(array(&$this, "process"));
190 }
191
192 /**
193 * Adds a group to processing queue
194 *
195 * @param RollingCurlGroup|Request $request
196 * @return bool
197 */
198 public function add($request) {
199 if ($request instanceof RollingCurlGroup)
200 return $request->addToRC($this);
201 else
202 return parent::add($request);
203 }
204
205 /**
206 * Execute processing
207 *
208 * @param int $window_size Max number of simultaneous connections
209 * @return bool|string
210 */
211 public function execute($window_size = null) {
212 if (count($this->requests) == 0)
213 return false;
214
215 return parent::execute($window_size);
216 }
217 }
218