90 | | function handle_job_completion($bj, $bji) { |
| 95 | The second is called when a job is issued: |
| 96 | |
| 97 | {{{ |
| 98 | function job_issued($job, $inst, $user) { |
| 99 | $job->set_priority(0); |
| 100 | } |
| 101 | }}} |
| 102 | |
| 103 | It sets the job's priority to zero so that no further instances will be issued |
| 104 | (unless this one times) out. |
| 105 | |
| 106 | The next function is called when a job instance is finished. |
| 107 | It gets the user's response to the job, |
| 108 | and stores it in the instance's app data field. |
| 109 | {{{ |
| 110 | function job_finished($job, $inst) { |
113 | | The script calls '''Bossa::script_init()''' to get PHP objects describing |
114 | | the user, the job, and the job instance. |
115 | | |
116 | | '''show_job()''' decodes the job description to get the image URL, |
117 | | and displays the image in an HTML page that |
118 | | lets the user click on the image or on a "No ellipse" button. |
119 | | |
120 | | '''handle_job_completion()''' gets the user's response (from form variables). |
121 | | It calls the '''completed()''' method of the job, |
122 | | passing it a JSON encoding of the response. |
123 | | I then calls '''Bossa::show_next_job()''' to immediately display another job. |
124 | | |
125 | | == Handling completed results == |
126 | | |
127 | | Finally, we need to specify how results are handled. |
128 | | This is specified in [source:/trunk/boinc/html/inc/bossa_example_backend.inc bossa_example_backend.inc]. |
129 | | |
130 | | This defines two functions, which must have names '''X_compare''' and '''X_handle''' |
131 | | where X is the application's short name. |
132 | | |
133 | | The first function compares two instances and decides if they are compatible: |
| 123 | The next function is called when a job times out. |
| 124 | It restores the priority to one so that the job will |
| 125 | be issued to another volunteer. |
135 | | function bossa_example_compare($r1, $r2) { |
136 | | if ($r1->have_ellipse) { |
137 | | if ($r2->have_ellipse) { |
138 | | $dx = ($r1->cx - $r2->cx); |
139 | | $dy = ($r1->cy - $r2->cy); |
140 | | $dsq = $dx*$dx + $dy*$dy; |
141 | | return ($dsq < 400); |
142 | | } else return false; |
143 | | } else { |
144 | | return !$r2->have_ellipse; |
145 | | } |
146 | | } |
147 | | }}} |
148 | | In this case, two instances are considered compatible if either |
149 | | * neither of them found an ellipse, or |
150 | | * they both found an ellipse and the centers are within 20 pixels |
151 | | |
152 | | The second function specifies what happens when a job has been completed, |
153 | | i.e. a consensus set has been found: |
154 | | {{{ |
155 | | function bossa_example_handle($bj, $c) { |
156 | | $res = $c[0]; |
157 | | if ($res->have_ellipse) { |
158 | | $res->cx = 0; |
159 | | $res->cy = 0; |
160 | | foreach ($c as $r) { |
161 | | $res->cx += $r->cx; |
162 | | $res->cy += $r->cy; |
163 | | } |
164 | | $res->cx /= count($c); |
165 | | $res->cy /= count($c); |
166 | | } |
167 | | |
168 | | $info = json_decode[$bj->info); |
169 | | $info->result = $res; |
170 | | $i = json_encode($info); |
171 | | $bj->update("info='$i'"); |
| 127 | function job_timed_out($job, $inst, $user) { |
| 128 | $job->set_priority(1); |
175 | | This function is called with the BossaJob record ($bj) and an |
176 | | array of job instances in the consensus set ($c). |
177 | | In this case, we average the center positions (if an ellipse was found) |
178 | | and store the JSON-encoded result in the '''info''' field of the job record. |
| 132 | The remaining functions return short strings |
| 133 | that show the app data for jobs, instances, and users |
| 134 | on the Admin page: |
| 135 | {{{ |
| 136 | function job_summary($job) { |
| 137 | $info = $job->get_info(); |
| 138 | return "<a href=".URL_BASE."$info->path>View image</a>"; |
| 139 | } |
| 140 | |
| 141 | function instance_summary($inst) { |
| 142 | $info = $inst->get_info(); |
| 143 | if ($info->have_ellipse) { |
| 144 | return "($info->cx, $info->cy)"; |
| 145 | } else { |
| 146 | return "no ellipse"; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | function show_user_summary($user) { |
| 151 | } |
| 152 | }}} |