Additions to the Messages file for Spanish Language from Jesus ea1dav
[spider.git] / perl / DXCluster.pm
index 91c43a7880a8944d7fdbb0754bc7d8c1e58d0b35..36d1d20f9db5e20e5d09a75a5dabbde5d078342c 100644 (file)
 
 package DXCluster;
 
-use Exporter;
-@ISA = qw(Exporter);
 use DXDebug;
-use Carp;
+use DXUtil;
 
 use strict;
 use vars qw(%cluster %valid);
@@ -25,13 +23,13 @@ use vars qw(%cluster %valid);
 %cluster = ();                                 # this is where we store the dxcluster database
 
 %valid = (
-                 mynode => '0,Parent Node,showcall',
+                 mynode => '0,Parent Node',
                  call => '0,Callsign',
                  confmode => '0,Conference Mode,yesno',
                  here => '0,Here?,yesno',
-                 dxchan => '5,Channel ref',
+                 dxchancall => '5,Channel Call',
                  pcversion => '5,Node Version',
-                 list => '5,User List,dolist',
+                 list => '5,User List,DXCluster::dolist',
                  users => '0,No of Users',
                 );
 
@@ -43,7 +41,7 @@ sub alloc
        $self->{call} = $call;
        $self->{confmode} = $confmode;
        $self->{here} = $here;
-       $self->{dxchan} = $dxchan;
+       $self->{dxchancall} = $dxchan->call;
 
        $cluster{$call} = bless $self, $pkg;
        return $self;
@@ -102,6 +100,14 @@ sub field_prompt
        my ($self, $ele) = @_;
        return $valid{$ele};
 }
+#
+# return a list of valid elements 
+# 
+
+sub fields
+{
+       return keys(%valid);
+}
 
 # this expects a reference to a list in a node NOT a ref to a node 
 sub dolist
@@ -110,7 +116,8 @@ sub dolist
        my $out;
        my $ref;
   
-       foreach $ref (@{$self}) {
+       foreach my $call (keys %{$self}) {
+               $ref = $$self{$call};
                my $s = $ref->{call};
                $s = "($s)" if !$ref->{here};
                $out .= "$s ";
@@ -131,15 +138,45 @@ sub cluster
 {
        my $users = DXCommandmode::get_all();
        my $uptime = main::uptime();
-       my $tot = $DXNode::users + 1;
+       my $tot = $DXNode::users;
                
        return " $DXNode::nodes nodes, $users local / $tot total users  Max users $DXNode::maxusers  Uptime $uptime";
 }
 
-sub DESTROY
+sub mynode
 {
        my $self = shift;
-       dbg('cluster', "destroying $self->{call}\n");
+       my $noderef = shift;
+       
+       if ($noderef) {
+               $self->{mynode} = $noderef->call;
+       } else {
+               $noderef = DXCluster->get_exact($self->{mynode});
+               unless ($noderef) {
+                       my $mynode = $self->{mynode};
+                       my $call = $self->{call};
+                       dbg("parent node $mynode has disappeared from $call") if isdbg('err');
+               }
+       }
+       return $noderef;
+}
+
+sub dxchan
+{
+       my $self = shift;
+       my $dxchan = shift;
+
+       if ($dxchan) {
+               $self->{dxchancall} = $dxchan->call;
+       } else {
+               $dxchan = DXChannel->get($self->{dxchancall});
+               unless ($dxchan) {
+                       my $dxcall = $self->{dxchancall};
+                       my $call = $self->{call};
+                       dbg("parent dxchan $dxcall has disappeared from $call") if isdbg('err');
+               }
+       }
+       return $dxchan;
 }
 
 no strict;
@@ -152,6 +189,9 @@ sub AUTOLOAD
        $name =~ s/.*:://o;
   
        confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
+       # this clever line of code creates a subroutine which takes over from autoload
+       # from OO Perl - Conway
+       *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
        @_ ? $self->{$name} = shift : $self->{$name} ;
 }
 
@@ -174,10 +214,9 @@ sub new
        die "tried to add $call when it already exists" if DXCluster->get_exact($call);
   
        my $self = $pkg->alloc($dxchan, $call, $confmode, $here);
-       $self->{mynode} = $node;
-       $node->{list}->{$call} = $self; # add this user to the list on this node
-       dbg('cluster', "allocating user $call to $node->{call} in cluster\n");
-       $node->update_users();
+       $self->{mynode} = $node->call;
+       $node->add_user($call, $self);
+       dbg("allocating user $call to $node->{call} in cluster\n") if isdbg('cluster');
        return $self;
 }
 
