#!/usr/local/bin/perl -I/usr/local/lib/perl
$rcsid = "$Id: verify-cci,v 1.2 1994/12/15 01:23:49 edburns Exp edburns $";
# test all aspects of the cci interface, to verify correctness.
# This program is pretty linear, but its intent is not to be clever.
# Ed Burns <edburns@ncsa.uiuc.edu>
# Mail questions to cci-app@ncsa.uiuc.edu.
#
require "getopts.pl";

$opt_a = 3;			# default number of items in cbd
$opt_i = "128.123.122.20";	# default ip
$opt_p = 6182;			# default port
$opt_u = "http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/cci-perl.html";
$opt_r = "/signet/elmo.html";	# default relative URI

&Getopts('a:i:p:u:r:oqschgz');

if ($opt_h) {
    die "usage: verify-cci [-a numargs] [-c test constants] [-g test &MCCIGet] \n [-h duh!] [-i ip] [-o test &MCCIGet with &MCCISendOutput] [-p port] \n [-q test port-query functions] [-r relative URI for &MCCIGet] \n [-s test &MCCISendAnchor] [-u default URI for &MCCIGet]\n [-z test everything possible]";
}

if ($opt_z) {
    $opt_c = $opt_q = $opt_s = $opt_g = $opt_o = 1;
}

print "This script tests the mosaic cci perl interface.\n\n";

if ($opt_c) {
    print "Testing Constants:\n";
    print "\$MCCI_OK is $MCCI_OK.\n";
    print "\$MCCI_FAIL is $MCCI_FAIL. \n";
    print "\$MCCI_OUTOFMEMORY is $MCCI_OUTOFMEMORY.\n";
    print "\$MCCI_RELATIVE is $MCCI_RELATIVE.\n";
    print "\$MCCI_DEFAULT is $MCCI_DEFAULT.\n";
    print "\$MCCI_OUTPUT_NONE is $MCCI_OUTPUT_NONE.\n";
    print "\$MCCI_OUTPUT_CURRENT is $MCCI_OUTPUT_CURRENT.\n";
    print "\$MCCI_OUTPUT_NEW is $MCCI_OUTPUT_NEW.\n";
    print "\$MCCI_ABSOLUTE is $MCCI_ABSOLUTE.\n";
    print "\$MCCI_RELATIVE is $MCCI_RELATIVE.\n";
}

$init = &MCCIInitialize;
print "&MCCIInitialize returned = $init.\n";

for ($i = 0; $i < $opt_a; $i++) {
  $cbd[$i]	= "arg $i";
}

$callBack = "perlCB";

print "&MCCIConnect($opt_i, $opt_p, $callBack, @cbd) returned:\n";
$CCIPort = &MCCIConnect($opt_i, $opt_p, $callBack, @cbd);
print "    $CCIPort.\n";

if ($opt_q && &MCCIIsConnected($CCIPort)) {
    print "Testing port query functions:\n";
    $socket = &MCCIGetSocketDescriptor($CCIPort);
    print "&MCCIGetSocketDescriptor($CCIPort) returned $socket.\n";

    $connected = &MCCIIsConnected($CCIPort);
    print "&MCCIIsConnected($CCIPort) returned $connected.\n";

    $poll = &MCCIPoll($CCIPort);
    print "&MCCIPoll($CCIPort) returned $poll.\n";

    $input = &MCCIIsThereInput($CCIPort);
    print "&MCCIIsThereInput($CCIPort) returned $input.\n";
}

if ($opt_s && &MCCIIsConnected($CCIPort)) {
    print "Testing SendAnchor functions:\n";
    $on = 1;
    $acb = "acb";
    $arc = &MCCISendAnchor($CCIPort,$on,$acb,@cbd);
    print "&MCCISendAnchor($CCIPort,$on,$acb,@cbd) returned: $arc.\n";
    do {
	print "\n\n>>>Activate an anchor in the Mosaic cci server.<<<\n";
	for ($i = 0; $i < 5; $i++) {
	    if (&MCCIIsThereInput($CCIPort)) {
		print "polling for an anchor...\n";
		$pollval = &MCCIPoll($CCIPort);
		print "&MCCIPoll($CCIPort) returned $pollval.\n";
	    }
	    sleep(1);
	}
	print ">>>You clicked no anchor in 6 seconds, Enter 'c' to continue polling again, any other key to continue this program.<<<--->";
	$_ = <STDIN>;
	chop;	
    } until $_ ne "c";
    while (&MCCIIsThereInput($CCIPort)) {
      print "polling for remaining anchors...\n";
      $pollval = &MCCIPoll($CCIPort);
      print "&MCCIPoll($CCIPort) returned $pollval.\n";
    }
    $on = 0;
    $arc = &MCCISendAnchor($CCIPort,$on,$acb,@cbd);
    print "&MCCISendAnchor($CCIPort,$on,$acb,@cbd) returned.\n   $arc\n";
}

