434019825aadebcb8dee0ffa3146f67f2d492772
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Task.pm
1 # $Id: Task.pm,v 1.7 2007-08-01 15:11:03 mike Exp $
2
3 package ZOOM::IRSpy::Task;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use Scalar::Util;
10
11 =head1 NAME
12
13 ZOOM::IRSpy::Task - base class for tasks in IRSpy
14
15 =head1 SYNOPSIS
16
17  use ZOOM::IRSpy::Task;
18  package ZOOM::IRSpy::Task::SomeTask;
19  our @ISA = qw(ZOOM::IRSpy::Task);
20  # ... override methods
21
22 =head1 DESCRIPTION
23
24 This class provides a base-class from which individual IRSpy task
25 classes can be derived.  For example, C<ZOOM::IRSpy::Task::Search>
26 will represent a searching task, carrying with it a query, a pointer
27 to a result-set, etc.
28
29 The base class provides nothing more exciting than a link to a
30 callback function to be called when the task is complete, and a
31 pointer to the next task to be performed after this.
32
33 =cut
34
35 sub new {
36     my $class = shift();
37     my($conn, $udata, $options, %cb) = @_;
38
39     my $this = bless {
40         irspy => $conn->{irspy},
41         conn => $conn,
42         udata => $udata,
43         options => $options,
44         cb => \%cb,
45         timeRegistered => time(),
46     }, $class;
47
48     #Scalar::Util::weaken($this->{irspy});
49     #Scalar::Util::weaken($this->{udata});
50
51     return $this;
52 }
53
54
55 sub irspy {
56     my $this = shift();
57     return $this->{irspy};
58 }
59
60 sub conn {
61     my $this = shift();
62     return $this->{conn};
63 }
64
65 sub udata {
66     my $this = shift();
67     return $this->{udata};
68 }
69
70 sub run {
71     my $this = shift();
72     die "can't run base-class task $this";
73 }
74
75 # In general, this sets the Connection's options to what is in the
76 # task's option hash and sets the option-hash entry to the
77 # Connection's old value of the option: this means that calling this
78 # twice restores the state to how it was before.  (That's not quite
79 # true as its impossible to unset an option that was previously set:
80 # such options are instead set to the empty string.)
81 #
82 # As a special case, options in the task's option-hash whose names
83 # begin with an asterisk are taken to be persistent: they are set into
84 # the Connection (with the leading hash removed) and deleted from the
85 # task's option-hash so that they will NOT be reset the next time this
86 # function is called.
87 #
88 sub set_options {
89     my $this = shift();
90
91     foreach my $key (sort keys %{ $this->{options} }) {
92         my $value = $this->{options}->{$key};
93         my $persistent = ($key =~ s/^\*//);
94         $value = "" if !defined $value;
95         $this->conn()->log("irspy_debug", "$this setting option '$key' -> ",
96                            defined $value ? "'$value'" : "undefined");
97         my $old = $this->conn()->option($key, $value);
98         if ($persistent) {
99             delete $this->{options}->{"*$key"}
100         } else {
101             $this->{options}->{$key} = $old;
102         }
103     }
104 }
105
106 sub render {
107     my $this = shift();
108     return "[base-class] " . ref($this);
109 }
110
111 use overload '""' => \&render;
112
113
114 =head1 SEE ALSO
115
116 ZOOM::IRSpy
117
118 =head1 AUTHOR
119
120 Mike Taylor, E<lt>mike@indexdata.comE<gt>
121
122 =head1 COPYRIGHT AND LICENSE
123
124 Copyright (C) 2006 by Index Data ApS.
125
126 This library is free software; you can redistribute it and/or modify
127 it under the same terms as Perl itself, either Perl version 5.8.7 or,
128 at your option, any later version of Perl 5 you may have available.
129
130 =cut
131
132 1;