+
+#
+# Allocate each country to a server
+#
+sub allocate_servers
+{
+ my $mappings = shift;
+
+ # 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 (!exists($country->{server}) &&
+ $server->{bandwidth_used} + $country->{bandwidth} <= $server->{bandwidth_limit})
+ {
+ $country->{server} = $server;
+ $server->{bandwidth_used} = $server->{bandwidth_used} + $country->{bandwidth};
+ }
+ }
+
+ return;
+}