Changes between Version 15 and Version 16 of BossaExampleOne


Ignore:
Timestamp:
Jul 20, 2008, 8:01:58 PM (16 years ago)
Author:
davea
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • BossaExampleOne

    v15 v16  
    7070== Displaying jobs ==
    7171
    72 Next we'll explain the script that displays a job to a volunteer
    73 and handles their response: [source:/trunk/boinc/html/user/bossa_example.php bossa_example_display.php].
     72The application is described by a PHP file ([source:/trunk/boinc/html/inc/bossa_example.inchtml/inc/bossa_example.inc])
     73that contains several functions.
     74
     75The first of these displays a job:
    7476{{{
    75 function show_job($bj, $bji) {
    76     $info = json_decode($bj->info);
    77     $img_url = $info->url;
     77function job_show($job, $inst, $user) {
     78    $info = $job->get_info($job);
     79    $path = $info->path;
     80    page_head("Find the Ellipse");
    7881    echo "
    79         <form method=get action='bossa_example_display.php'>
     82        <form method=get action=bossa_job_finished.php>
    8083        Click on the center of the ellipse.
    8184        If you don't see one, click here:
    82         <br><br><input type=submit name=submit value=None>
    83         <input type=hidden name=bji value=$bji->id>
    84         <input type=hidden name=completion value=1>
    85         <input type=image name=pic src='$img_url'>
     85        <input type=submit name=submit value=None>
     86        <br><br>
     87        <input type=hidden name=bji value=$inst->id>
     88        <input type=image name=pic src=$path>
    8689        </form>
    8790    ";
     91    page_tail();
    8892}
     93}}}
    8994
    90 function handle_job_completion($bj, $bji) {
     95The second is called when a job is issued:
     96
     97{{{
     98function job_issued($job, $inst, $user) {
     99    $job->set_priority(0);
     100}
     101}}}
     102
     103It sets the job's priority to zero so that no further instances will be issued
     104(unless this one times) out.
     105
     106The next function is called when a job instance is finished.
     107It gets the user's response to the job,
     108and stores it in the instance's app data field.
     109{{{
     110function job_finished($job, $inst) {
    91111    $response = null;
    92112    if (get_str('submit', true)) {
     
    94114    } else {
    95115        $response->have_ellipse = 1;
    96         $response->cx = get_int('pic.x');
    97         $response->cy = get_int('pic.y');
     116        $response->cx = get_int('pic_x');
     117        $response->cy = get_int('pic_y');
    98118    }
    99     $bji->info = json_encode($response);
    100     $bji->completed($bj);
    101     Bossa::show_next_job($bj);    // show another job immediately
    102 }
    103 
    104 Bossa::script_init($user, $bj, $bji);
    105 
    106 if (isset($_GET['completion'])) {
    107     handle_job_completion($bj, $bji);
    108 } else {
    109     show_job($bj, $bji);
     119    $inst->update_info($response);
    110120}
    111121}}}
    112122
    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:
     123The next function is called when a job times out.
     124It restores the priority to one so that the job will
     125be issued to another volunteer.
    134126{{{
    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'");
     127function job_timed_out($job, $inst, $user) {
     128    $job->set_priority(1);
    172129}
    173130}}}
    174131
    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.
     132The remaining functions return short strings
     133that show the app data for jobs, instances, and users
     134on the Admin page:
     135{{{
     136function job_summary($job) {
     137    $info = $job->get_info();
     138    return "<a href=".URL_BASE."$info->path>View image</a>";
     139}
     140
     141function 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
     150function show_user_summary($user) {
     151}
     152}}}