| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: OnlineUser.pm,v 1.8 2003/12/16 15:21:23 eserte Exp $ | ||
| 5 | # Author: Slaven Rezic | ||
| 6 | # | ||
| 7 | # Copyright (C) 2001 Online Office Berlin. All rights reserved. | ||
| 8 | # Copyright (C) 2002 Slaven Rezic. | ||
| 9 | # This is free software; you can redistribute it and/or modify it under the | ||
| 10 | # terms of the GNU General Public License, see the file COPYING. | ||
| 11 | |||
| 12 | # | ||
| 13 | # Mail: slaven@rezic.de | ||
| 14 | # WWW: http://we-framework.sourceforge.net | ||
| 15 | # | ||
| 16 | |||
| 17 | package WE::DB::OnlineUser; | ||
| 18 | |||
| 19 | 2 | ▶ | use base qw/WE::DB::Base/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | |||
| 21 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 2 | ▶ | use vars qw($VERSION $TIMEOUT); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 23 | $VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/); | ||
| 24 | |||
| 25 | __PACKAGE__->mk_accessors(qw/Timeout DBFile/); | ||
| 26 | |||
| 27 | 0 | ▶ | use DB_File; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 28 | |||
| 29 | $TIMEOUT = 10*60; | ||
| 30 | |||
| 31 | =head1 NAME | ||
| 32 | |||
| 33 | WE::DB::OnlineUser - methods for users who are currently online | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | new WE::DB::OnlineUser $rootdb, $databasefilename, -timeout => 30*60; | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | This class holds methods for users who are currently online. Users may | ||
| 42 | login and logout and should in intervals smaller than C<$TIMEOUT> ping | ||
| 43 | back to the server so they signal that they are still logged in. | ||
| 44 | |||
| 45 | All timeouts are in seconds. The default timeout is 10 minutes. | ||
| 46 | |||
| 47 | =head2 CONSTRUCTOR new($class, $root, $file, %args) | ||
| 48 | |||
| 49 | Usually called from C<WE::DB>. | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub new { | ||
| 54 | my($class, $root, $file, %args) = @_; | ||
| 55 | my $self = {}; | ||
| 56 | bless $self, $class; | ||
| 57 | $self->DBFile($file); | ||
| 58 | |||
| 59 | $args{-readonly} = 0 unless defined $args{-readonly}; | ||
| 60 | $args{-writeonly} = 0 unless defined $args{-writeonly}; | ||
| 61 | |||
| 62 | my $flags; | ||
| 63 | if ($args{-readonly}) { | ||
| 64 | $flags = O_RDONLY; | ||
| 65 | } elsif ($args{-writeonly}) { | ||
| 66 | $flags = O_RDWR; | ||
| 67 | } else { | ||
| 68 | $flags = O_RDWR|O_CREAT; | ||
| 69 | } | ||
| 70 | |||
| 71 | if (!defined $args{-connect} || $args{-connect} ne 'never') { | ||
| 72 | tie %{$self->{DB}}, "DB_File", $file, $flags, 0664 | ||
| 73 | or die("Can't tie database $file: $!"); | ||
| 74 | $self->Connected(1); | ||
| 75 | } | ||
| 76 | if (defined $args{-timeout}) { | ||
| 77 | $self->Timeout($args{-timeout}); | ||
| 78 | } else { | ||
| 79 | $self->Timeout($TIMEOUT); | ||
| 80 | } | ||
| 81 | $self; | ||
| 82 | } | ||
| 83 | |||
| 84 | =head2 METHODS | ||
| 85 | |||
| 86 | =over 4 | ||
| 87 | |||
| 88 | =item login($user) | ||
| 89 | |||
| 90 | Log in the specified C<$user> to the online user database. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub login { | ||
| 95 | my($self, $user) = @_; | ||
| 96 | $self->{DB}{$user} = time; | ||
| 97 | } | ||
| 98 | |||
| 99 | =item logout($user) | ||
| 100 | |||
| 101 | Log out the specified C<$user> from the online user database. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub logout { | ||
| 106 | my($self, $user) = @_; | ||
| 107 | delete $self->{DB}{$user}; | ||
| 108 | } | ||
| 109 | |||
| 110 | =item check_logged($user, [$timeout], [$result]) | ||
| 111 | |||
| 112 | Check whether the specified user is still logged in. Return either a | ||
| 113 | true or false value. The C<$timeout> parameter is optional. If | ||
| 114 | C<$result> is specified, it has to be a reference to a scalar value | ||
| 115 | and will hold the exact result string (e.g. "Not logged in", "Timed | ||
| 116 | out" or "Logged in") after the method returns. Some usage examples: | ||
| 117 | |||
| 118 | $bool = $onlineuserdb->check_logged("eserte"); | ||
| 119 | $bool = $onlineuserdb->check_logged("eserte", undef, \$result); | ||
| 120 | print "Result is: $result\n"; | ||
| 121 | $bool = $onlineuserdb->check_logged("eserte", 10*60, \$result); | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub check_logged { | ||
| 126 | my($self, $user, $timeout, $result) = @_; | ||
| 127 | $timeout = $self->Timeout unless defined $timeout; | ||
| 128 | my $last_check = $self->{DB}{$user}; | ||
| 129 | if (!defined $last_check) { | ||
| 130 | $$result = "Not logged in" if ref $result eq 'SCALAR'; | ||
| 131 | return 0; | ||
| 132 | } | ||
| 133 | if ($last_check+$timeout < time) { | ||
| 134 | $$result = "Timed out" if ref $result eq 'SCALAR'; | ||
| 135 | return 0; | ||
| 136 | } | ||
| 137 | $$result = "Logged in" if ref $result eq 'SCALAR'; | ||
| 138 | 1; | ||
| 139 | } | ||
| 140 | |||
| 141 | =item ping($user) | ||
| 142 | |||
| 143 | The C<$user> marks himself as alive in the online database. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub ping { | ||
| 148 | my($self, $user) = @_; | ||
| 149 | $self->{DB}{$user} = time; | ||
| 150 | } | ||
| 151 | |||
| 152 | =item cleanup([$timeout]) | ||
| 153 | |||
| 154 | Delete all non-logged-in users from the online user database. The | ||
| 155 | C<$timeout> parameter is optional. | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub cleanup { | ||
| 160 | my($self, $timeout) = @_; | ||
| 161 | # XXX locking? | ||
| 162 | my(@todel); | ||
| 163 | while(my($user) = each %{$self->{DB}}) { | ||
| 164 | if (!$self->check_logged($user, $timeout)) { | ||
| 165 | push @todel, $user; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | foreach (@todel) { | ||
| 169 | $self->logout($_); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | =item delete_db_contents | ||
| 174 | |||
| 175 | Delete all database contents | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub delete_db_contents { | ||
| 180 | my $self = shift; | ||
| 181 | my(@todel) = keys %{$self->{DB}}; | ||
| 182 | foreach (@todel) { | ||
| 183 | delete $self->{DB}{$_}; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | =item disconnect | ||
| 188 | |||
| 189 | Disconnect the database. No further access on the database may be done. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub disconnect { | ||
| 194 | my $self = shift; | ||
| 195 | eval { | ||
| 196 | untie %{ $self->{DB} }; | ||
| 197 | };warn $@ if $@; | ||
| 198 | } | ||
| 199 | |||
| 200 | # XXX del: | ||
| 201 | # =item delete_db | ||
| 202 | |||
| 203 | # Delete the database completely (including the disk file). | ||
| 204 | |||
| 205 | # =cut | ||
| 206 | |||
| 207 | # sub delete_db { | ||
| 208 | # my $self = shift; | ||
| 209 | # unlink $self->DBFile; | ||
| 210 | # } | ||
| 211 | |||
| 212 | 1; | ||
| 213 | |||
| 214 | __END__ | ||