#! /usr/bin/perl # FXSessionManager # managers user session identification # and session data which in this # case are 2 files: # 1) standard fx currency symbols with descriptions (currency) # 2) exchange rates for currency (rates) # # For a New User a New Session set up # currency and rate files are created copied # from a set of master files. # both of these 2 user data files # have the same base name with different # extensions. # # Established users get the # files identified by their userid in their session cookie. # # package FXSessionManager; use FXData; use CGI; use strict; # master files use constant RATE_FILE => "arbitrage.dat"; use constant CUR_FILE => "currency.dat"; # user data directory use constant USERDIR => "/tmp/"; # user data file extensions my $rate_ext = "rates"; my $sym_ext = "sym"; # cookie data use constant SessionName => "fx_session"; use constant SessionPath => "/cgi-bin/arbitrage"; use constant SessionDomain => "www.matrixwebdev.com"; # FXSessionManager(CGI()) # CGI - CGI object sub new { my $class = shift; my $self = {}; $self->{cgi} = shift; $self->{sessionid} = ""; $self->{ratefile} = ""; $self->{curfile} = ""; bless ($self, $class); $self->_initialize(); return $self; } # # Public Methods # # get_session_cookie() -> string # returns a cookie based on the objects # current state. sub get_session_cookie { my $self = shift; $self->{cgi}->cookie(-name => SessionName, -value => $self->{sessionid}, -expires => '+10y', -path => SessionPath, -domain => SessionDomain ); } # fxdata_factory() -> FXData object # returns the appropriate FXData object. sub fxdata_factory { my $self = shift; return new FXData($self->{ratefile}, $self->{curfile} ); } # last_update_date() # returns the last timestamp when # data was last downloaded into the # users currency data file (rate file). sub last_update_date { my $self = shift; my @stat = stat($self->{ratefile}); return localtime($stat[8]); } # # Private Methods # # controls the setup of the users session # called when the object is instantiated. sub _initialize { my $self = shift; # does session cookie exist? my $session = $self->{cgi}->cookie(SessionName); if ($session) { $self->{sessionid} = $session; } else { $self->_create_new_session(); } $self->{ratefile} = $self->{sessionid} . ".$rate_ext"; $self->{curfile} = $self->{sessionid} . ".$sym_ext"; } # create sessionid and new session data files. sub _create_new_session{ my $self = shift; my $tmpfile; my $rate_file; my $sym_file; # create temporary session files. do { my $mid = $$ . srand(9999) . getppid(); $tmpfile = USERDIR . "tmp$mid"; $rate_file = "$tmpfile.$rate_ext"; $sym_file = "$tmpfile.$sym_ext"; } until (! (-e($rate_file) && -e($sym_file)) ); # set up new data files. # with data from primary data set. $self->{fxdata} = new FXData(RATE_FILE, CUR_FILE); $self->{fxdata}->set_rate_file($rate_file); $self->{fxdata}->set_currency_file($sym_file); $self->{fxdata}->dump_spotrates(); $self->{fxdata}->dump_currencies(); $self->{sessionid} = $tmpfile; } 1;