| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::SeedServe; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 6 |  |  | 6 |  | 141584 | use 5.008; | 
|  | 6 |  |  |  |  | 17 |  | 
| 4 | 6 |  |  | 6 |  | 20 | use strict; | 
|  | 6 |  |  |  |  | 7 |  | 
|  | 6 |  |  |  |  | 106 |  | 
| 5 | 6 |  |  | 6 |  | 17 | use warnings; | 
|  | 6 |  |  |  |  | 9 |  | 
|  | 6 |  |  |  |  | 107 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 6 |  |  | 6 |  | 2507 | use IO::Socket::INET; | 
|  | 6 |  |  |  |  | 85000 |  | 
|  | 6 |  |  |  |  | 26 |  | 
| 8 | 6 |  |  | 6 |  | 2145 | use IO::All; | 
|  | 6 |  |  |  |  | 6 |  | 
|  | 6 |  |  |  |  | 29 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $VERSION = '0.2.7'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub new | 
| 13 |  |  |  |  |  |  | { | 
| 14 | 3 |  |  | 3 | 1 | 124 | my $class = shift; | 
| 15 | 3 |  |  |  |  | 50 | my $self = +{}; | 
| 16 | 3 |  |  |  |  | 28 | bless $self, $class; | 
| 17 | 3 |  |  |  |  | 33 | $self->_init(@_); | 
| 18 | 3 |  |  |  |  | 17 | return $self; | 
| 19 |  |  |  |  |  |  | } | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | sub _init | 
| 22 |  |  |  |  |  |  | { | 
| 23 | 3 |  |  | 3 |  | 15 | my $self = shift; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 3 |  |  |  |  | 80 | my %args = (@_); | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 3 | 50 |  |  |  | 73 | my $port = $args{'port'} or | 
| 28 |  |  |  |  |  |  | die "Port not specified!"; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 3 |  |  |  |  | 67 | $self->{'port'} = $port; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 3 | 50 |  |  |  | 119 | $self->{'status_file'} = $args{'status_file'} or | 
| 33 |  |  |  |  |  |  | die "Success file not specified"; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 3 |  |  |  |  | 6 | return 0; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub _update_status_file | 
| 39 |  |  |  |  |  |  | { | 
| 40 | 3 |  |  | 3 |  | 8 | my $self = shift; | 
| 41 | 3 |  |  |  |  | 4 | my $string = shift; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 3 |  |  |  |  | 22 | io()->file($self->{'status_file'})->print("$string\n"); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub loop | 
| 47 |  |  |  |  |  |  | { | 
| 48 | 3 |  |  | 3 | 1 | 5 | my $self = shift; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 3 |  |  |  |  | 6 | my $serving_socket; | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | $serving_socket = | 
| 53 |  |  |  |  |  |  | IO::Socket::INET->new( | 
| 54 |  |  |  |  |  |  | Listen    => 5, | 
| 55 |  |  |  |  |  |  | LocalAddr => 'localhost', | 
| 56 | 3 |  |  |  |  | 76 | LocalPort => $self->{'port'}, | 
| 57 |  |  |  |  |  |  | Proto     => 'tcp' | 
| 58 |  |  |  |  |  |  | ); | 
| 59 | 3 | 50 |  |  |  | 3525 | if (!defined($serving_socket)) | 
| 60 |  |  |  |  |  |  | { | 
| 61 | 3 |  |  |  |  | 13 | $self->_update_status_file("Status:Error"); | 
| 62 | 3 |  |  |  |  | 17752 | die $@; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | $self->_update_status_file( | 
| 66 | 0 |  |  |  |  |  | "Status:Success\tPort:" . $self->{'port'} . "\tPID:$$" | 
| 67 |  |  |  |  |  |  | ); | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 0 |  |  |  |  |  | my @queue; | 
| 70 |  |  |  |  |  |  | my $next_seed; | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | my $clear = sub { | 
| 73 | 0 |  |  | 0 |  |  | @queue = (); | 
| 74 | 0 |  |  |  |  |  | $next_seed = 1; | 
| 75 | 0 |  |  |  |  |  | }; | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 |  |  |  |  |  | $clear->(); | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 0 |  |  |  |  |  | while (my $conn = $serving_socket->accept()) | 
| 80 |  |  |  |  |  |  | { | 
| 81 | 0 |  |  |  |  |  | my $request = $conn->getline(); | 
| 82 | 0 |  |  |  |  |  | my $response; | 
| 83 | 0 | 0 |  |  |  |  | if ($request =~ /^FETCH/) | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | { | 
| 85 | 0 |  |  |  |  |  | my $seed; | 
| 86 | 0 | 0 |  |  |  |  | if ($seed = shift(@queue)) | 
| 87 |  |  |  |  |  |  | { | 
| 88 | 0 |  |  |  |  |  | $response = $seed; | 
| 89 | 0 |  |  |  |  |  | $next_seed = $seed+1; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | else | 
| 92 |  |  |  |  |  |  | { | 
| 93 | 0 |  |  |  |  |  | $response = $next_seed++; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  | elsif ($request =~ /^CLEAR/) | 
| 97 |  |  |  |  |  |  | { | 
| 98 | 0 |  |  |  |  |  | $clear->(); | 
| 99 | 0 |  |  |  |  |  | $response = "OK"; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  | elsif ($request =~ /^ENQUEUE ((?:\d+,)+)/) | 
| 102 |  |  |  |  |  |  | { | 
| 103 | 0 |  |  |  |  |  | my $nums = $1; | 
| 104 | 0 |  |  |  |  |  | $nums =~ s{,$}{}; | 
| 105 | 0 |  |  |  |  |  | push @queue, split(/,/, $nums); | 
| 106 | 0 |  |  |  |  |  | $response = "OK"; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  | else | 
| 109 |  |  |  |  |  |  | { | 
| 110 | 0 |  |  |  |  |  | $response = "ERROR"; | 
| 111 |  |  |  |  |  |  | } | 
| 112 | 0 |  |  |  |  |  | $conn->print("$response\n"); | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | 1; | 
| 117 |  |  |  |  |  |  | __END__ |