IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 7, 2006, 5:29:19 PM (20 years ago)
Author:
jhoblitt
Message:

change Nebulous::Server instantiate objects (init -> new) instead of working with class data
add a DESTROY method to Nebulous::Server to tear down the database connection

Location:
trunk/Nebulous-Server
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server/Changes

    r5671 r10546  
    11Revision history for Perl module Poi::PixelData
     2
     30.03
     4    - change Nebulous::Server instantiate objects (init -> new) instead of
     5      working with class data
     6    - add a DESTROY method to Nebulous::Server to tear down the database
     7      connection
    28
    390.02 Fri Dec  2 17:36:59 HST 2005
  • trunk/Nebulous-Server/lib/Nebulous/Server.pm

    r5664 r10546  
    11# Copyright (c) 2004  Joshua Hoblitt
    22#
    3 # $Id: Server.pm,v 1.19 2005-12-03 02:32:56 jhoblitt Exp $
     3# $Id: Server.pm,v 1.20 2006-12-08 03:29:19 jhoblitt Exp $
    44
    55package Nebulous::Server;
     
    88use warnings FATAL => qw( all );
    99
    10 our $VERSION = '0.02';
     10our $VERSION = '0.03';
     11
     12use base qw( Class::Accessor::Fast );
    1113
    1214use DBI;
     
    1820use Params::Validate qw( validate_pos SCALAR );
    1921
    20 my $log;
    21 my $sql;
    22 my $db;
    23 
    24 sub init {
    25     my $self = shift;
     22__PACKAGE__->mk_accessors(qw( log sql db ));
     23
     24sub new {
     25    my $class = shift;
    2626
    2727    # let Nebulous::Server::Config validate our params
     
    3131    Nebulous::Server::Log->init;
    3232
    33     $log = Log::Log4perl::get_logger( "Nebulous::Server" );
    34     $sql = Nebulous::Server::SQL->new;
    35 
    36     $log->debug( "entered - @_" );
    37 
     33    my $log = Log::Log4perl::get_logger( "Nebulous::Server" );
     34    my $sql = Nebulous::Server::SQL->new;
     35
     36    $log->debug( "entered - @_" );
     37
     38    my $db;
    3839    eval {
    3940        $db = DBI->connect(
     
    6162    $log->debug( "leaving" );
    6263
    63     return 1;
     64    my $self = bless {}, ref $class || $class;
     65    $self->log($log);
     66    $self->sql($sql);
     67    $self->db($db);
     68
     69    return $self;
    6470}
    6571
     
    7682                'is + integer' => sub { $_[0] =~ /^\d+$/ },
    7783                # check that the class requested is valid
    78                 'is valid class id' => sub { _is_valid_class_id( $_[0] ) },
     84                'is valid class id' => sub { $self->_is_valid_class_id( $_[0] ) },
    7985            },
    8086            default     => 0,
     
    8490            callbacks   => {
    8591                # check that the volume requested is valid
    86                 'is valid volume name' => sub { _is_valid_volume_name($_[0]) },
     92                'is valid volume name' => sub { $self->_is_valid_volume_name($_[0]) },
    8793            },
    8894            optional    => 1,
     
    98104    );
    99105
     106    my $log = $self->log;
     107    my $sql = $self->sql;
     108    my $db  =$self->db;
     109
    100110    $log->debug( "entered - @_" );
    101111
    102112    # TODO do some intelligent volume allocated
    103113    # add hooks to make this decision based on avaiable space
    104     $volume = _get_storage_volume( $volume );
     114    $volume = $self->_get_storage_volume( $volume );
    105115
    106116    my $uri;
     
    194204    );
    195205
     206    my $log = $self->log;
     207    my $sql = $self->sql;
     208    my $db  =$self->db;
     209
    196210    $log->debug( "entered - @_" );
    197211
    198212    # TODO
    199     $volume = _get_storage_volume();
     213    $volume = $self->_get_storage_volume();
    200214
    201215    my $path;
     
    277291        },
    278292    );
     293
     294    my $log = $self->log;
     295    my $sql = $self->sql;
     296    my $db  =$self->db;
    279297
    280298    $log->debug( "entered - @_" );
     
    393411    );
    394412
     413    my $log = $self->log;
     414    my $sql = $self->sql;
     415    my $db  =$self->db;
     416
    395417    $log->debug( "entered - @_" );
    396418
     
    514536    );
    515537
     538    my $log = $self->log;
     539    my $sql = $self->sql;
     540    my $db  =$self->db;
     541
    516542    $log->debug( "entered - @_" );
    517543
     
    548574        },
    549575    );
     576
     577    my $log = $self->log;
     578    my $sql = $self->sql;
     579    my $db  =$self->db;
    550580
    551581    $log->debug( "entered - @_" );
     
    622652    );
    623653
     654    my $log = $self->log;
     655    my $sql = $self->sql;
     656    my $db  =$self->db;
     657
    624658    $log->debug( "entered - @_" );
    625659
     
    649683
    650684sub _get_storage_volume {
     685    my $self = shift;
     686
     687    my $log = $self->log;
     688    my $sql = $self->sql;
     689    my $db  =$self->db;
     690
    651691    no warnings qw( uninitialized );
    652692    $log->debug( "entered - @_" );
     
    690730
    691731sub _is_valid_class_id {
    692     my $class_id = shift;
     732    my ($self, $class_id) = @_;
     733
     734    my $log = $self->log;
     735    my $sql = $self->sql;
     736    my $db  = $self->db;
    693737
    694738    my $class;
     
    709753
    710754sub _is_valid_volume_name  {
    711     my $vol_name = shift;
     755    my ($self, $vol_name) = @_;
     756
     757    my $log = $self->log;
     758    my $sql = $self->sql;
     759    my $db  =$self->db;
    712760
    713761    my $volume;
    714762    eval {
    715         my $query = $db->prepare_cached( $sql->get_volume_by_name  );
     763        my $query = $db->prepare_cached( $sql->get_volume_by_name );
    716764        $query->execute( $vol_name );
    717765        ( $volume ) = $query->fetchrow_array;
     
    727775}
    728776
     777sub DESTROY {
     778    my $self = shift;
     779
     780    my $log = $self->log;
     781    my $sql = $self->sql;
     782    my $db  =$self->db;
     783
     784    $log->debug( "entered" );
     785
     786    $self->db->disconnect;       
     787
     788    $log->debug( "disconnected from database: ", sub { $db->data_sources; } );
     789
     790    $log->debug( "leaving" );
     791}
     792
    7297931;
    730794
  • trunk/Nebulous-Server/t/02_server_setup.t

    r4873 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 02_server_setup.t,v 1.4 2005-08-25 01:40:04 jhoblitt Exp $
     5# $Id: 02_server_setup.t,v 1.5 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1717Test::Nebulous->setup;
    1818
    19 ok(
    20     Nebulous::Server->init(
     19isa_ok(
     20    Nebulous::Server->new(
    2121        dsn         => "DBI:mysql:database=test:host=localhost",
    2222        dbuser      => "test",
    2323        dbpasswd    => "",
    2424    ),
    25     "connect",
     25    "Nebulous::Server"
    2626);
    2727
     
    2929
    3030eval {
    31     Nebulous::Server->init(
     31    Nebulous::Server->new(
    3232        dsn         => "DBI:mysql:database=foobar:host=localhost",
    3333        dbuser      => "baz",
  • trunk/Nebulous-Server/t/03_server_create_object.t

    r5504 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 03_server_create_object.t,v 1.10 2005-11-10 22:07:53 jhoblitt Exp $
     5# $Id: 03_server_create_object.t,v 1.11 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1616use Test::Nebulous;
    1717
    18 my $neb = "Nebulous::Server";
    19 
    20 $neb->init(
     18my $neb = Nebulous::Server->new(
    2119    dsn         => "DBI:mysql:database=test:host=localhost",
    2220    dbuser      => "test",
  • trunk/Nebulous-Server/t/04_server_replicate_object.t

    r4873 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 04_server_replicate_object.t,v 1.5 2005-08-25 01:40:04 jhoblitt Exp $
     5# $Id: 04_server_replicate_object.t,v 1.6 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1616use Test::Nebulous;
    1717
    18 my $neb = "Nebulous::Server";
    19 
    20 $neb->init(
     18my $neb = Nebulous::Server->new(
    2119    dsn         => "DBI:mysql:database=test:host=localhost",
    2220    dbuser      => "test",
  • trunk/Nebulous-Server/t/05_server_lock_object.t

    r4873 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 05_server_lock_object.t,v 1.5 2005-08-25 01:40:04 jhoblitt Exp $
     5# $Id: 05_server_lock_object.t,v 1.6 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1616use Test::Nebulous;
    1717
    18 my $neb = "Nebulous::Server";
    19 
    20 $neb->init(
     18my $neb = Nebulous::Server->new(
    2119    dsn         => "DBI:mysql:database=test:host=localhost",
    2220    dbuser      => "test",
  • trunk/Nebulous-Server/t/06_server_unlock_object.t

    r4873 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 06_server_unlock_object.t,v 1.5 2005-08-25 01:40:04 jhoblitt Exp $
     5# $Id: 06_server_unlock_object.t,v 1.6 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1616use Test::Nebulous;
    1717
    18 my $neb = "Nebulous::Server";
    19 
    20 $neb->init(
     18my $neb = Nebulous::Server->new(
    2119    dsn         => "DBI:mysql:database=test:host=localhost",
    2220    dbuser      => "test",
  • trunk/Nebulous-Server/t/07_server_find_instances.t

    r4873 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 07_server_find_instances.t,v 1.5 2005-08-25 01:40:04 jhoblitt Exp $
     5# $Id: 07_server_find_instances.t,v 1.6 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1616use Test::Nebulous;
    1717
    18 my $neb = "Nebulous::Server";
    19 
    20 $neb->init(
     18my $neb = Nebulous::Server->new(
    2119    dsn         => "DBI:mysql:database=test:host=localhost",
    2220    dbuser      => "test",
  • trunk/Nebulous-Server/t/08_server_delete_instance.t

    r4873 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 08_server_delete_instance.t,v 1.5 2005-08-25 01:40:04 jhoblitt Exp $
     5# $Id: 08_server_delete_instance.t,v 1.6 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1616use Test::Nebulous;
    1717
    18 my $neb = "Nebulous::Server";
    19 
    20 $neb->init(
     18my $neb = Nebulous::Server->new(
    2119    dsn         => "DBI:mysql:database=test:host=localhost",
    2220    dbuser      => "test",
  • trunk/Nebulous-Server/t/09_server_is_valid_class_id.t

    r5498 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 09_server_is_valid_class_id.t,v 1.1 2005-11-10 21:31:26 jhoblitt Exp $
     5# $Id: 09_server_is_valid_class_id.t,v 1.2 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1616use Test::Nebulous;
    1717
    18 my $neb = "Nebulous::Server";
    19 
    20 $neb->init(
     18my $neb = Nebulous::Server->new(
    2119    dsn         => "DBI:mysql:database=test:host=localhost",
    2220    dbuser      => "test",
     
    2422);
    2523
    26 *_is_valid_class_id = \&Nebulous::Server::_is_valid_class_id;
    27 
    2824Test::Nebulous->setup;
    2925
    3026# 0 is the default and is guarenteed to exist
    31 ok( _is_valid_class_id( 0 ), "default class id");
     27ok( $neb->_is_valid_class_id( 0 ), "default class id");
    3228
    3329Test::Nebulous->setup;
    3430
    3531# the test setup adds class id 1
    36 ok( _is_valid_class_id( 1 ), "custom class id");
     32ok( $neb->_is_valid_class_id( 1 ), "custom class id");
    3733
    3834Test::Nebulous->setup;
    3935
    40 is( _is_valid_class_id( 99 ), undef, "invalid class id");
     36is( $neb->_is_valid_class_id( 99 ), undef, "invalid class id");
    4137
    4238Test::Nebulous->cleanup;
  • trunk/Nebulous-Server/t/09_server_stat_object.t

    r5492 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 09_server_stat_object.t,v 1.7 2005-11-09 01:35:59 jhoblitt Exp $
     5# $Id: 09_server_stat_object.t,v 1.8 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1616use Test::Nebulous;
    1717
    18 my $neb = "Nebulous::Server";
    19 
    20 $neb->init(
     18my $neb = Nebulous::Server->new(
    2119    dsn         => "DBI:mysql:database=test:host=localhost",
    2220    dbuser      => "test",
  • trunk/Nebulous-Server/t/10_server_is_valid_volume_name.t

    r5667 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 10_server_is_valid_volume_name.t,v 1.1 2005-12-03 02:52:31 jhoblitt Exp $
     5# $Id: 10_server_is_valid_volume_name.t,v 1.2 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1616use Test::Nebulous;
    1717
    18 my $neb = "Nebulous::Server";
    19 
    20 $neb->init(
     18my $neb = Nebulous::Server->new(
    2119    dsn         => "DBI:mysql:database=test:host=localhost",
    2220    dbuser      => "test",
     
    2422);
    2523
    26 *_is_valid_volume_name = \&Nebulous::Server::_is_valid_volume_name;
     24Test::Nebulous->setup;
     25
     26ok( $neb->_is_valid_volume_name( 'node01' ), "valid volume name" );
    2727
    2828Test::Nebulous->setup;
    2929
    30 ok( _is_valid_volume_name( 'node01' ), "valid volume name" );
     30ok( $neb->_is_valid_volume_name( 'node02' ), "valid volume name" );
    3131
    3232Test::Nebulous->setup;
    3333
    34 ok( _is_valid_volume_name( 'node02' ), "valid volume name" );
    35 
    36 Test::Nebulous->setup;
    37 
    38 is( _is_valid_volume_name( 'node99' ), undef, "invalid volume name" );
     34is( $neb->_is_valid_volume_name( 'node99' ), undef, "invalid volume name" );
    3935
    4036Test::Nebulous->cleanup;
  • trunk/Nebulous-Server/t/11_server_is_valid_class_id.t

    r5667 r10546  
    33# Copryight (C) 2004-2005  Joshua Hoblitt
    44#
    5 # $Id: 11_server_is_valid_class_id.t,v 1.1 2005-12-03 02:52:31 jhoblitt Exp $
     5# $Id: 11_server_is_valid_class_id.t,v 1.2 2006-12-08 03:29:19 jhoblitt Exp $
    66
    77use strict;
     
    1616use Test::Nebulous;
    1717
    18 my $neb = "Nebulous::Server";
    19 
    20 $neb->init(
     18my $neb = Nebulous::Server->new(
    2119    dsn         => "DBI:mysql:database=test:host=localhost",
    2220    dbuser      => "test",
     
    2422);
    2523
    26 *_is_valid_class_id = \&Nebulous::Server::_is_valid_class_id;
    27 
    2824Test::Nebulous->setup;
    2925
    3026# 0 is the default and is guarenteed to exist
    31 ok( _is_valid_class_id( 0 ), "default class id");
     27ok( $neb->_is_valid_class_id( 0 ), "default class id");
    3228
    3329Test::Nebulous->setup;
    3430
    3531# the test setup adds class id 1
    36 ok( _is_valid_class_id( 1 ), "custom class id");
     32ok( $neb->_is_valid_class_id( 1 ), "custom class id");
    3733
    3834Test::Nebulous->setup;
    3935
    40 is( _is_valid_class_id( 99 ), undef, "invalid class id");
     36is( $neb->_is_valid_class_id( 99 ), undef, "invalid class id");
    4137
    4238Test::Nebulous->cleanup;
Note: See TracChangeset for help on using the changeset viewer.