File Coverage

blib/lib/Geo/StormTracker/Data.pm
Criterion Covered Total %
statement 18 438 4.1
branch 0 142 0.0
condition 0 21 0.0
subroutine 6 28 21.4
pod 13 13 100.0
total 37 642 5.7


line stmt bran cond sub pod time code
1             package Geo::StormTracker::Data;
2              
3 1     1   9 use Carp;
  1         2  
  1         61  
4 1     1   528 use Geo::StormTracker::Parser;
  1         2  
  1         34  
5 1     1   7 use IO::File;
  1         1  
  1         159  
6 1     1   933 use IO::Dir;
  1         62734  
  1         171  
7 1     1   11 use strict;
  1         1  
  1         102  
8 1     1   8 use vars qw($VERSION);
  1         2  
  1         8736  
9              
10             $VERSION = '0.02';
11              
12             #------------------------------------------------------------------------------
13             sub new {
14 0     0 1   my $self=shift;
15 0           my $path=shift;
16              
17 0           my ($msg,$success, $io)=undef;
18 0           my $anon_HR={};
19              
20             #Check to see if the path was given
21 0 0         unless (defined($path)){
22 0           $msg = "The mandatory path argument was not provided to the new method!";
23 0           carp $msg,"\n";
24 0           return (undef,$msg);
25             }
26              
27             #Make sure the path ends with a single slash.
28 0           $path =~ s!/*$!/!;
29              
30             #Now check the path to see if it exists.
31             #If not return undefined.
32 0 0         unless (-e $path){
33 0           $msg="The new method only creates an object if the path already exists!";
34 0           $msg.="Consider using the shiny_new method instead!";
35 0           carp $msg,"\n";
36 0           return (undef,$msg);
37             }#if
38            
39             #anytime a new data object is created it should contain a
40             #the path, year, region, and event number for the weather event in question.
41              
42 0           $io=IO::File->new();
43 0 0         unless ($io->open("<${path}region")){
44 0           $msg="Couldn't open the ${path}region file!";
45 0           carp $msg, "\n";
46 0           return (undef, $msg);
47             }#unless
48 0           $anon_HR->{'region'}=$io->getline();
49 0           chomp $anon_HR->{'region'};
50 0           $io->close();
51            
52 0 0         unless ($io->open("<${path}year")){
53 0           $msg="Couldn't open the ${path}year file!";
54 0           carp $msg, "\n";
55 0           return (undef, $msg);
56             }#unless
57 0           $anon_HR->{'year'}=$io->getline();
58 0           chomp $anon_HR->{'year'};
59 0           $io->close();
60            
61 0 0         unless ($io->open("<${path}event_number")){
62 0           $msg="Couldn't open the ${path}event_number file!";
63 0           carp $msg, "\n";
64 0           return (undef, $msg);
65             }#unless
66 0           $anon_HR->{'event_number'}=$io->getline();
67 0           chomp $anon_HR->{'event_number'};
68 0           $io->close();
69              
70 0           $anon_HR->{'path'} = $path;
71 0           bless $anon_HR, 'Geo::StormTracker::Data';
72              
73 0           return ($anon_HR, undef);
74              
75             }#new
76             #------------------------------------------------------------------------------
77             sub shiny_new {
78 0     0 1   my $self=shift;
79 0           my $path=shift;
80 0           my $region=shift;
81 0           my $year=shift;
82 0           my $event_num=shift;
83            
84 0           my ($msg,$success, $io)=undef;
85 0           my $anon_HR={};
86            
87             #Check to see if the path was given
88 0 0         unless (defined($path)){
89 0           $msg = "The mandatory path argument was not provided to the shiny_new method!";
90 0           carp $msg,"\n";
91 0           return (undef,$msg);
92             }#unless
93              
94             #Check the region argument.
95 0 0 0       unless (
96             (defined $region) and
97             ($region =~ m!^\w{2}$!)
98             ){
99 0           $msg = "The shiny_new method's mandatory region argument was not provided or failed the syntax check!";
100 0           carp $msg,"\n";
101 0           return (undef,$msg);
102             }#unless
103            
104             #Check the year argument.
105 0 0 0       unless (
106             (defined $year) and
107             ($year =~ m!^\d{4}$!)
108             ){
109 0           $msg = "The shiny_new method's mandatory year argument was not provided or was not a 4 digit number!";
110 0           carp $msg,"\n";
111 0           return (undef,$msg);
112             }#unless
113            
114             #Check the event_num argument.
115 0 0 0       unless (
116             (defined $event_num) and
117             ($event_num =~ m!^\d+$!)
118             ){
119 0           $msg = "The shiny_new method's mandatory event number argument was not provided or was not a number!";
120 0           carp $msg,"\n";
121 0           return (undef,$msg);
122             }#unless
123            
124             #Make sure the path ends with a single slash.
125 0           $path =~ s!/*$!/!;
126            
127             #Now check the path to see if it exist.
128             #If the path already exists, return undefined.
129 0 0         if (-e $path){
130 0           $msg="The path already exists. The shiny_new method always fails in this event!";
131 0           return (undef,$msg);
132             }#if
133              
134 0           $success=mkdir($path,0776);
135 0 0         unless ($success) {
136 0           $msg = "Could not create a directory $path!";
137 0           $msg .= "Consider using the new method!";
138 0           carp $msg,"\n";
139 0           return (undef,$msg);
140             }#unless
141              
142             #Write out the region file.
143 0           $io=IO::File->new();
144 0 0         unless ($io->open(">${path}region")){
145 0           $msg="Could not create a ${path}region file!";
146 0           $msg.= " The database is likely in a corrupt state due to this failure!";
147 0           carp $msg, "\n";
148 0           return (undef, $msg);
149             }#unless
150 0           $io->print($region);
151 0           $io->close();
152              
153             #Write out the year file.
154 0 0         unless ($io->open(">${path}year")){
155 0           $msg="Could not create a ${path}year file!";
156 0           $msg.= " The database is likely in a corrupt state due to this failure!";
157 0           carp $msg, "\n";
158 0           return (undef, $msg);
159             }#unless
160 0           $io->print($year);
161 0           $io->close();
162            
163             #Write out the event_number file.
164 0 0         unless ($io->open(">${path}event_number")){
165 0           $msg="Could not create a ${path}event_number file!";
166 0           $msg.= " The database is likely in a corrupt state due to this failure!";
167 0           carp $msg, "\n";
168 0           return (undef, $msg);
169             }#unless
170 0           $io->print($event_num);
171 0           $io->close();
172            
173             #anytime a new data object is created it should contain a
174             #the path, region, year, and event_number for the weather event in question.
175            
176 0           $anon_HR->{'path'} = $path;
177 0           $anon_HR->{'region'}=$region;
178 0           $anon_HR->{'year'}=$year;
179 0           $anon_HR->{'event_number'}=$event_num;
180 0           bless $anon_HR, 'Geo::StormTracker::Data';
181            
182 0           return ($anon_HR, undef);
183            
184             }#shiny_new
185             #------------------------------------------------------------------------------
186             sub get_path {
187 0     0 1   my $self=shift;
188 0           return $self->{'path'};
189             }#get_path
190             #------------------------------------------------------------------------------
191             sub get_region {
192 0     0 1   my $self=shift;
193 0           return $self->{'region'};
194             }#get_region
195             #------------------------------------------------------------------------------
196             sub get_year {
197 0     0 1   my $self=shift;
198 0           return $self->{'year'};
199             }#get_year
200             #------------------------------------------------------------------------------
201             sub get_event_number {
202 0     0 1   my $self=shift;
203 0           return $self->{'event_number'};
204             }#get_event_number
205             #------------------------------------------------------------------------------
206             sub is_active {
207 0     0 1   my $self=shift;
208 0           my $arg=shift;
209 0           my $ignore_lock=shift;
210            
211 0           my ($activefile,$io,$success,$error,$msg)=undef;
212              
213 0 0         $ignore_lock=0 if (!defined $ignore_lock);
214              
215 0           $activefile=$self->{'path'}.'activefile';
216              
217 0 0         if (defined($arg)){
218 0 0         ($success,$error)=$self->_patiently_grab_lock() unless ($ignore_lock);
219 0 0 0       unless (($success) or ($ignore_lock)){
220 0           $msg="Couldn't grab write lock for this weather event!";
221 0           $msg.=" error was: $error";
222 0           carp $msg,"\n";
223 0           return (undef,$msg);
224             }#unless
225              
226 0 0         if ($arg){
227 0           $io=IO::File->new();
228 0 0         unless($io->open(">$activefile")){
229 0           $msg="Couldn't open $activefile in is_active method!";
230 0           carp $msg,"\n";
231 0           return (undef,$msg);
232             }
233 0           $io->print($$);
234 0           $io->close();
235             }
236             else {
237 0 0         unless(unlink($activefile)){
238 0           $msg="Couldn't unlink $activefile in is_active method!";
239 0           carp $msg,"\n";
240 0           return (undef,$msg);
241             }
242             }#if/else
243              
244 0 0         ($success,$error)=$self->_patiently_release_lock() unless ($ignore_lock);
245 0 0 0       unless (($success) or ($ignore_lock)){
246 0           $msg="Couldn't release write lock for this weather event!";
247 0           $msg.=" error was: $error";
248 0           carp $msg,"\n";
249 0           return (undef,$msg);
250             }
251 0           return ($arg,undef);
252             }
253             else {
254 0 0         if (-e $activefile){
255 0           return (1,undef);
256             }
257             else {
258 0           return (0,undef);
259             }
260             }#if/else
261             }
262             #------------------------------------------------------------------------------
263             #Need to figure out something sensensible to do with the success and error return
264             #values.
265             sub insert_advisory {
266 0     0 1   my $self=shift;
267 0           my $adv_obj=shift;
268 0           my $force_option=shift;
269 0           my $active_state=shift;
270              
271 0           my ($i,$got_lock,$lost_lock,$success,$error,$msg,$set_to)=undef;
272              
273             #attempt to grab a write lock
274 0           ($got_lock,$msg)=$self->_patiently_grab_lock();
275 0 0         unless ($got_lock){
276 0           return (undef,$msg);
277             }#unless
278            
279 0           ($success,$msg)=$self->_write_advisory($adv_obj,$force_option);
280 0 0         unless ($success){
281 0           ($lost_lock,$error)=$self->_patiently_release_lock();
282 0 0         $msg .= $error unless ($lost_lock);
283 0           return (undef,$msg);
284             }#unless
285              
286 0           ($success,$msg)=$self->_update_trackfile($adv_obj,$force_option);
287 0 0         unless ($success){
288 0           ($lost_lock,$error)=$self->_patiently_release_lock();
289 0 0         $msg .= $error unless ($lost_lock);
290 0           return (undef,$msg);
291             }#unless
292              
293 0 0         if (defined $active_state){
294 0           ($set_to,$msg)=$self->is_active($active_state,1);
295 0 0         unless (defined $set_to){
296 0           ($lost_lock,$error)=$self->_patiently_release_lock();
297 0 0         $msg .= $error unless ($lost_lock);
298 0           return (undef,$msg);
299             }
300             }#if
301              
302             #attempt to release the write lock
303 0           ($success,$error)=$self->_patiently_release_lock();
304              
305 0 0         if ($success){
306 0           return (1,undef);
307             }
308             else {
309 0           return (undef,$error);
310             }#if/else
311              
312             }#insert_advisory
313             #------------------------------------------------------------------------------
314             sub all_advisories {
315 0     0 1   my $self=shift;
316              
317 0           my ($parser,$file,$adv_obj)=undef;
318 0           my @advisory_files=();
319 0           my @adv_obj_array=();
320              
321 0           @advisory_files=$self->_sorted_advisory_files();
322              
323 0           $parser=Geo::StormTracker::Parser->new();
324            
325 0           foreach $file (@advisory_files){
326 0           $adv_obj=$parser->read_file($self->{'path'}.$file);
327 0 0         push (@adv_obj_array,$adv_obj) if (defined $adv_obj);
328             }
329              
330 0 0         return wantarray ? @adv_obj_array : \@adv_obj_array;
331             }#all_advisories
332             #-----------------------------------------------------------------------------
333             sub current_advisory {
334 0     0 1   my $self=shift;
335              
336 0           my ($parser,$current_advisory_file,$adv_obj);
337 0           my @advisory_files=();
338              
339 0           @advisory_files=$self->_sorted_advisory_files();
340 0           $current_advisory_file=$advisory_files[$#advisory_files];
341            
342 0           $parser=Geo::StormTracker::Parser->new();
343              
344 0           $adv_obj=$parser->read_file($self->{'path'}.$current_advisory_file);
345              
346 0           return $adv_obj;
347             }#current_advisory
348             #-----------------------------------------------------------------------------
349             sub advisory_by_number {
350 0     0 1   my $self=shift;
351 0           my $advisory_number=shift;
352              
353 0           my ($d,$path,$msg,$file,$target_file,$parser,$number,$adv_obj)=undef;
354 0           my @file_list=();
355              
356 0           $path=$self->{'path'};
357              
358             #Check the advisory_number argument
359 0 0         unless (defined $advisory_number){
360 0           $msg="An advisory number argument must be given to the advisory_by_number method!";
361 0           carp $msg,"\n";
362 0           return undef;
363             }
364              
365             #Grab list of files in the $path directory.
366 0           $d=IO::Dir->new();
367 0           $d->open($path);
368 0 0         unless (defined($d)){
369 0           $msg = "Had trouble reading $path directory!";
370 0           carp $msg,"\n";
371 0           return undef;
372             }
373 0           @file_list=$d->read();
374 0           $d->close();
375              
376             #Figure out which file is has the advisory number we want.
377 0           $target_file=undef;
378 0           foreach $file (@file_list){
379 0 0         next if $file =~ m!^(\.|\.\.)$!;
380 0           $file =~ m!(\d+[A-Z]*)\.adv!;
381 0           $number=$1;
382             #if ((defined $number) and ($advisory_number == $number)){
383 0 0 0       if ((defined $number) and ($self->_compare_advisory_numbers($advisory_number,$number) == 0) ){
384 0           $target_file=$file;
385 0           last;
386             }#if;
387             }#foreach
388              
389             #If the desired file wasn't found then return undef.
390 0 0         return undef unless (defined $target_file);
391              
392             #parse the file and obtain its advisory object.
393 0           $parser=Geo::StormTracker::Parser->new();
394              
395 0           $adv_obj=$parser->read_file($self->{'path'}.$target_file);
396              
397             #return the advisory object
398 0           return $adv_obj;
399             }#advisory_by_number
400             #------------------------------------------------------------------------------
401             sub _sorted_advisory_files {
402 0     0     my $self=shift;
403              
404 0           my ($d,$path,$msg,$file)=undef;
405 0           my @unsorted_advisory_files=();
406 0           my @advisory_files=();
407 0           my @file_list=();
408              
409 0           $path=$self->{'path'};
410              
411 0           $d=IO::Dir->new();
412 0           $d->open($path);
413 0 0         unless (defined($d)){
414 0           $msg = "Had trouble reading $path directory!";
415 0           carp $msg,"\n";
416 0           return undef;
417             }
418            
419 0           @file_list=$d->read;
420 0           $d->close();
421              
422 0           foreach $file (@file_list){
423 0 0         if ($file =~ m!\d+[A-Z]*\.adv$!){
424 0           push(@unsorted_advisory_files,$file);
425             }#if
426             }#foreach
427              
428 0           @advisory_files=sort {
429 0           $a =~ m!(\d+[A-Z]*)\.adv$!;
430 0           my $num_a=$1;
431 0           $b =~ m!(\d+[A-Z]*)\.adv$!;
432 0           my $num_b=$1;
433             #$num_a <=> $num_b;
434 0           $self->_compare_advisory_numbers($num_a,$num_b);
435             }
436             @unsorted_advisory_files;
437              
438 0           return @advisory_files;
439              
440             }#_sorted_advisory_files
441             #------------------------------------------------------------------------------
442             sub current_position {
443 0     0 1   my $self=shift;
444              
445 0           my @position_array=();
446              
447 0           @position_array=$self->position_track();
448              
449 0           return $position_array[$#position_array];
450              
451             }#current_postion
452             #------------------------------------------------------------------------------
453             sub position_track {
454 0     0 1   my $self=shift;
455              
456 0           my ($trackfile,$in_line,$lat_digit,$lat_dir,$long_digit,$long_dir,$msg)=undef;
457 0           my @position_array=();
458              
459 0           $trackfile=$self->{'path'}.'trackfile';
460              
461 0           my $io_in=IO::File->new();
462 0 0         unless ($io_in->open("<$trackfile")){
463 0           $msg="position_track method couldn't read from $trackfile!";
464 0           carp $msg,"\n";
465 0           return undef;
466             }
467 0           while (defined($in_line=$io_in->getline)){
468 0           chomp($in_line);
469 0           ($lat_digit,$lat_dir,$long_digit,$long_dir)=split(',',(split("\t",$in_line))[5]);
470 0           push (@position_array,[$lat_digit,$lat_dir,$long_digit,$long_dir]);
471             }#while
472              
473 0 0         return wantarray ? @position_array : \@position_array;
474             }#positon_track
475             #------------------------------------------------------------------------------
476             sub _construct_data_line {
477 0     0     my $self=shift;
478 0           my $adv_obj=shift;
479              
480 0           my ($data_line,$advisory_number,$event_type,$release_time)=undef;
481 0           my ($max_winds,$min_central_pressure,$position_AR)=undef;
482 0           my @data_line=();
483              
484 0           $advisory_number=$adv_obj->advisory_number();
485 0           $event_type=$adv_obj->event_type();
486 0           $release_time=$adv_obj->release_time();
487 0           $max_winds=$adv_obj->max_winds();
488 0           $min_central_pressure=$adv_obj->min_central_pressure();
489 0           $position_AR=$adv_obj->position();
490              
491 0 0         if (defined $advisory_number){
492 0           push(@data_line,$advisory_number);
493             }
494             else {
495 0           push(@data_line,'');
496             }#if/else
497              
498 0 0         if (defined($event_type)){
499 0           push(@data_line,$event_type);
500             }
501             else {
502 0           push(@data_line,'');
503             }#if/else
504              
505 0 0         if (defined $release_time){
506 0           push(@data_line,$release_time);
507             }
508             else {
509 0           push(@data_line,'');
510             }#if/else
511              
512 0 0         if (defined $max_winds){
513 0           push(@data_line,$max_winds);
514             }
515             else {
516 0           push(@data_line,'');
517             }#if/else
518              
519 0 0         if (defined $min_central_pressure){
520 0           push(@data_line,$min_central_pressure);
521             }
522             else {
523 0           push(@data_line,'');
524             }#if/else
525              
526 0 0         if (defined $position_AR){
527 0           push(@data_line,join(',',@{$position_AR}));
  0            
528             }
529             else {
530 0           push(@data_line,'');
531             }#if/else
532              
533 0           $data_line=join("\t",@data_line);
534              
535 0           return $data_line;
536              
537             }#_construct_data_line
538             #------------------------------------------------------------------------------
539             sub _update_trackfile {
540 0     0     my $self=shift;
541 0           my $adv_obj=shift;
542 0           my $force_option=shift;
543            
544 0           my ($msg,$success,$io_in,$io_out,$in_line,$advisory_index,$data_line)=undef;
545 0           my ($adv_comp,$trackfile,$advisory_number,$added_data)=undef;
546              
547 0           $data_line=$self->_construct_data_line($adv_obj);
548              
549 0           $trackfile=$self->{'path'}."trackfile";
550 0           $advisory_number=$adv_obj->advisory_number();
551            
552 0 0         if (-e $trackfile) {
553 0           $success=rename($trackfile,"$trackfile\.old");
554 0 0         unless ($success) {
555 0           $msg="Couldn't move $trackfile to $trackfile\.old!";
556 0           croak $msg,"\n";
557             #return (0,$msg);
558             }#unless
559              
560 0           $io_in=IO::File->new();
561 0 0         unless ($io_in->open("<$trackfile\.old")){
562 0           $msg="Couldn't open $trackfile\.old for reading!";
563 0           croak $msg,"\n";
564             #return (0,$msg);
565             }#unless
566              
567 0           $io_out=IO::File->new();
568 0 0         unless ($io_out->open(">$trackfile")){
569 0           $msg="Couldn't open $trackfile for writting!";
570 0           croak $msg,"\n";
571             #return (0,$msg);
572             }#unless
573              
574 0           $added_data=0;
575 0           while (defined($in_line=$io_in->getline)){
576 0           chomp($in_line);
577              
578 0           $advisory_index=(split("\t",$in_line))[0];
579            
580 0           $adv_comp=$self->_compare_advisory_numbers($advisory_index,$advisory_number);
581            
582             #if ($advisory_index < $advisory_number){
583 0 0         if ($adv_comp < 0){
    0          
584 0           $io_out->print($in_line,"\n");
585             }
586             #elsif ($advisory_index == $advisory_number){
587             elsif ($adv_comp == 0){
588 0 0         if ($force_option){
589 0           $io_out->print($data_line,"\n");
590             }
591             else {
592 0           $io_out->print($in_line,"\n");
593 0           $msg="Advisory number $advisory_number already exists and force option is not on!";
594 0           $msg.=" The original track information was not changed!";
595 0           carp $msg,"\n";
596             }#if/else
597              
598 0           $added_data=1;
599             }
600             else {
601 0 0         unless ($added_data){
602 0           $io_out->print($data_line,"\n");
603 0           $added_data=1;
604             }#unless
605              
606 0           $io_out->print($in_line,"\n");
607             }#if/elsif/else
608             }#while
609 0           $io_out->close();
610 0           $io_in->close();
611            
612 0 0         unless(unlink("$trackfile\.old")){
613 0           $msg="Couldn't unlink $trackfile\.old!";
614 0           carp $msg,"\n";
615             }
616             }
617             else {
618 0           $io_out=IO::File->new();
619 0 0         unless ($io_out->open(">$trackfile")){
620 0           $msg="Couldn't open $trackfile for writting!";
621 0           croak $msg,"\n";
622             #return (0,$msg);
623             }#unless
624 0           $io_out->print($data_line,"\n");
625 0           $io_out->close();
626             }#if/else
627            
628 0           return (1,$msg);
629             }#_update_trackfile
630             #------------------------------------------------------------------------------
631             sub _compare_advisory_numbers {
632 0     0     my $self=shift;
633 0           my $adv_num1=shift;
634 0           my $adv_num2=shift;
635              
636 0           my ($num1_digits,$num1_alpha,$num2_digits,$num2_alpha)=undef;
637              
638 0           $adv_num1 =~ m!(\d+)([A-Z]*)$!;
639 0           $num1_digits=$1;
640 0           $num1_alpha=$2;
641 0 0         $num1_alpha=uc $num1_alpha if (defined $num1_alpha);
642 0           $num1_alpha =~ tr/ABCDEFGHI/123456789/;
643 0           $adv_num1="$num1_digits\.$num1_alpha";
644              
645 0           $adv_num2 =~ m!(\d+)([A-Z]*)$!;
646 0           $num2_digits=$1;
647 0           $num2_alpha=$2;
648 0 0         $num2_alpha=uc $num2_alpha if (defined $num2_alpha);
649 0           $num2_alpha =~ tr/ABCDEFGHI/123456789/;
650 0           $adv_num2="$num2_digits\.$num2_alpha";
651              
652 0           return $adv_num1 <=> $adv_num2;
653             }#_compare_advisory_numbers
654             #------------------------------------------------------------------------------
655             sub _write_advisory {
656 0     0     my $self=shift;
657 0           my $adv_obj=shift;
658 0           my $force_option=shift;
659              
660 0           my ($io,$filename,$msg,$path)=undef;
661              
662             #Come up with a filename unique to each advisory number
663             #and which has some indicator of storm type
664 0           $path=$self->{'path'};
665 0           $filename =$adv_obj->event_type();
666 0           $filename =~ s!\s!!gs;
667 0           $filename .= $adv_obj->advisory_number();
668 0           $filename .= '.adv';
669 0           $filename = "${path}${filename}";
670              
671              
672 0 0 0       if ((-e $filename) and (!$force_option)) {
673 0           $msg="Filename $filename exists and force option is not on!";
674 0           carp $msg,"\n";
675 0           return (0,$msg);
676             }
677             else {
678 0           $io=IO::File->new();
679 0 0         unless ($io->open(">$filename")){
680 0           $msg="Couldn't write to file $filename!";
681 0           carp $msg,"\n";
682 0           return (0,$msg);
683             }
684 0           $io->print($adv_obj->stringify());
685 0           $io->close();
686 0           return (1,undef);
687             }
688             }#_write_advisory
689             #------------------------------------------------------------------------------
690             sub _patiently_grab_lock{
691 0     0     my $self=shift;
692              
693 0           my ($success,$msg,$i)=undef;
694              
695 0           for ($i = 0; $i <= 4; $i++){
696 0           $success=$self->_grab_advisory_lock();
697 0 0         last if ($success);
698 0 0         if ($i == 4) {
699 0           $msg="Could not grab a write lock!";
700 0           carp $msg,"\n";
701 0           return (0,$msg);
702             }
703             else {
704 0           sleep 2;
705             }#if/else
706             }#for
707              
708 0           return (1,undef);
709              
710             }#_patiently_grab_lock
711             #------------------------------------------------------------------------------
712             sub _patiently_release_lock{
713 0     0     my $self=shift;
714              
715 0           my ($success,$msg,$i)=undef;
716              
717 0           for ($i = 0; $i <= 4; $i++){
718 0           $success=$self->_release_advisory_lock();
719 0 0         last if ($success);
720 0 0         if ($i == 4) {
721 0           $msg="Could not release the write lock!";
722 0           carp $msg,"\n";
723 0           return (0,$msg);
724             }
725             else {
726 0           sleep 2;
727             }#if/else
728             }#for
729              
730 0           return (1,undef);
731              
732             }#_patiently_release_lock
733             #------------------------------------------------------------------------------
734             #$success=$self->_grab_advisory_lock();
735             sub _grab_advisory_lock {
736 0     0     my $self=shift;
737              
738 0           my ($lock_file,$io)=undef;
739              
740 0           $lock_file=$self->{'path'}."lockfile";
741 0 0         if (-e $lock_file) {
742 0           return 0;
743             }
744             else {
745 0           $io=IO::File->new();
746 0 0         $io->open(">$lock_file") or croak "Couldn't write to $lock_file\n";
747 0           $io->print("$$");
748 0           $io->close();
749 0           return 1;
750             }#if/else
751             }#_grab_advisory_lock
752             #------------------------------------------------------------------------------
753             #$success=$self->_release_advisory_lock();
754             sub _release_advisory_lock {
755 0     0     my $self=shift;
756              
757 0           my $lock_file=undef;
758              
759 0           $lock_file=$self->{'path'}."lockfile";
760              
761 0 0         if (unlink($lock_file)){
762 0           return 1;
763             }
764             else {
765 0           return 0;
766             }
767             }#_release_advisory_lock
768             #------------------------------------------------------------------------------
769              
770             1;
771             __END__