@@ -185,12 +224,10 @@ sub del
 {
        my $self = shift;
        my $call = $self->{call};
-       my $node = $self->{mynode};
+       my $node = $self->mynode;
 
-       delete $node->{list}->{$call};
-       delete $DXCluster::cluster{$call}; # remove me from the cluster table
-       dbg('cluster', "deleting user $call from $node->{call} in cluster\n");
-       $node->update_users();
+       $node->del_user($call);
+       dbg("deleting user $call from $node->{call} in cluster\n") if isdbg('cluster');
 }
 
 sub count
@@ -224,10 +261,10 @@ sub new
        my $self = $pkg->alloc($dxchan, $call, $confmode, $here);
        $self->{pcversion} = $pcversion;
        $self->{list} = { } ;
-       $self->{mynode} = $self;        # for sh/station
+       $self->{mynode} = $self->call;  # for sh/station
        $self->{users} = 0;
        $nodes++;
-       dbg('cluster', "allocating node $call to cluster\n");
+       dbg("allocating node $call to cluster\n") if isdbg('cluster');
        return $self;
 }
 
@@ -253,8 +290,36 @@ sub del
                $ref->del();                    # this also takes them out of this list
        }
        delete $DXCluster::cluster{$call}; # remove me from the cluster table
-       dbg('cluster', "deleting node $call from cluster\n"); 
-       $nodes-- if $nodes > 0;
+       dbg("deleting node $call from cluster\n") if isdbg('cluster'); 
+       $users -= $self->{users};    # it may be PC50 updated only therefore > 0
+       $users = 0 if $users < 0;
+       $nodes--;
+       $nodes = 0 if $nodes < 0;
+}
+
+sub add_user
+{
+       my $self = shift;
+       my $call = shift;
+       my $ref = shift;
+       
+       $self->{list}->{$call} = $ref; # add this user to the list on this node
+       $self->{users} = keys %{$self->{list}};
+       $users++;
+       $maxusers = $users+$nodes if $users+$nodes > $maxusers;
+}
+
+sub del_user
+{
+       my $self = shift;
+       my $call = shift;
+
+       delete $self->{list}->{$call};
+       delete $DXCluster::cluster{$call}; # remove me from the cluster table
+       $self->{users} = keys %{$self->{list}};
+       $users--;
+       $users = 0, warn "\$users gone neg, reset" if $users < 0;
+       $maxusers = $users+$nodes if $users+$nodes > $maxusers;
 }
 
 sub update_users
@@ -262,14 +327,10 @@ sub update_users
        my $self = shift;
        my $count = shift;
        $count = 0 unless $count;
-  
-       $users -= $self->{users} if $self->{users};
-       if ((keys %{$self->{list}})) {
-               $self->{users} = (keys %{$self->{list}});
-       } else {
-               $self->{users} = $count;
-       }
-       $users += $self->{users} if $self->{users};
+       
+       $users -= $self->{users};
+       $self->{users} = $count unless keys %{$self->{list}};
+       $users += $self->{users};
        $maxusers = $users+$nodes if $users+$nodes > $maxusers;
 }
 
@@ -282,5 +343,13 @@ sub dolist
 {
 
 }
+
+sub DESTROY
+{
+       my $self = shift;
+       undef $self->{list} if $self->{list};
+}
+
+
 1;
 __END__