- push @servers, { name => $servername, priority => $priority, distance => $distance };
- }
+# Loop over the mappings, trying to assign each country to the
+# nearest server, but subject to the bandwidth limits;
+foreach my $mapping (sort { $b->{priority} <=> $a->{priority} || $a->{distance} <=> $b->{distance} } @mappings)
+{
+ my $country = $mapping->{country};
+ my $server = $mapping->{server};
+
+ if ($country->{bandwidth} <= $server->{bandwidth} && !exists($country->{server}))
+ {
+ $country->{server} = $server;
+ $server->{bandwidth} = $server->{bandwidth} - $country->{bandwidth};