Many radical changes to the IRSpy engine, enabling a far more asynchronous approach...
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Connection.pm
1 # $Id: Connection.pm,v 1.1 2006-10-06 11:33:07 mike Exp $
2
3 package ZOOM::IRSpy::Connection;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use ZOOM;
10 our @ISA = qw(ZOOM::Connection);
11
12 use ZOOM::IRSpy::Task::Connect;
13 use ZOOM::IRSpy::Task::Search;
14
15
16 =head1 NAME
17
18 ZOOM::IRSpy::Connection - ZOOM::Connection subclass with IRSpy functionality
19
20 =head1 DESCRIPTION
21
22 This class provides some additional private data and methods that are
23 used by IRSpy but which would be useless in any other application.
24 Keeping the private data in these objects removes the need for ugly
25 mappings in the IRSpy object itself; adding the methods makes the
26 application code cleaner.
27
28 The constructor takes an additional first argument, a reference to the
29 IRSpy object that it is associated with.
30
31 =cut
32
33 sub new {
34     my $class = shift();
35     my $irspy = shift();
36
37     my $this = $class->SUPER::new(@_);
38     $this->{irspy} = $irspy;
39     $this->{record} = undef;
40     $this->{tasks} = undef;
41
42     return $this;
43 }
44
45
46 sub irspy {
47     my $this = shift();
48     return $this->{irspy};
49 }
50
51
52 sub record {
53     my $this = shift();
54     my($new) = @_;
55
56     my $old = $this->{record};
57     $this->{record} = $new if defined $new;
58     return $old;
59 }
60
61
62 sub tasks {
63     my $this = shift();
64     my($new) = @_;
65
66     my $old = $this->{tasks};
67     $this->{tasks} = $new if defined $new;
68     return $old;
69 }
70
71
72 sub current_task {
73     my $this = shift();
74     my($new) = @_;
75
76     my $old = $this->{current_task};
77     if (defined $new) {
78         $this->{current_task} = $new;
79         $this->log("irspy_debug", "set current task to $new");
80     }
81
82     return $old;
83 }
84
85
86 sub log {
87     my $this = shift();
88     my($level, @msg) = @_;
89
90     $this->irspy()->log($level, $this->option("host"), " ", @msg);
91 }
92
93
94 sub irspy_connect {
95     my $this = shift();
96     my(%cb) = @_;
97
98     $this->add_task(new ZOOM::IRSpy::Task::Connect($this, %cb));
99     $this->log("irspy", "registered connect()");
100 }
101
102
103 sub irspy_search_pqf {
104     my $this = shift();
105     my($query, %cb) = @_;
106
107     $this->add_task(new ZOOM::IRSpy::Task::Search($query, $this, %cb));
108     $this->log("irspy", "registered search_pqf($query)");
109 }
110
111
112 sub add_task {
113     my $this = shift();
114     my($task) = @_;
115
116     my $tasks = $this->tasks();
117     if (!defined $tasks) {
118         $this->tasks([ $task ]);
119     } else {
120         $tasks->[-1]->{next} = $task;
121         push @$tasks, $task;
122     }
123
124     $this->log("irspy", "added task $task");
125 }
126
127
128 sub start_task {
129     my $this = shift();
130     my($task) = @_;
131     die "no task defined for " . $this->option("host")
132         if !defined $task;
133
134     $this->current_task($task);
135     $task->run();
136 }
137
138
139 =head1 SEE ALSO
140
141 ZOOM::IRSpy
142
143 =head1 AUTHOR
144
145 Mike Taylor, E<lt>mike@indexdata.comE<gt>
146
147 =head1 COPYRIGHT AND LICENSE
148
149 Copyright (C) 2006 by Index Data ApS.
150
151 This library is free software; you can redistribute it and/or modify
152 it under the same terms as Perl itself, either Perl version 5.8.7 or,
153 at your option, any later version of Perl 5 you may have available.
154
155 =cut
156
157 1;