1package Magus::Lock;
2#
3# Copyright (c) 2007,2008 Chris Reinhardt. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# 1. Redistributions of source code must retain the above copyright notice
10#    this list of conditions and the following disclaimer.
11#
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27
28#
29# MAINTAINER=   ctriv@MidnightBSD.org
30#
31
32
33use base qw(Magus::DBI);
34use strict;
35use warnings;
36
37__PACKAGE__->table('locks');
38__PACKAGE__->columns(Essential => qw(id port machine));
39__PACKAGE__->sequence('locks_id_seq');
40
41__PACKAGE__->has_a(machine => "Magus::Machine");
42__PACKAGE__->has_a(port    => "Magus::Port");
43
44__PACKAGE__->set_sql(by_run => <<'END_OF_SQL');
45SELECT locks.* FROM locks,ports WHERE port=ports.id AND ports.run=?
46END_OF_SQL
47
48sub get_ready_lock {
49  my ($class, $run) = @_;
50
51  my $lock;
52  my $port;
53
54  while (!defined $lock) {
55    my $port = Magus::Port->get_ready_port($run);
56
57    if (!$port) { # we ran thru all the ports...
58      return;
59    }
60
61    $lock = $class->_get_lock($port);
62  }
63
64  return $lock;
65}
66
67
68sub _get_lock {
69  my ($class, $port) = @_;
70  my $lock;
71
72  eval {
73    $lock = $class->insert({
74      port    => $port,
75      machine => $Magus::Machine,
76    });
77  };
78
79  if ($@) {
80    if ($@ =~ m/duplicate/i) {
81      return;
82    } else {
83      die $@;
84    }
85  }
86
87  return $lock;
88}
89
90#
91# depreacted method
92#
93use Carp qw(cluck);
94sub arch {
95  my ($self) = @_;
96  cluck("Use of deprecated method: " . ref $self . "->arch. Use ->machine->arch instead.");
97  return $self->machine->arch;
98}
99
1001;
101__END__
102
103