#! /usr/bin/perl -w use strict; use lib "/nss/nfx/shmem/lib/lib"; use lib "/nss/nfx/shmem/lib/lib64"; use lib "/nss/nfx/shmem/lib/lib/perl5/site_perl/"; use File::Basename; use Getopt::Long; use File::Copy; use Data::Dumper; use strict; ###################### ######## Shared Memory Decl. ######### use IPC::Shareable; ################ my $child; my $serv_id; my $maxRcvrCount; my $stripeInstance; my $maxStripeInstance; my $user; my @ftpDirectoryListing=(); my @deDuppedArray=(); my @filesDownloaded=(); my $glue = 'data'; my %options = ( create => 1, exclusive => 0, mode => 0644, destroy => 0, ); my $deDuppedArrayHandle = tie @deDuppedArray, 'IPC::Shareable', undef, \%options; my $filesDownloadedHandle = tie @filesDownloaded, 'IPC::Shareable', undef, \%options; sub pollDaemonParent { my $error = ""; my $user = $_[0]; my $tmpIntervalHolder = .5; my @previousListing=(); my @currentListing=(); ConnectAndLogin(); while (1) { @previousListing = @currentListing; @currentListing = getListing($user); $deDuppedArrayHandle->shlock(); @deDuppedArray = deDupArray1(\@previousListing, \@currentListing); if ($#deDuppedArray >= 0) { print "\nParent: currentListing: @currentListing deDuppedArray: @deDuppedArray\n"; } $deDuppedArrayHandle->shunlock(); deleteRemoteFiles1(); select (undef, undef, undef,$tmpIntervalHolder); } } sub deleteRemoteFiles1 { # TODO: Get list and delete from server. once deleted $filesDownloadedHandle->shlock(); if ($#filesDownloaded >= 0) { print "\nParent: deleteListing: @filesDownloaded\n"; } foreach (@filesDownloaded) { print "Deleting File: $_\n"; } @filesDownloaded=(); $filesDownloadedHandle->shunlock(); } sub pollDaemonChild { my $error = ""; my $user = $_[0]; my $tmpIntervalHolder = 1; print "ChildStarted: $stripeInstance\n"; while (1) { @ftpDirectoryListing = (); $deDuppedArrayHandle->shlock(); @ftpDirectoryListing = @deDuppedArray; $deDuppedArrayHandle->shunlock(); if ($#ftpDirectoryListing >= 0) { print "Child: @ftpDirectoryListing\n"; } select (undef, undef, undef,$tmpIntervalHolder); processList($user); } } sub ConnectAndLogin { return; } sub getListing { my @ftpListing1 = (); push (@ftpListing1, "abc.xml"); push (@ftpListing1, "xyz.xml"); return @ftpListing1; } sub deDupArray1 { my ($previousArray, $currentArray) = @_; my @union;my @intersection;my@difference; @union = @intersection = @difference = (); my %count = (); my $element=""; ##################################################################################################### # get Difference of two arrays. ##################################################################################################### # go into foreach loop for any element that exist in both previousArray and currentArray and increment # count of element value. foreach $element (@$previousArray, @$currentArray) { $count{$element}++ } foreach $element (keys %count) { #print "$element:$count{$element}\n"; push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element; } ##################################################################################################### # Remove any element that existed in previous list from not in current list. ##################################################################################################### my @elements_needs_to_be_removed = (@intersection, @$previousArray); %count = (); foreach $element (@elements_needs_to_be_removed, @difference) { $count{$element}++ } @difference=(); foreach $element (keys %count) { # print "$element:$count{$element} "; push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element; } return @difference; } sub addToDeleteList { $|++; #print "\nAdding file to Delete List: $_[0]\n"; $filesDownloadedHandle->shlock(); push (@filesDownloaded, "$_[0]"); $filesDownloadedHandle->shunlock(); } # main { $user = "IAmUser"; $serv_id = "1"; $maxRcvrCount = "5"; $stripeInstance = "1" ; $maxStripeInstance = "2"; $SIG{'ALRM'} = 'sig_alrm'; my $startingInstanceNum=1; my @kids; for (1 .. $maxStripeInstance) { $stripeInstance = $startingInstanceNum; print "stripeInstance: $startingInstanceNum\n"; unless ($child = fork) { # i'm the child die "cannot fork: $!" unless defined $child; pollDaemonChild($user); exit; } push @kids, $child; # in case we care about their pids $startingInstanceNum++; } print "@kids\n"; pollDaemonParent($user); }