#! /usr/local/bin/perl -w

# Client program for COPE object implementation examples
# Assumes it will get the stringified IOR for the server's Supervisor
# object on the command line

use lib '.';

use COPE::CORBA::ORB;
use authenticateIdl::AuthServer;
use serverIdl::Server;
use clientIdl::BlasterCallback_impl;
use Experimental::Exception;

print "\nIs xmlBlaster server running?\n\n";

my $orb = CORBA::ORB_init();


#--------------- find server object reference IOR -------
$ARGV[0] or die "Usage: $0 <ior_string>";
my $authServer = $orb->string_to_object($ARGV[0]);
$authServer = authenticateIdl::AuthServer->_narrow($authServer);


#--------------- create my BlasterCallback server ----
my $boa = $orb->BOA_init();
my $callback = clientIdl::BlasterCallback_impl->new();
my $callback_ref = $orb->object_to_string($callback);
print "BlasterCallback IOR:\n";
print $callback_ref, "\n\n";


#-------------- login() to AuthServer_obj ---------
my $loginQos = "<qos><callback type='IOR'>" . $callback_ref . "</callback></qos>";
my $loginName = "Perl-Heidi";
my $xmlBlaster = $authServer->login($loginName, "secret", $loginQos);
print "login($loginName) successful\n\n";


#-------------- publish() a message -------------
my $xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n";
$xmlKey .= "<key oid=''>\n";
$xmlKey .= "   <AGENT id='192.168.124.10' subId='1' type='generic'>\n";
$xmlKey .= "      <DRIVER id='FileProof' pollingFreq='10'>\n";
$xmlKey .= "      </DRIVER>\n";
$xmlKey .= "   </AGENT>\n";
$xmlKey .= "</key>\n";
print "publish() of xmlKey=\n$xmlKey";

my $content = "Hello xmlBlaster, i'm a Perl client";

#$message = new serverIdl::MsgUnit xmlKey => $xmlKey, content => $content, qos => $qos;
my $qos = "";
$message = {xmlKey => $xmlKey, content => $content, qos => $qos};
my $publishRetQos = $xmlBlaster->publish($message);

print "Successful published message returned QoS=$publishRetQos\n\n";


#-------------- subscribe() to the previous message OID -------
print "Subscribing using the exact oid=$publishOid ...\n";
$xmlKey =  "<?xml version='1.0' encoding='ISO-8859-1' ?>\n";
$xmlKey .= "<key oid='" . $publishOid . "'>\n";
$xmlKey .= "</key>";
$qualityOfService = "<qos></qos>";

$xmlBlaster->subscribe($xmlKey, $qualityOfService);
print "Subscribed to '$publishOid' ...\n\n";


# Go into the BOA's event loop waiting for and processing requests
$boa->impl_is_ready();

