#!/usr/bin/perl

use strict;
use warnings;

use IO::Socket::INET;

my $num_args = $#ARGV + 1;
if ( ($num_args == 0) or
     (($num_args == 1) && (($ARGV[0] eq "--help") or ($ARGV[0] eq "-h"))) )
{
    showusage();
    exit;
}

my $argumentstring = "";
my $ip = "127.0.0.1";
my $port = "9900";

for (my $argnum=0; $argnum <= $#ARGV; $argnum++) {
    if ($ARGV[$argnum] eq "-ip") {
	die "No argument after -ip\n" unless $argnum+1<=$#ARGV;
        $argnum = $argnum+1;
	$ip = $ARGV[$argnum];
	if ($ip =~ s/:(\d)$//) {
		$port = $1;
	}
    } elsif ($ARGV[$argnum] eq "-port") {
	die "No argument after -port\n" unless $argnum+1<=$#ARGV;
        $argnum = $argnum+1;
	$port = $ARGV[$argnum];
    } else {
	$argumentstring .= "$ARGV[$argnum] ";
    }
}

# create a connecting socket
my $socket = new IO::Socket::INET (
    PeerHost => $ip,
    PeerPort => $port,
    Proto => 'tcp',
);
die "Cannot connect to the rtpengine $!\n" unless $socket;

$socket->autoflush(1);

#set send/recv timeout so script doesn't hang when rtpengine doesn't interact
setsockopt($socket, SOL_SOCKET, SO_SNDTIMEO, pack('L!L!', 3, 0) ) or die $!;
setsockopt($socket, SOL_SOCKET, SO_RCVTIMEO, pack('L!L!', 3, 0) ) or die $!;

$argumentstring = trim($argumentstring);
my $size = $socket->send($argumentstring);
 
# notify server that request has been sent
shutdown($socket, 1);
 
# receive a response of up to 10MB
my $response = "";

do {
   $response = "";
   $socket->recv($response, 1024*1024*10);
   print $response; 
} while ( not $response eq "");

$socket->close();

sub showusage {
    print "\n";
    print "    rtpengine-ctl [ -ip <ipaddress>[:<port>] -port <port> ] <command>\n";
    print "\n";
    print "    Supported commands are:\n";
    print "\n";
    print "    list [ numsessions | maxsessions | maxopenfiles\n";
    print "          | sessions [ <callid> | all | own | foreign ] | totals | loglevel ]\n";
    print "         numsessions           : print the number of sessions\n";
    print "         maxsessions           : print the number of allowed sessions\n";
    print "         maxopenfiles          : print the number of allowed open files\n";
    print "         sessions <callid>     : print detail about one session\n";
    print "         sessions all          : print one-liner all sessions information\n";
    print "         sessions own          : print one-liner own sessions information\n";
    print "         sessions foreign      : print one-liner foreign sessions information\n";
    print "         totals                : print total statistics\n";
    print "         timeout               : print timeout parameters\n";
    print "         loglevel              : print current log level\n";
    print "\n";
    print "    terminate [ <callid> | all | own | foreign ]\n";
    print "         <callid>              : session is immediately terminated\n";
    print "         all                   : terminates all current sessions\n";
    print "         own                   : terminates own current sessions\n";
    print "         foreign               : terminates foreign current sessions\n";
    print "\n";
    print "    set [ maxsessions <int> | maxopenfiles <uint> | timeout <uint>\n";
    print "          | silent_timeout <uint> | final_timeout <uint> | loglevel <uint> ]\n";
    print "         maxsessions  <int>    : set the max nr of allowed sessions\n";
    print "         maxopenfiles <uint>   : set the max nr of allowed open files\n";
    print "         timeout <uint>        : set the --timeout parameter \n";
    print "         silenttimeout <uint>  : set the --silent-timeout parameter \n";
    print "         finaltimeout <uint>   : set the --final-timeout parameter \n";
    print "         loglevel <uint>       : set the log level to new value (1-7)\n";
    print "\n";
    print "    ksadd [ keyspace <uint>]\n";
    print "         keyspace <uint>       : subscribe to 'keyspace' database\n";
    print "\n";
    print "    ksrm [ keyspace <uint>]\n";
    print "         keyspace <uint>       : unsubscribe to 'keyspace' database\n";
    print "                               : remove all foreign calls for that 'keyspace'\n";
    print "\n";
    print "    kslist                     : print all currently subscribed keyspaces\n";
    print "\n";
    print "\n";
    print "    Return Value:\n";
    print "    0 on success with output from server side, other values for failure.\n";
    print "\n";
}

sub  trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
