#!/usr/bin/perl -w $keep = 10000; $dir = "save"; opendir(DIR, $dir ); @WARRIORS = grep { -f "$dir/$_" } readdir(DIR); my $tmp = @WARRIORS; print "We have $tmp warriors!\n"; for($i=0; $i < @WARRIORS; $i++){ $SCORES{$WARRIORS[$i]} = -1; } use IO::Socket; $sock = new IO::Socket::INET (LocalHost => 'ned', LocalPort => 1200, Proto => 'tcp', Listen => 10, Reuse => 1 ); die "Socket could not be created. Reason: $!" unless $sock; print "Serving:\n"; while(1){ $new_sock = $sock->accept(); #Read read score $buf = $new_sock->getline(); chomp $buf; ($warrior,$score) = split(/:/,$buf); print("client returned: $warrior:$score\n"); #Insert scores into array if($score != -1){ if($score == 0){ $score = 1; } $SCORES{$warrior} = $score; } #assign new work for($i=0; $i < @WARRIORS; $i++){ if($SCORES{$WARRIORS[$i]} > -1){ $assigned = 0; next; } $assigned = 1; print "Assigning $WARRIORS[$i]\n"; print($new_sock "$WARRIORS[$i]:0\n"); $SCORES{$WARRIORS[$i]} = 0; last; } #We're out of work! Check and see if we have any that haven't come #back yet, we'll reassign if(!$assigned){ for($i=0; $i < @WARRIORS; $i++){ if($SCORES{$WARRIORS[$i]} > 0){ $assigned = 0; next; } $assigned = 1; print "Assigning $WARRIORS[$i]\n"; print($new_sock "$WARRIORS[$i]:0\n"); $SCORES{$WARRIORS[$i]} = 0; last; } if(!$assigned){ print "No work to assign!\n"; print($new_sock "-1:-1"); } } #close client connection close($new_sock); #check to see if we have all our answers $dc = 0; for($i=0; $i < @WARRIORS; $i++){ if($SCORES{$WARRIORS[$i]} > 0){ $done = 1; $dc++; next; } $done = 0; } printf("%f done (%d / %d)\n",($dc/$tmp)*100,$dc,$tmp); if($done){ last; } $done = 1; } foreach $W (sort {$SCORES{$b} <=> $SCORES{$a}} keys %SCORES) { $cnt++; if ($cnt <= ($keep)) { if($cnt <= 10){ print "keeping $W with score $SCORES{$W}\n"; #system("cp save/$W skim/"); } next; } for ($j=0;$j < @WARRIORS; $j++) { if ($WARRIORS[$j] eq $W) { #print "dropping $W with score $SCORES{$W}\n"; unlink("$dir/$W"); splice(@WARRIORS,$j,1); } } }