#!/usr/bin/perl

use Getopt::Std;
use SOAP::Lite;
#for debugging use the below
#use SOAP::Lite +trace => qw(all);
use Config::IniFiles;
use threads;
use strict;
use warnings;

my $nb_threads = $ARGV[0];
my $nb_requests_per_thread = $ARGV[1];

my $date;

my $cfg = new Config::IniFiles( -file => "soap-server.conf" );
my $ADMIN_USER = $cfg->val('server','user');
my $ADMIN_PWD = $cfg->val('server','password');
my $PF_HOST = $cfg->val('server','host');

# radius call parameters
my $nas_port_type  = "Wireless-802.11";
my $switch_ip      = "192.168.1.60";
my $eap_type       = 0;
my $mac            = "00:13:ce:58:42:e2";
my $port           = 12345;
my $user_name      = "aabbccddeeff";
my $ssid           = "Inverse-Invite";

die "SOAP calls: please specify the number of threads and the number of requests per thread on the command line" if (!($nb_threads && $nb_requests_per_thread));

print "about to launch $nb_threads threads sending each $nb_requests_per_thread SOAP calls...\n";

# worker launcher
my @threads;
for (my $i = 0; $i<$nb_threads; $i++) {

  # create the thread
  push @threads, threads->create( \&soap_requests, $i);
}

# wait for everyone
foreach my $thread (@threads) {
  $thread->join();
}

sub soap_requests {

  my ($tid) = @_;

  for(my $i=0; $i<$nb_requests_per_thread; $i++) {
  
    $date = time;
  
    print "thread $tid connection #$i: about to launch SOAP call\n";
  
    eval {
      my $soap = new SOAP::Lite(
        uri => 'http://www.packetfence.org/PFAPI',
        proxy => 'https://' . $ADMIN_USER . ':' . $ADMIN_PWD . '@' . $PF_HOST . '/webapi'
      );
      my $result = $soap->radius_authorize($nas_port_type, $switch_ip, $eap_type, $mac, $port, $user_name, $ssid);
      if ($result->fault) {
        print ("warning thread $tid connection #$i radius call failed: " . $result->faultcode . " - " . $result->faultstring . " - " . $result->faultdetail."\n");
      } else {
        # syslog("info", "success");
      }
    };
    if ($@) {
      print ("warning thread $tid connection #$i to $PF_HOST with username $ADMIN_USER was NOT successful: $@\n");
      next;
    }
  
  }
}

=head1 AUTHOR

Inverse inc. <info@inverse.ca>

=head1 COPYRIGHT

Copyright (C) 2005-2019 Inverse inc.

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.

=cut

