Business-OCV

 view release on metacpan or  search on metacpan

utilities/ocv  view on Meta::CPAN

# given account, make sure the transaction ends up in the account's 
# transaction log. If you don't do this, then the totals reconciliation 
# will be out as these manual transactions will be unaccounted for.

use strict;

use Term::ReadLine;

#BEGIN {$OCV::debug = 1;}	# turn on/off module debugging at 'use' time

use OCV;

my $reminder = sub { };
END { $reminder->(); }

use constant CLIENTID => 'ocv'; # up to 8 chars

use constant TIMEOUT	=> 60;		# OCV send/recv timeout (seconds)

use constant LOGDIR		=> './var';	# link to OCV txn log dir
use constant TXNLOG		=> 'ocv.txnlog';
use constant DEBUGLOG	=> 'ocv.debug';

use constant HISTFILE	=> LOGDIR . '/.ocv.hist';

my $server = shift;
my $accountnum = shift;

die "usage: $0 <server<:port>> <account number>\n" unless 
	($server and $accountnum);

# print a 'merge' reminder at exit
$reminder = sub
{
	$_ =<<"	."; s/^\t//mg;
	###############################################################
	If you've completed any transactions, make sure to merge
	${\LOGDIR}/${\TXNLOG}
	into the main transaction logs for this account!
	###############################################################
	.
	warn $_;
};


# set the Transaction Reference number generator
# - the OCV allows a 16 character field for the transaction reference
# - basically a client identifier string, a hex-encoded, offset time, and 
#   a sequence number. The time offset is simply to reduce the number of 
#   characters in the resulting string.
#   938700000 = 99/10/01 00:00:00 in TZ=Australia/NSW
my $sn = 0;
sub txnref { $sn %= 100; sprintf("%s%X%02d", "$$-", time()-938700000, $sn++) }

# required parameters are: server address, client ID, account number
my $ocv = new OCV (
		Server		=> $server, 
		ClientId	=> CLIENTID, 
		AccountNum	=> $accountnum, 
		TxnRef		=> \&txnref, 
		Timeout		=> TIMEOUT, 
		LogDir		=> LOGDIR,
		TxnLog		=> TXNLOG,
		DebugLog	=> DEBUGLOG,
		Debug		=> 1, 
	)
	or die "Could not create OCV object: $@\n";

my $statistics = $ocv->statistics(SubCode => STATS_CURRENT) 
	or warn "Statistics: $@\n";

print "Server up since $statistics->[14]\n";

my $term;

{
	local $^W = 0;	# (try to) suppress ReadLine warnings

	$term = new Term::ReadLine ("OCV Console");
	$term->ornaments(0);

	if (open(HISTORY, '<' . HISTFILE))
	{
		while (<HISTORY>)
			{ chomp($_); next unless ($_ and /\S/); $term->addhistory($_); }
	}
}

open(HISTORY, '>>'.HISTFILE) or 
	warn ("couldn't append to history file [$0.hist]: $!\n"), 
	open(HISTORY, ">/dev/null");

help();

my $indent = 0;
sub printmsg
{
	my $s = shift;	# ref to message object
	my @o = @_;		# orientation: () = list, list = table (values are widths)

	my %m = $s->hash;
	if (@o)	# table format:just print rows of data
	{
		my $i = 0;
		for my $f ($s->fields)
		{
			my $v = $m{$f};
			if (!defined($v))		{ printf '%*s ', $o[$i++], '-'; }
			elsif ($v =~ /^\d+$/)	{ printf '%*d ', $o[$i++], $v;  }
			else 					{ printf '%*s ', $o[$i++], $v;  }
		}
		print "\n";
	}
	else
	{
		$indent++;
		for my $f ($s->fields)	# fields => hash keys, sorted
		{
			print "\t" x $indent;
			if (ref($m{$f}))	# sub-message or list of sub-messages
			{



( run in 1.992 second using v1.01-cache-2.11-cpan-98e64b0badf )