Advertisement
DRVTiny

Untitled

Mar 5th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.84 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use 5.16.1;
  3. use strict;
  4. use warnings;
  5. use constant {
  6.     DFLT_SER_TAG        => 'CB',
  7.     N_PERC_RED          =>  0.1,
  8. };
  9. use subs qw(store_data);
  10.  
  11. use File::Slurp qw(write_file);
  12. use File::Map qw(map_file);
  13. use AnyEvent;
  14. use Try::Tiny;
  15. use Tag::DeCoder;
  16.  
  17. my $alpha=join('' => 'a'..'z','A'..'Z','0'..'9');
  18. my %hsh=map {'t'.$_ => {'triggerid'=>$_, 'status'=>(int(rand 25) & 1), 'lostfunk'=>rand(),'svc-path'=>[map [map int(rand 10000), 2..(int(rand 5)+2)], 0..int(rand 5)]}} 1..10000;
  19.  
  20. my $pthMapFile='/tmp/test.mmap.'.$$;
  21.  
  22. # Initialy write data to the file that will be mmap'ed later
  23. store_data($pthMapFile, \%hsh);
  24.  
  25. # Save mmaped file name
  26. write_file '/tmp/mmaped_file.pth', $pthMapFile;
  27. say "Mapping to: $pthMapFile";
  28.  
  29. # Actually create mapping from file to $strMap string variable
  30. map_file my $strMap, $pthMapFile, '+<';
  31.  
  32.  
  33. my $cv=AnyEvent->condvar;
  34. my $aeh=AnyEvent->timer('after'=>0, 'interval'=>1, 'cb' => sub {
  35. # Doi it immediately and repeat every 1 sec...
  36.     try {
  37.         say scalar(localtime);
  38.         @{$hsh{'t'.(int(rand 10000)+1)}}{qw/lostfunk status description/} = (
  39.             rand(),
  40.             int(rand 25) & 1,
  41.             join('' => map substr($alpha, int(rand length($alpha)), 1), 100..(100+int(rand 156)))
  42.         ) for 7..int(rand 28)+7;
  43.         store_data(\$strMap, \%hsh);
  44.     } catch {
  45.         say "ERROR: $_";
  46.     }
  47. });
  48. $cv->recv;
  49.  
  50. sub store_data ($$$) {
  51.     my $where2store = $_[0];
  52.     my $sData = encodeByTag($_[2] // DFLT_SER_TAG() => $_[1]);
  53.     use bytes;
  54.     unless (ref $where2store) {
  55.         open my $fh, '>', $where2store;
  56.         print $fh pack('V', length $sData), $sData, "\x00" x int(length($sData)*N_PERC_RED*0.01);
  57.         close $fh;
  58.     } else {
  59.         substr(${$where2store}, 0, length($sData)+4) = pack('V', length $sData) . $sData
  60.     }
  61.     no bytes;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement