#!/usr/bin/perl

eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;


# Authors: Jeff Parsons, James Edmondson
# Purpose: Setup a Publisher

use Env (ACE_ROOT);
use Env (DDS_ROOT);
use lib "$ACE_ROOT/bin";
use PerlACE::Run_Test;

require  "/export/home/tczar/scripts/scripts.lib";

chdir("/home/tczar/DDS/performance/throughput/NDDS");
system("/export/home/tczar/enable_cores.sh");


# Because we now have to use sudo on our tests, we have to reset some
# environment variables that sudo clears for 'security' reasons
# see the manpage for sudo for details.

$lib='/home/tczar/ACE_wrappers/ace';
$lib.=':/home/tczar/ACE_wrappers/lib'; $lib.=':/home/tczar/ACE_wrappers/TAO/TAO_IDL'; $lib.=':/export/home/tczar/ACE_wrappers/TAO/DDS/lib'; $lib.=':/home/tczar/rti/ndds.4.1c/lib/i86Linux2.6gcc3.4.3'; $lib.=':/export/home/tczar/DDS/Stats';
$lib.=':/export/home/tczar/DDS/performance/throughput/TAO_DDS/TypeNoKeyBounded';$lib.=':/home/tczar/OpenSplice/V2.0beta/x86.linux2.6/lib';

$ENV{'LD_LIBRARY_PATH'} = $lib;



&readSettingsFromArgs();
#&setupNDDSDiscovery(1);
$status = 0;

print "Discovery is " . $ENV{'NDDS_DISCOVERY_PEERS'} . "\n";

PerlACE::add_lib_path('../TypeNoKeyBounded');

# if user wants the number of primer messages or stats messages
# modified, go ahead and change the defaults.

if( $settings{'primer'} ne "" )
{
  $primer_messages = $settings{'primer'};
}
else
{
  $primer_messages=100;
}

if( $settings{'statsmessages'} ne "" )
{
  $stats_messages = $settings{'statsmessages'};
}
else
{
  $stats_messages=10000;
}

$pub_writer_id=0;
$repo_port = 60001;

# Because our settings files no longer allow simple QoS labels in the
# settings files, convert our QoS key/values into simpler ones

$settings{'samples.maxperinstance'} = $settings{'datawriter.resourcelimits.' .
                                    'maxsamplesperinstance'};
$settings{'samples.max'} = $settings{'datawriter.resourcelimits.' .
                                    'maxsamples'};
$settings{'instances.max'} = $settings{'datawriter.resourcelimits.' .
                                    'maxinstances'};
$settings{'reliability'} = $settings{'datawriter.reliability.kind'};
$settings{'durability'} = $settings{'datawriter.durability.kind'};
$settings{'history'} = $settings{'datawriter.history.kind'};
$settings{'history.depth'} = $settings{'datawriter.history.depth'};


# setup the parameter string to the executable.

$parameters = "";
$parameters .= " -pn $primer_messages -sn $stats_messages";

if( $settings{'samples.maxperinstance'} )
{
  $parameters .= " -msi " . $settings{'samples.maxperinstance'};
}

if( $settings{'samples.max'} )
{
  $parameters .= ' -mxs ' . $settings{'samples.max'};
}

if( $settings{'instances.max'} )
{
  $parameters .= ' -mxi ' . $settings{'instances.max'};
}

$parameters .= ' -r ' . $settings{'results'} . '.stats -n ' . $settings{'net'};
$parameters .= ' -q ' . $settings{'qos'};


$settings{'transport'} =~ s/ //g;

@transports = split(/\|/,$settings{'transport'});
$done = 0;

print "Transport is " . $settings{'transport'} . "\n";

while( $done == 0 && @transports > 0 )
{
  $transport = shift(@transports);

  print "Accessing first transport ($transport)...\n";

  if( $transport eq "multicast" || $transport eq "multi-cast"  )
  {
    $parameters .= " -multicast";
    $done = 1;
  }
  elsif( $transport eq "unicast" || $transport eq "uni-cast" )
  {
    $done = 1;
  }
}


$settings{'reliability'} =~ s/ //g;

@reliabilities = split(/\|/,$settings{'reliability'});
$done = 0;
while( $done == 0 && @reliabilities > 0 )
{
  $reliability = shift(@reliabilities);
  if( $reliability eq "reliable" )
  {
    $parameters .= " -reliable";
    $done = 1;
  }
  elsif( $reliability eq "best" || $reliability eq "best-effort" ||
         $reliability eq "best effort"|| $reliability eq "besteffort" )
  {
    $done = 1;
  }
}

$settings{'notify'} =~ s/ //g;

@notifies = split(/\|/,$settings{'notify'});
$done = 0;
while( $done == 0 && @notifies > 0 )
{
  $notify = shift(@notifies);
  if( $notify eq "listener" )
  {
    $done = 1;
  }
  elsif( $notify eq "wait" || $notify eq "wait-based" ||
         $notify eq "waitbased" )
  {
#    $parameters .= ' -wait';
    $done = 1;
  }
}

$settings{'history'} =~ s/ //g;

@histories = split(/\|/,$settings{'history'});
$done = 0;
while( $done == 0 && @histories > 0 )
{
  $history = shift(@histories);
  if( $history eq "keeplast" || $history eq "last" )
  {
    $done = 1;
  }
  elsif( $history eq "keepall" || $history eq "all" )
  {
    $parameters .= ' -keep_all';
    $done = 1;
  }
}

if( $settings{'history.depth'} ne "" )
{
  $parameters .= ' -depth ' . $settings{'history.depth'};
}




# log all Publisher parameters and settings

@dataSizes = &generateDataSizes($settings{'minsize'},$settings{'maxsize'});

open( FILE, '>' . $settings{'results'} . '.params' );
  print FILE $parameters . "\n";
close(FILE);

&writeSettings($settings{'results'} . '.settings');

#unlink($settings{'results'} . '.exists');
#exit(0);

$testDirectory = &getTestDirectory();
$id = &getIndividualId();

#unlink($settings{'results'} . '.exists');
#print "Settings and Parameters have been written... Exiting";
#exit 0;

foreach $data_size (@dataSizes)
{

  $exe = "objs/i86Linux2.6gcc3.4.3" .
         "/publisher";
  $outfile = $testDirectory . "/pub0." . "strace.$data_size";

  &touch( $settings{'results'} . '.' . $data_size );

#  system("strace -o pub0.strace.$data_size $exe " .
#         "$parameters -Size $data_size -top test_topic_$data_size" .
#         " -num_of_sub " . $settings{'subscribers'} );

  $Publisher = new PerlACE::Process ( 
     $exe , $parameters  . " -Size $data_size -top test_topic_$data_size"
     . " -role $id" 
      );

  print $Publisher->CommandLine(), "\n";

  $Publisher->Spawn ();

  $PublisherResult = $Publisher->WaitKill (2400);

  if ($PublisherResult != 0) {
      print STDERR "ERROR: publisher returned $PublisherResult \n";
      $status = 1;
  }

  unlink( $settings{'results'} . '.' . $data_size );

  # give the subscribers time to restart and get read for the next test.

  $done = 0;

  while( $done == 0 )
  {
    $done = 1;
    for( $i = 0; $i < $settings{'subscribers'}; $i++)
    {
      if( -e $testDirectory . "/sub$i.$data_size" )
      {
        $done = 0;
      }
    }
  }

  print "Finished $data_size... Sleeping for 3 seconds before next round.\n";

  sleep(3);

  $Publisher = "";
}

unlink($settings{'results'} . '.exists');

exit $status;

