Perl Filter and Perl API
[idzebra-moved-to-github.git] / perl / lib / IDZebra / Service.pm
1 #!/usr/bin/perl
2 # ============================================================================
3 # Zebra perl API header
4 # =============================================================================
5 use strict;
6 use Carp;
7 # ============================================================================
8 package IDZebra::Service;
9 use IDZebra;
10 use IDZebra::Session;
11 use IDZebra::Logger qw(:flags :calls);
12 use Scalar::Util qw(weaken);
13
14 our @ISA = qw(IDZebra::Logger);
15
16 1;
17 # -----------------------------------------------------------------------------
18 # Class constructors, destructor
19 # -----------------------------------------------------------------------------
20 sub new {
21     my ($proto,$configName) = @_;
22     my $class = ref($proto) || $proto;
23     my $self = {};
24     $self->{configName} = $configName;
25     $self->{sessions} = {};
26     $self->{sessc} = 0;
27     bless ($self, $class);
28     return ($self);
29 }
30
31 sub start {
32     my ($proto,$configName) = @_;
33     my $self = {};
34     if (ref($proto)) { $self = $proto; } else { 
35         $self = $proto->new($configName);
36     }
37     unless (defined($self->{zs})) {
38         $self->{zs} = IDZebra::start($self->{configName});
39     }
40     return ($self);
41 }
42
43 sub stop {
44     my ($self) = @_;
45     foreach my $sess (values(%{$self->{sessions}})) {
46         $sess->close;
47     }
48     IDZebra::stop($self->{zs}) if ($self->{zs});    
49     $self->{zs} = undef;
50 }
51
52 sub DESTROY {
53     my ($self) = @_;
54     $self->stop; 
55 }
56 # -----------------------------------------------------------------------------
57 # Session management
58 # -----------------------------------------------------------------------------
59 sub createSession {
60     my ($self) = @_;
61     my $session = IDZebra::Session->new($self);
62     $self->{sessions}{$self->{sessc}} = $session;
63     weaken ($self->{sessions}{$self->{sessc}});
64     $self->{sessc}++;
65     return($session);
66 }
67
68 sub openSession {
69     my ($self) = @_;
70     my $session = IDZebra::Session->open($self);
71     $self->{sessions}{$self->{sessc}} = $session;
72     weaken ($self->{sessions}{$self->{sessc}});
73     $self->{sessc}++;
74     return($session);
75 }
76
77 __END__
78
79 =head1 NAME
80
81 IDZebra::Service - 
82
83 =head1 SYNOPSIS
84
85 =head1 DESCRIPTION
86
87 =head1 COPYRIGHT
88
89 Fill in
90
91 =head1 AUTHOR
92
93 Peter Popovics, pop@technomat.hu
94
95 =head1 SEE ALSO
96
97 IDZebra, IDZebra::Data1, Zebra documentation
98
99 =cut