| 1 | # Copyright 2008, 2009, 2010, 2011, 2013 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::SubprocessMain; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Storable; | ||
| 22 | use IO::Handle; | ||
| 23 | |||
| 24 | use App::Chart::Download; | ||
| 25 | |||
| 26 | # set this to 1 for development debugging prints | ||
| 27 | use constant { DEBUG => 0, | ||
| 28 | DEBUG_TTY_FILENAME => '/dev/tty' }; | ||
| 29 | |||
| 30 | sub main { | ||
| 31 | my ($class) = @_; | ||
| 32 | ## no critic (ProhibitExplicitStdin, ProhibitExit) | ||
| 33 | |||
| 34 | # subprocess unbuffered and utf8 | ||
| 35 | binmode (STDIN, ':raw') or die; | ||
| 36 | *STDOUT->autoflush(1); | ||
| 37 | *STDERR->autoflush(1); | ||
| 38 | |||
| 39 | if (DEBUG) { | ||
| 40 | ## no critic (ProhibitBarewordFileHandles RequireBriefOpen) | ||
| 41 | open TTY, '>', DEBUG_TTY_FILENAME or die; | ||
| 42 | print TTY "SubprocessMain\n"; | ||
| 43 | *TTY->autoflush(1); | ||
| 44 | } | ||
| 45 | |||
| 46 | for (;;) { | ||
| 47 | my $len = <STDIN>; | ||
| 48 | if (DEBUG) { print "SubprocessMain: length $len\n"; } | ||
| 49 | my $data; | ||
| 50 | my $got = read STDIN, $data, $len; | ||
| 51 | if (! defined $got) { | ||
| 52 | print STDERR "Read error: ",Glib::strerror($!),"\n"; | ||
| 53 | exit 1; | ||
| 54 | } | ||
| 55 | if ($got != $len) { | ||
| 56 | print STDERR "Read $got wanted $len\n"; | ||
| 57 | exit 1; | ||
| 58 | } | ||
| 59 | $data = Storable::thaw ($data); | ||
| 60 | |||
| 61 | if (DEBUG) { require Data::Dumper; | ||
| 62 | print "SubprocessMain: ",Data::Dumper::Dumper($data); | ||
| 63 | print TTY "SubprocessMain ", Data::Dumper::Dumper($data); } | ||
| 64 | if (! $data) { last; } | ||
| 65 | |||
| 66 | my ($jobclass, @args) = @$data; | ||
| 67 | |||
| 68 | # use Module::Load; | ||
| 69 | # Module::Load::load ($jobclass); | ||
| 70 | # $jobclass->run (@args); | ||
| 71 | |||
| 72 | if ($jobclass eq 'intraday') { | ||
| 73 | require App::Chart::Intraday; | ||
| 74 | App::Chart::Intraday->command_line_download (\@args); | ||
| 75 | |||
| 76 | } elsif ($jobclass eq 'latest') { | ||
| 77 | require App::Chart::LatestHandler; | ||
| 78 | App::Chart::LatestHandler->download (@args); | ||
| 79 | |||
| 80 | } elsif ($jobclass eq 'download') { | ||
| 81 | require App::Chart::Download; | ||
| 82 | if ($args[0] =~ /^--/p) { | ||
| 83 | my $key = ${^POSTMATCH}; | ||
| 84 | require App::Chart::Gtk2::Symlist; | ||
| 85 | $args[0] = App::Chart::Gtk2::Symlist->new_from_key ($key); | ||
| 86 | } | ||
| 87 | App::Chart::Download->command_line_download ('subprocess', \@args); | ||
| 88 | |||
| 89 | } elsif ($jobclass eq 'vacuum') { | ||
| 90 | require App::Chart::Vacuum; | ||
| 91 | App::Chart::Vacuum::vacuum (@args); | ||
| 92 | |||
| 93 | } elsif ($jobclass eq 'exit') { | ||
| 94 | last; | ||
| 95 | |||
| 96 | } else { | ||
| 97 | print "Unknown job type $_\n"; | ||
| 98 | } | ||
| 99 | # App::Chart::Download::status ('Foo'); | ||
| 100 | # sleep (2); | ||
| 101 | |||
| 102 | App::Chart::Download::status ('Idle'); | ||
| 103 | *STDOUT->flush; | ||
| 104 | } | ||
| 105 | |||
| 106 | if (DEBUG) { print TTY "SubprocessMain exit\n"; } | ||
| 107 | exit 0; | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; | ||
| 111 | __END__ |