With the exception of Sinan, I have had absolutely no help yet. Since
this is a prototype; emails about the structure of the code are
frankly meaningless. I don't need an adduser function because I don't
plan on adding users. That code is merely a placeholder for database
access code. Comments about what I do or do not know about Perl are
basically meaningless as well. I don't claim to be an expert in Perl.
That's why I am here asking a very simple question. Call it fumbling
with different attempts at the syntax but basically that's the real
problem I'm having. I only need something that works and I wanted to
show people that I have actually tried to solve this on my own before
I came here.
I need to see if this language is feasible as a SOAP Server, even
though there is little support in Perl's weakly typed language to
generate WSDL files automatically compared to Java, C/C++, .NET and
most other languages. You can't honestly expect me to read 900 pages
just for a small sample. If I wanted to try out in 4 languages I'd
basically have to read 3600 pages. **It only has to work**, it doesn't
have to work well. I just want to see how complicated it's going to
be. I already have a soap sample working but I now I want to extend it
with instances of objects and this is where it's all going wrong. The
objects aren't being remembered across the session - or at least
that's what I think.
When I test user_management.pm with my test script. The output is what
I expect. When I try to use it in soap I get this:
http://bayimg.com/bAjpCAabi
When I take out the line that says "use strict" I get this:
http://bayimg.com/BaJpDaAbi
perhaps I can be forgiven for actually "closing my eyes" since it only
happens in the SOAP client -> Soap Server example and it doesn't
happen when I run a test script directly against user_management.pm.
---
I think the function returns "0 of 0" because the class variables are
not being remembered. That's my feeling and that's the question I was
looking to solve but apparently I have been branded as ignorant
because I wanted to postpone reading a book until after I solved it.
The fact I have constructed an existing SOAP Server example, have
already demonstrated a considerable knowledge for someone who hasn't
yet read a book doesn't seem to count for anything. There seems to be
an invisible threshold of questions. The simpler the question the more
is warrants abuse from a developer's misunderstanding of the
fundamentals.
Anyway I have posted the code below and I really hope someone can help
me.
------------ USER.PM -------------
#!/usr/bin/perl -w
use strict;
package user;
sub new
{
my ($class_name) = @_;
my ($self) = {};
#warn "We just created our new variable...\n ";
bless ($self, $class_name);
#warn "and now it's a $class_name object!\n";
#$self->{'_created'} = 1;
return $self;
}
sub getId
{
my $self = shift;
return $self->{'id'};
}
sub setId
{
my ($self, $id) = @_;
$self->{'id'} = $id;
}
sub getUsername
{
my $self = shift;
return $self->{'username'};
}
sub setUsername
{
my ($self, $username) = @_;
$self->{'username'} = $username;
}
sub getPassword
{
my $self = shift;
return $self->{'password'};
}
sub setPassword
{
my ($self, $password) = @_;
$self->{'password'} = $password;
}
sub getSessionKey
{
my $self = shift;
return $self->{'sessionkey'};
}
sub setSessionKey
{
my ($self, $key) = @_;
$self->{'sessionkey'} = $key;
}
1;
-------- USER_MANAGEMENT.PM ------------------
#!/usr/bin/perl -w
package user_management;
use user;
use strict;
#my $instance = new user_management;
sub new
{
my ($class_name) = @_;
my ($self) = {};
bless ($self, $class_name);
#some objects for the array
my $phill = new user;
$phill->setId(6);
$phill->setUsername("ptaylor");
$phill->setPassword("password");
$phill->setSessionKey("");
my $simon = new user;
$simon->setId(7);
$simon->setUsername("sroberts");
$simon->setPassword("cheese");
$simon->setSessionKey("");
my $nick = new user;
$nick->setId(8);
$nick->setUsername("nick");
$nick->setPassword("rock");
$nick->setSessionKey("");
#put them in the array
my @users = ( $phill, $simon, $nick );
$self->{users} = \@users; #put the array in an instance variable (or
perl equiv)
$self->{nextSessionId} = ":-0";
return $self;
}
sub login
{
my ($self, $username, $password) = @_;
my @users = @{ $self->{users} };
foreach my $user (@users)
{
if ($user->getUsername() eq $username)
{
if ($user->getPassword() eq $password)
{
if ($user->getSessionKey ne "")
{
return "ERR: User already logged in";
}
else
{
#generate session key
$user->setSessionKey("SESS" . $self->{'nextSessionId'});
$self->{'nextSessionId'} = $self->{'nextSessionId'} + 1;
return $user->getSessionKey();
}
}
else
{ return "ERR: Password is wrong"; }
}
}
return "ERR: No users";
}
sub query
{
my $self = shift;
#return "Session id= " . $self->{nextSessionId} . "\n";
my @users = @{$self->{users}};
my $active = 0;
my $total = @users;
my $retval = "";
#return "Total: $total\n";
foreach my $usr (@users)
{
bless($usr, "user");
if ($usr->getSessionKey() ne "")
{
$retval = $retval . "user " . $usr->getUsername() . " logged in\n";
$active++;
}
else
{
$retval = $retval . "user " . $usr->getUsername() . " logged out
\n";
}
}
return "$retval$active of $total";
}
sub logout
{
my ($self, $sessionKey) = @_;
my @users = @{ $self->{users} };
foreach my $usr (@users)
{
if ($usr->getSessionKey() eq $sessionKey)
{
$usr->setSessionKey("");
return 1;
}
}
return 0;
}
1;
------------ TEST_USER.PL ----------------------------------
#!/usr/bin/perl -w
#THIS SCRIPT TESTS THAT USER_MANAGEMENT.PM WORKS CORRECTLY
#IT DOES WORK CORRECT ACCORDING TO THIS SCRIPT!
use user;
use user_management;
use strict;
print "code started\n";
my $phill1 = new user;
$phill1->setId(6);
$phill1->setUsername("phill");
$phill1->setPassword("hello");
my $dave1 = new user;
$dave1->setId(7);
$dave1->setUsername("dave");
$dave1->setPassword("omg");
my $alan1 = new user;
$alan1->setId(8);
$alan1->setUsername("alan");
$alan1->setPassword("alsecret");
my @users = ( $phill1, $alan1, $dave1 );
foreach my $currentUser (@users)
{
print "user id:\t" . $currentUser->getId() . "\n";
print "username:\t" . $currentUser->getUsername() . "\n";
print "password:\t" . $currentUser->getPassword() . "\n";
}
print "done\n";
#&load;
my $sessionKey = login("ptaylor","password");
print $sessionKey;
my $answer = &query;
print "$answer\n";
logout ($sessionKey);
$answer = &query;
print "$answer\n";
$sessionKey = login("nick","rock");
print "session key for nick is $sessionKey\n";
$answer = &query;
print "$answer\n";
print "done\n";
------------ USER_MANAGEMENT.CGI ----------------------
#!/usr/bin/perl -w
package user_management;
use user_management;
no warnings;
use SOAP::Transport::HTTP;
SOAP::Transport::HTTP::CGI
-> dispatch_to('user_management')
-> handle;
------------ THE WSDL FILE ----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="user_management"
targetNamespace="**textdeleted**"
xmlns="
http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="
http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="**textdeleted**"
xmlns:xsd="
http://www.w3.org/2001/XMLSchema" >
<message name="loginParams">
<part name="username" type="xsd:string" />
<part name="password" type="xsd:string" />
</message>
<message name="loginRetval">
<part name="retval" type="xsd:string" />
</message>
<message name="void">
</message>
<message name="queryRetval">
<part name="retval" type="xsd:string" />
</message>
<message name="logoutParams">
<part name="sessionKey" type="xsd:string" />
</message>
<message name="logoutRetval">
<part name="retval" type="xsd:int" />
</message>
<portType name="user_management_port_type">
<operation name="login">
<input message="tns:loginParams" />
<output message="tns:loginRetval" />
</operation>
<operation name="query">
<input message="tns:void" />
<output message="tns:queryRetval" />
</operation>
<operation name="logout">
<input message="tns:logoutParams" />
<output message="tns:logoutRetval" />
</operation>
</portType>
<binding name="user_management_binding"
type="tns:user_management_port_type">
<soap:binding style="rpc" transport="
http://schemas.xmlsoap.org/soap/
http" />
<operation name="login">
<soap
peration soapAction="urn:user_management#login" />
<input>
<soap:body
encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:user_management"
use="encoded" />
</input>
<output>
<soap:body
encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:user_management"
use="encoded" />
</output>
</operation>
<operation name="query">
<soap
peration soapAction="urn:user_management#query" />
<input>
<soap:body
encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:user_management"
use="encoded" />
</input>
<output>
<soap:body
encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:user_management"
use="encoded" />
</output>
</operation>
<operation name="logout">
<soap
peration soapAction="urn:user_management#logout" />
<input>
<soap:body
encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:user_management"
use="encoded" />
</input>
<output>
<soap:body
encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:user_management"
use="encoded" />
</output>
</operation>
</binding>
<service name="UserManagementService">
<documentation>Session Management for AM</documentation>
<port binding="tns:user_management_binding"
name="user_management_port">
<soap:address
location="**textdeleted**/user_management.cgi" />
</port>
</service>
</definitions>
-------------- THE SOAP CLIENT -------------------------
'consists of a textbox and a button of the window.
Imports SessionManagementExample.UserManagement
Public Class Form1
Private server As UserManagementService
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
server = New UserManagementService()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim answer As String = server.query()
TextBox1.Text = answer.Replace(Chr(10), vbCrLf)
End Sub
End Class