#!/usr/bin/perl -w

use Frontier::Daemon;
use Frontier::Client;
use xmlBlaster::Exception ;
use xmlBlaster::XmlBlaster ;
use xmlBlaster::MessageUnit ;
use xmlBlaster::EraseRetQos ;


my $start_port=9095;
my $max_clients=4;

my $login;
my $count=0;

sub do_update {
	print "GOT AN UPDATE (CLIENT $login)\n";
	return "<qos><state>OK</state></qos>";
}

sub do_ping {
	print "got ping (CLIENT $login)\n";
	return "<qos><state>OK</state></qos>";
}

sub fork_server($) {
	my ($port) = @_;

	$login="dk:$port";

	my $pid=fork();
	if ($pid) {
		# Parent
		return;
	}

	Frontier::Daemon->new(
		    LocalPort => $port,
		    methods => {
			        'update'   => \&do_update,
			        'ping'   => \&do_ping
		    });
}

my $server_url =  $ARGV[0];

#
# Create a bunch of separate callback servers,
# then log them all in.
#
# Sorry - no way to stop them yet - kill them
# by hand for now.
#
for (my $x=0; $x < $max_clients; $x++) {
	print "Starting callback server client $x\n";
	fork_server($start_port + $x);
}

sleep(2);

# Now, log them all in
for (my $x=0; $x < $max_clients; $x++) {

	my $p=$start_port+$x;
	my $local_url="http://127.0.0.1:$p/RPC2";

	my $server = Frontier::Client->new(url => $server_url);
	print "Connected to xmlBlaster server on $server_url \n";

	# Call the remote server and get our result.
	my $sessionId = $server->call('authenticate.login', "dk$x", "dk$x",
			"<qos><callback type='XML-RPC'>$local_url</callback>".
			"<local>false</local></qos>", "");
	print "\nLogin success client dk$x with sessionId=$sessionId \n";

	$server->call('xmlBlaster.subscribe',
			$sessionId,
			"<key oid='' queryType='XPATH'>//service</key>",
			"<qos><duplicateUpdates>false</duplicateUpdates></qos>");

	print "\nSubscribe success client dk$x\n";
}

# Wait a moment, then publish one message, sleep a bit, then
# publish the same message again.

sleep 2;

my $server = Frontier::Client->new(url => $server_url);
print "Connected to xmlBlaster server on $server_url \n";

# Call the remote server and get our result.
$sessionId = $server->call('authenticate.login', "dkpub", "dkpub", "<qos><local>false</local></qos>", "");
print "\nLogin client success with sessionId=$sessionId \n";

$message = $server->call('xmlBlaster.publish', $sessionId,
	"<key oid='' contentMime='text/xml'>".
	"<service>post</service>".
	"<type>request</type>".
	"<id>123</id>".
	"</key>",
	"<event>testing</event>",
	"<qos><isVolatile>true</isVolatile><isDurable>false</isDurable></qos>");

print "Published one message... (expect 4 updates)\n";


#sleep, then publish again.
sleep 5;

$message = $server->call('xmlBlaster.publish', $sessionId,
	"<key oid='' contentMime='text/xml'>".
	"<service>post</service>".
	"<type>request</type>".
	"<id>123</id>".
	"</key>",
	"<event>testing</event>",
	"<qos><isVolatile>true</isVolatile><isDurable>false</isDurable></qos>");

print "Published one message again... (expect 4 updates)\n";

# Logout from xmlBlaster
$server->call('authenticate.logout', $sessionId);
print "\nLogout done, bye.\n";

