+ # Populate the routing table
+ my $rn = Route::Node::get($pc->[7]);
+ unless ($rn) {
+ $rn = Route::Node->new($pc->[7]);
+ dbg("ROUTE $self->{call}: ADD NEW node: $pc->[7]") if isdbg('pc11');
+ }
+ my $r = Route::User::get($pc->[6]);
+ unless ($r) {
+ $rn->add_user($pc->[6], 0, undef);
+ dbg("ROUTE $self->{call}: ADD NEW user: $pc->[6] -> $pc->[7]") if isdbg('pc11');
+ $r = Route::User::get($pc->[6]);
+ }
+
+ # Add/Change any IP address info
+ if ($pcno == 61) {
+
+ # as we have a route to a user, if it (or the node) does not exist then create them
+ # link the user to the node if not already done.
+ # then add or alter the IP address
+ if ($pc->[8]) {
+ my $new = $pc->[8];
+ if ($r) {
+ if ($r->ip ne $new) {
+ if ($r->ip) {
+ my $old = $r->ip;
+ $r->ip($new);
+ dbg("ROUTE $self->{call}: ALTER IP node: $pc->[7] user: $pc->[6] old IP: '$old'-> new IP: '$new'") if isdbg('pc11');
+ } else{
+ $r->ip($new);
+ dbg("ROUTE $self->{call}: NEW IP node: $pc->[7] user: $pc->[6] IP: '$new'") if isdbg('pc11');
+ }
+ }
+ } else {
+ dbg("ROUTE $self->{call}: ADD Failed for node $pc->[7] user $pc->[6]") if isdbg('pc11');
+ }
+ } else {
+ dbg("PCPROT: ROUTE $self->{call} NO IP ADDRESS in '$line'!");
+ }
+ }
+
+ # this is where we decide to delay PC11s in the hope that a PC61 will be along soon.
+ my $key = join '|', @spot[0..2,4,7]; # not including text
+
+ unless ($recurse) {
+
+ if ($pcno == 61) {
+
+ if ($pc11_saved{$key}) {
+ # before we promote by route, check that it's not a dupe (but don't insert it).
+ if (Spot::dup_find(@spot[0..4,7])) {
+ dbg("PCPROT: Duplicate Spot $self->{call}: $pc->[0] $key ignored\n") if isdbg('chanerr') || isdbg('dupespot') || isdbg('pc11');
+ delete $pc11_saved{$key};
+ return;
+ }
+
+ ++$pc11_to_61;
+ my $percent = $pc11_rx ? $pc11_to_61 * 100 / $pc11_rx : 0;
+ dbg(sprintf("PROMOTED $self->{call}: BETTER $pc->[0] $key, using pc61, WAITING pc11 DUMPED: $pc61_rx pc11: $pc11_rx better pc61: $pc11_to_61 (%0.1f%%)", $percent)) if isdbg("pc11");
+ }
+ }
+
+ if ($pcno == 11) {
+
+ # if this fires then we have already had one or more PC11s but no PC61 for this spot
+ if ($pc11_saved{$key}) {
+ dbg("DUPE $self->{call} $pc->[0] key: $key is saved, ignored") if isdbg("pc11");
+ return; # because it's a dup
+ }
+
+ # before we promote by route, check that it's not been preceded by a previous PC61
+ if (Spot::dup_find(@spot[0..4,7])) {
+ my $s = exists $pc11_saved{$key} ? " stored $key removed" : "";
+ dbg("PCPROT: Duplicate Spot $self->{call}: PC11$s, recurse: $recurse, ignored\n") if isdbg('chanerr') || isdbg('dupespot') || isdbg('pc11');
+ delete $pc11_saved{$key};
+ return;
+ }
+
+
+ # If we have an ip address we can promote by route
+ if ($r && $r->ip) {
+ $pcno = 61;
+ $pc->[0] = 'PC61';
+ my $hops = $pc->[8];
+ $pc->[8] = $spot[14] = $r->ip;
+ ++$rpc11_to_61;
+ my $percent = $pc11_rx ? $rpc11_to_61 * 100 / $pc11_rx : 0;
+ dbg(sprintf("PROMOTED $self->{call}: ROUTE pc11 $key PROMOTED to pc61 with IP $spot[14] pc61: $pc61_rx pc11: $pc11_rx route->pc61 $rpc11_to_61 (%0.1f%%)", $percent)) if isdbg("pc11");
+ $line = join '^', @$pc, $hops, '~';
+
+ # update the stats
+ ++$pc11_rx; # 'cos we received it and it won't be a pc11 anymore
+ --$pc61_rx; # 'cos we'll increment it later as it's now a pc61, no double counting
+
+# dbg("CHANGED saved key: $key PC11 line to $line") if isdbg('pc11');
+ delete $pc11_saved{$key};
+ }
+
+ # if it is STILL (despite all efforts to change it) a PC11
+ # save it and wait - it will be called from pc11_process
+ if ($pcno == 11) {
+ $pc11_saved{$key} = [$main::systime, $self, $pcno, $line, $origin, $pc];
+ dbg("WAITING $self->{call}: NEW $pc->[0] spot $key waiting for a better offer") if isdbg("pc11");
+ return;
+ }
+ }
+ }
+