Hi Monks:

I am attempting to learn how to use objects and have made progress, but here I am stymied. I am using HTTP::Server::Simple::CGI;= and having cognitive difficulties getting it to pass the incoming message to another class. I thought the answer was in the post_setup_hook method, but when I try to run I get a error that says I cannot call SUPER when I reach the handle_request method.

What I am trying to do below is to geth the message from the handle_request method of HTTP::Server::Simple::CGI;= to the process method of the Kiosk package and then to get the response to go back out. I tried passing the Kiosk object into the server first adn that did not work because I don't see how to add this to the "new" method of the server, so now I am trying to pass the server into the Kiosk module and attempting to get a hold of the message

this is the main program, as you can see I instantiate the server adn pass it to the Kiosk

#!/usr/bin/perl { package MyWebServer; # https://metacpan.org/pod/HTTP::Server::Simple # curl --header "Content-Type: application/json" --request POST -- +data '{ "type":"cardCheck", "date":"1482307795601", "lang": "EN", "ma +chineName":"PLUSMAKM", "cardNumber":"1234567890", "PIN": "1234" }' ht +tp://localhost:2999 use lib qw (/home/pi/cashcade /home/gary/projects/cashcade); use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); # use HTTP::Server::Simple; # use base qw(HTTP::Server::Simple); use Data::Dumper; use JSON; sub handle_request { my $self = shift; my $cgi = shift; my $json_string = $cgi->param('POSTDATA'); # { "type":"cardCheck", "date":"1482307795601", "lang": "EN", +"machineName":"PLUSMAKM", "cardNumber":"1234567890", "PIN": "1234" } print "json_string=$json_string\n"; $json_dict = decode_json $json_string; print "json_dict=" . Dumper($json_dict) . "\n"; my $msg = $json_dict->{param}->{POSTDATA}->[0]; # my $response = $self->{kiosk}->process($msg); # note: this +says I cannot call on undefined, the problme is, how do I pass the ob +ject in # print "response=$response\n"; return $msg; } } sub sigIntHandler { exit; } END { print "MAIN: END*END*END*END*END*END*END*END*END*END*END*END*END*E +ND\n"; $kiosk->disconnect(); } my $kiosk_port = shift // 'unknown'; die "FATAL ERROR: Need to specify a port\n" unless ($kiosk_port ne 'un +known'); my $placard = shift // 'unknown'; die "FATAL ERROR: Need to specify a placard\n" unless ($placard ne 'un +known'); my $debug_level = shift // 9; # TODO: make level higher on final print "$placard, $debug_level\n"; use Kiosk; # start the server on port __ print "INSTANTIATING SERVER port $kiosk_port ========================= +=====\n"; my $server = MyWebServer->new($kiosk_port); print "INSTANTIATING KIOSK ==============================\n"; $kiosk = Kiosk->new($placard, $server, $debug_level);

In the Kiosk module (the first part below, I run the server when I instantiate the Kiosk

package Kiosk; # This implements communications to a VNE kiosk as an HTTP server # based on VNE JSON Communication Protocol Ver. 1.8 # Alessandro: add to protocol: change PIN? There is no card removed me +ssage use strict; use Device::SerialPort; #use Data::CISerializer; #use MIME::Base64; #use JSON; use Time::HiRes qw(usleep gettimeofday tv_interval time); use Data::Dumper; use Debug; use GetConfiguration; use Database; use DatabaseProcesses; $|=1; sub new { my ($class, $placard, $server, $debug_level) = @_; # config is a d +ata structure that has serial setup information my $self = {}; $self->{me} = 'Kiosk'; $self->{class} = $class; $self->{placard} = $placard; $self->{server} = $server; $self->{debug} = Debug->new($placard, "MAIN_$placard", $debug_leve +l); $self->{config} == GetConfiguration->new($self->{debug}, '/etc/cas +hcade/cashcade.conf'); $self->{debug}->prt(9, "KIOSK Config " . Dumper($self->{config}), +'KIOSK'); $self->{dbcomm} = Database->new($self->{debug}, $self->{configurat +ion}->{config}->{local_db}); # the DB connection must go first and su +cceed $self->{dbcomm}->connect(); die "Database unable to connect!" if (!$self->{dbcomm}->{connected +}); print "Database Connected\n"; $self->{dbproc} = DatabaseProcesses->new($self->{debug}, $self->{d +bcomm}); $self->{maxDeposit} = $self->{config}->{maxDepositCents}; $self->{maxWithdraw} = $self->{config}->{maxWithdrawCents}; $self->{debug}->prt(2, "KIOSK CONFIG: kiosk_id=$self->{kiosk_id} m +axDeposit=$self->{maxDeposit} maxWithdraw=$self->{maxWithdraw}", 'KIO +SK INIT'); $self->{connected} = 0; $self->{verbose} = 0; $self->{kiosk_id} = $self->{dbproc}->get_kiosk_sysid_from_placard( +$self->{placard}); $self->{card_number} = 0; # this is the latest card number seen $self->{account_id} = 0; # this is the account id while the card i +s in the reader of the kiosk $self->{account_value} = 0; # value of account in cents $self->{response} = ''; $self->{server}->run(); bless($self,$class); return $self; } sub process { my ($self, $msg) = @_; # $msg is the pointer to the incoming mess +age json hash # { "type":"cardCheck", "date":"1482307795601", "lang": "EN", "mac +hineName":"PLUSMAKM", "cardNumber":"1234567890", "PIN": "1234" } # TODO: is message for this machine $msg->{machineName} # TODO: is date of message reasonable $msg->{date} $self->{response} = ''; if ( $msg->{type} eq 'cardCheck') { return $self->card_was_inserted($msg); } if ( $msg->{type} eq 'cardLoad') { return $self->deposit_amount_received($msg); } if ( $msg->{type} eq 'cardWithdrawal') { return $self->withdrawal_amount_requested($msg); } }

Anyhow, I am confused after several hours of tinkering and appreciate your help.


In reply to Mass Class Confusion - Who calls what how? by holandes777

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.