3 * Service discovery using DNS SRV records
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 class DnsSrvDiscoverer
{
33 * @param string $domain
35 public function __construct( $domain ) {
36 $this->domain
= $domain;
40 * Fetch the servers with a DNS SRV request
44 public function getServers() {
46 foreach ( $this->getDnsRecords() as $record ) {
48 'target' => $record['target'],
49 'port' => $record['port'],
50 'pri' => $record['pri'],
51 'weight' => $record['weight'],
59 * Pick a server according to the priority fields.
60 * Note that weight is currently ignored.
62 * @param array $servers from getServers
65 public function pickServer( array $servers ) {
71 foreach ( $servers as $server ) {
72 $srvsByPrio[$server['pri']][] = $server;
75 $min = min( array_keys( $srvsByPrio ) );
76 if ( count( $srvsByPrio[$min] ) == 1 ) {
77 return $srvsByPrio[$min][0];
80 $rand = mt_rand( 0, count( $srvsByPrio[$min] ) - 1 );
82 return $srvsByPrio[$min][$rand];
87 * @param array $server
88 * @param array $servers
91 public function removeServer( $server, array $servers ) {
92 foreach ( $servers as $i => $srv ) {
93 if ( $srv['target'] === $server['target'] && $srv['port'] === $server['port'] ) {
94 unset( $servers[$i] );
99 return array_values( $servers );
105 protected function getDnsRecords() {
106 return dns_get_record( $this->domain
, DNS_SRV
);