if ($opt_g && &MCCIIsConnected($CCIPort)) {
    print "testing &MCCIGet:\n";

# testing absolute URI
    print "Mosaic is getting $opt_u, output to browser.\n";
    $getval = &MCCIGet($CCIPort,$opt_u,$MCCI_DEFAULT,$MCCI_DEFAULT,$NULL);
    print "&MCCIGet($CCIPort,$opt_u,\$MCCI_DEFAULT,\$MCCI_DEFAULT,\$NULL) returned $getval.\n";

#    print "Mosaic is getting $opt_u, hiding output to browser.\n";
#    $getval = &MCCIGet($CCIPort,$opt_u,$MCCI_OUTPUT_NONE,$MCCI_DEFAULT,$NULL);
#    print "&MCCIGet($CCIPort,$opt_u,\$MCCI_OUTPUT_NONE,\$MCCI_DEFAULT,\$NULL) returned $getval.\n";

    print "Mosaic is getting $opt_u, in a new browser.\n";
    $getval = &MCCIGet($CCIPort,$opt_u,$MCCI_OUTPUT_NEW,$MCCI_DEFAULT,$NULL);
    print "&MCCIGet($CCIPort,$opt_u,\$MCCI_OUTPUT_NEW,\$MCCI_DEFAULT,\$NULL) returned $getval.\n";

# testing relative URI
    print "Mosaic is getting $opt_r, output to browser.\n";
    $getval = &MCCIGet($CCIPort,$opt_r,$MCCI_DEFAULT,$MCCI_RELATIVE,$NULL);
    print "&MCCIGet($CCIPort,$opt_r,\$MCCI_DEFAULT,\$MCCI_RELATIVE,\$NULL) returned $getval.\n";

#    print "Mosaic is getting $opt_r, hiding output to browser.\n";
#    $getval = &MCCIGet($CCIPort,$opt_r,$MCCI_OUTPUT_NONE,$MCCI_RELATIVE,$NULL);
#    print "&MCCIGet($CCIPort,$opt_r,\$MCCI_OUTPUT_NONE,\$MCCI_RELATIVE,\$NULL) returned $getval.\n";

    print "Mosaic is getting $opt_u, in a new browser.\n";
    $getval = &MCCIGet($CCIPort,$opt_r,$MCCI_OUTPUT_NEW,$MCCI_RELATIVE,$NULL);
    print "&MCCIGet($CCIPort,$opt_u,\$MCCI_OUTPUT_NEW,\$MCCI_RELATIVE,\$NULL) returned $getval.\n";
}

if ($opt_o && &MCCIIsConnected($CCIPort)) {
    print "testing &MCCIGet with &MCCISendOutput:\n";
    $mimeType = "text/html";
    $socb = "socb";
    $socval = &MCCISendOutput($CCIPort, $mimeType, $socb, @cbd);
    print "&MCCISendOutput($CCIPort, \$mimeType, \$socb, \@cbd) returned $socval.\n";

    print "Mosaic is getting $opt_u, output to this program.\n";
    $getval = &MCCIGet($CCIPort,$opt_u,$MCCI_DEFAULT,$MCCI_DEFAULT,$NULL);
    print "&MCCIGet($CCIPort,$opt_u,\$MCCI_DEFAULT,\$MCCI_DEFAULT,\$NULL) returned $getval.\n";

    $sostopval = &MCCISendOutputStop($CCIPort, $mimeType);
    print "&MCCISendOutputStop($CCIPort, $mimeType) returned $sostopval.\n";

    print "Testing to see if &MCCIGet still gets to browser.\n";
    $getval = &MCCIGet($CCIPort,$opt_u,$MCCI_DEFAULT,$MCCI_DEFAULT,$NULL);
    print "&MCCIGet($CCIPort,$opt_u,\$MCCI_DEFAULT,\$MCCI_DEFAULT,\$NULL) returned $getval.\n";
}

$closeval = &MCCIDisconnect($CCIPort);
print "&MCCIDisconnect($CCIPort) returned $closeval.\n";
exit;

sub perlCB {
    local(@data) = @_;
    foreach $arg (@data) {
      print "disconnect callback worked, data is $arg\n";
    }
}

sub acb { 
	local(@data) = @_;
	print "your URL is $data[0]\n";
	foreach $arg (@data) {
  	print "anchor callback worked, data is $arg\n";
	}
}

sub socb {
	local($mt, $data, $len, @socbd) = @_;
	print "mimeType = $mt\n";
	print "len = $len\n";
        print "data = $data\n";
	foreach $arg (@socbd) {
		print "sendOutputCallBack worked, data is $arg\n";
  }
}
