From: Brian Elliott Finley on behalf of Brian Elliott Finley
Sent: Sat 31/12/2005 15:15
To: Bernard Li
Cc: sisuite-devel@lists.sourceforge.net
Subject: Re: Help with flamethrowerd
Thus spake Bernard Li ([EMAIL PROTECTED]):
>Hey
Brian:
>
>I can check the code into trunk, but I think my focus
right now would be to get this running with SystemImager, so I don't know if I
will have time to test to see if the transport works with standalone
flamethrower... is that going to be a problem?
Not initially.
I don't anticipate doing a release from the flamethrower
trunk anytime soon,
so I'm not too worried about it.
Also, I would expect that your BT mods
to flamethrower are feature
additions, and should not break existing
functionality, even during
development. But even if it does, go ahead
and use trunk for the
flamethrower stuff in this
case.
>Regarding my issue, it appears to be caused by the fact
that I have a bunch of system() calls within maketorrent() - I don't think that
plays well with fork/cast... I'll have to look deeper into the code - if
you have any suggestions, please let me know.
Will take a look after you
check it in.
Cheers,
-Brian
>
>Thanks,
>
>Bernard
>
>________________________________
>
>From:
Brian Elliott Finley on behalf of Brian Elliott Finley
>Sent: Sat
31/12/2005 11:19
>To: Bernard Li
>Subject: Re: Help with
flamethrowerd
>
>
>
>Go ahead and check your code into
the flamethrower trunk.
>
>I'm betting that what you're getting
stuck on is a perl fork related concept.
>
>I've attached a very
simple forking perl daemon as an example.
>
>Again, please send
these to the -devel list too. Others may have good
>feedback on
things like this also.
>
>Cheers,
-Brian
>
>
>
>Thus spake Bernard Li
([EMAIL PROTECTED]):
>>Hi Brian:
>>
>>I'm integrating the
BitTorrent transport into Flamethrower and could use your help explaining the
cast() subrountine in flamethrowerd.
>>
>>Instead of running
"build_udp_sender_cmd", I created a new subrountine called
"build_bittorrent_cmd" which calls the bittorrent-console command. This
command is used to seed the torrent and is expected to be started when
flamethrowerd is started and not stopped until flamethrowerd is
stopped.
>>
>>I have added a call to subroutine maketorrent()
which creates the torrent inside the cast subroutine which is called prior to
build_bittorrent_cmd().
>>
>>Here's the code snippet from
cast():
>>
>> # Make torrent if using the
BitTorrent transport
>> if ($transport eq
"bittorrent") {
>>
maketorrent($module);
>>
}
>> # Fork and cast
>> if
($parent_pid = fork) {
>>
record_pid($parent_file, $parent_pid);
>> } elsif
(defined $parent_pid) { # send the forked child
off
>> setsid or die "Can't
start a new session: $!";
>>
print "casting $module\n"
if($debug);
>> my $cmd =
"";
>> if ($transport eq
"bittorrent")
{
>>
$cmd =
build_bittorrent_cmd($module);
>>
} else
{
>>
$cmd =
build_udp_sender_cmd($module);
>>
}
>>
>>It basically loops through each module, making the
torrent and seeding it, however, it gets stuck in the middle until I kill the
process of the previous module (the pid is recorded in
/var/state/systemimager/flamethrower).
>>
>>Perhaps if I
understand the cast() subroutine better, then I will be able to figure this
out.
>>
>>Thanks,
>>
>>Bernard
>
>--
>Brian
Elliott Finley
>Mobile:
630.631.6621
>
>
--
Brian Elliott Finley
Mobile:
630.631.6621
Index: flamethrowerd =================================================================== --- flamethrowerd (revision 53) +++ flamethrowerd (working copy) @@ -111,11 +111,23 @@ exit 1; } -my $file = "udp-sender"; -unless( Flamethrower->which($file,$ENV{PATH}) ){ - print ("Couldn't find $file in path: $ENV{PATH}\n"); - print ("Please install udp-sender, then try again.\n"); - exit 1; +my $pname = ""; +my $transport = $ft_config->transport(); + +if ($transport eq "bittorrent") { + $pname = "bittorrent-console"; + unless( Flamethrower->which($pname,$ENV{PATH}) ){ + print ("Couldn't find $pname in path: $ENV{PATH}\n"); + print ("Please install $pname, then try again.\n"); + exit 1; + } +} else { + $pname = "udp-sender"; + unless( Flamethrower->which($pname,$ENV{PATH}) ){ + print ("Couldn't find $pname in path: $ENV{PATH}\n"); + print ("Please install $pname, then try again.\n"); + exit 1; + } } &daemonize; @@ -335,6 +347,68 @@ return $cmd; } +# Usage: my $cmd = build_bittorrent_cmd($module); +sub build_bittorrent_cmd { + + my $module = shift; + my $tardir = "/tmp"; + + ############################################################################ + # + # Set vars for all options + # + ############################################################################ + + ############################################################################ + # + # Global settings + # + my $log; + if ( $ft_config->get("log") ) { + $log = " > " . $ft_config->get("log") . ".$module"; + } + + my $bt_max_upload_rate; + if ( $ft_config->get("bt_max_upload_rate") ) { + $bt_max_upload_rate = " --max_upload_rate " . $ft_config->get("bt_max_upload_rate"); + } + # + ############################################################################ + + + ############################################################################ + # + # Module specific overrides + # + my $dir; + if ( $ft_config->get("${module}_dir") ) { + $dir = $ft_config->get("${module}_dir"); + } + + my $portbase; + if ( $ft_config->get("${module}_portbase") ) { + $portbase = " --portbase " . $ft_config->get("${module}_portbase"); + } + # + ############################################################################ + + # Build command + # + # GNU tar opts explained: + # -B, --read-full-records / reblock as we read (for reading 4.2BSD pipes) + # + # -S, --sparse / handle sparse files efficiently + # + my $cmd = qq(bittorrent-console --save_as $tardir/${module}.tar http://localhost/torrents/${module}.torrent); + #my $cmd = qq(bittorrent-console --save_as $tardir/${module}.tar http://localhost/torrents/${module}.torrent ); + $cmd .= $bt_max_upload_rate if(defined $bt_max_upload_rate); + #$cmd .= $slice_size if(defined $slice_size); + $cmd .= $log if(defined $log); + #$cmd .= " &"; + + return $cmd; +} + # Usage: update_directory(); sub update_directory { @@ -553,13 +627,18 @@ my $state_dir = shift; my $parent_file = "${state_dir}/flamethrowerd.${module}.pid"; - my $child_file = "${state_dir}/flamethrowerd.${module}.udp-sender.pid"; + my $child_file = "${state_dir}/flamethrowerd.${module}.$pname.pid"; my $file; my $parent_pid; my $child_pid; print "cast($module, $state_dir)\n" if($debug); + + # Make torrent if using the BitTorrent transport + if ($transport eq "bittorrent") { + maketorrent($module); + } # Fork and cast if ($parent_pid = fork) { @@ -570,7 +649,12 @@ print "casting $module\n" if($debug); - my $cmd = build_udp_sender_cmd($module); + my $cmd = ""; + if ($transport eq "bittorrent") { + $cmd = build_bittorrent_cmd($module); + } else { + $cmd = build_udp_sender_cmd($module); + } print "cmd $cmd\n" if($debug); my $child_pid = open(SENDER, "$cmd|") or die "Couldn't fork: $cmd"; @@ -595,6 +679,35 @@ } } +# Usage: maketorrent($module); +sub maketorrent { + my $module = shift; + my $port = "2222"; + my $cmd = ""; + my $docroot = "/var/www/html"; + my $tardir = "/tmp"; + + my $ip = "192.168.0.2"; + + my $dir; + if ( $ft_config->get("${module}_dir") ) { + $dir = $ft_config->get("${module}_dir"); + } + + print "maketorrent $module\n" if($debug); + + # First, need to tar up the module + $cmd = "cd $dir && tar -cf $tardir/$module.tar *"; + + # Make torrent file using maketorrent + $cmd .= " && maketorrent-console http://$ip:$port/announce $tardir/$module.tar"; + + # Move resulting torrent to document root's torrents directory + $cmd .= " && mv $tardir/$module.tar.torrent $docroot/torrents/$module.torrent"; + print "cmd $cmd\n" if($debug); + system($cmd); +} + # Usage: read_config_file($file); sub read_config_file { my $file = shift;