4 * Copyright (c) 2008 Jerry Jalava <jerry.jalava@gmail.com>
5 * Licensed under the GPLv3 license
6 * http://www.gnu.org/licenses/gpl.html
10 class couchdb_replicator_http
extends couchdb_replicator
implements couchdb_replicator_interface
12 public function replicate()
16 $from_packet = json_encode(array(
17 'source' => "http://{$this->configuration['source']['host']}:{$this->configuration['source']['port']}/{$this->configuration['source']['db']}",
18 'target' => "http://{$this->configuration['target']['host']}:{$this->configuration['source']['port']}/{$this->configuration['target']['db']}",
21 $transport = new couchdb_transport($this->local_configuration
);
25 $from_results = $transport->free_execute('_replicate', 'POST', $from_packet);
27 catch (couchdb_transport_exception
$e)
29 throw new couchdb_replicator_exception("Unknown error occured while executing replication to {$this->configuration['target']['host']}. {$e}");
34 if ( !isset($this->configuration
['setup']['one_way'])
35 ||
!$this->configuration
['setup']['one_way'])
37 $to_packet = json_encode(array(
38 'source' => "http://{$this->configuration['target']['host']}:{$this->configuration['source']['port']}/{$this->configuration['target']['db']}",
39 'target' => "http://{$this->configuration['source']['host']}:{$this->configuration['source']['port']}/{$this->configuration['source']['db']}",
44 $to_results = $transport->free_execute('_replicate', 'POST', $to_packet);
46 catch (couchdb_transport_exception
$e)
48 throw new couchdb_replicator_exception("Unknown error occured while executing replication to {$this->configuration['source']['host']}. {$e}");
53 'from' => $from_results,
58 private function set_view($function)
62 * This will create temp db in sources and run the given temp view
63 * and inserts the result to the created temp db.
64 * After that those dbs are replicated to the original targets
70 public function check_config()
72 if (! isset($this->configuration
['source']['host']))
74 if (empty($this->local_configuration
['host']))
76 throw new couchdb_replicator_exception('No source host found in configuration!');
79 $this->configuration
['source']['host'] = $this->local_configuration
['host'];
82 if (! isset($this->configuration
['source']['port']))
84 if (empty($this->local_configuration
['port']))
86 throw new couchdb_replicator_exception('No source port found in configuration!');
89 $this->configuration
['source']['port'] = $this->local_configuration
['port'];
92 if (! isset($this->configuration
['source']['db']))
94 throw new couchdb_replicator_exception('No source db found in configuration!');
97 if (! isset($this->configuration
['target']['host']))
99 if (empty($this->local_configuration
['host']))
101 throw new couchdb_replicator_exception('No target host found in configuration!');
104 $this->configuration
['target']['host'] = $this->local_configuration
['host'];
107 if (! isset($this->configuration
['target']['port']))
109 if (empty($this->local_configuration
['port']))
111 throw new couchdb_replicator_exception('No target port found in configuration!');
114 $this->configuration
['target']['port'] = $this->local_configuration
['port'];
117 if (! isset($this->configuration
['target']['db']))
119 throw new couchdb_replicator_exception('No target db found in configuration!');
122 if (! isset($this->configuration
['setup']))
124 $this->configuration
['setup'] = array(
132 private function can_connect()
134 $source_socket = @fsockopen
($this->configuration
['source']['host'], $this->configuration
['source']['port'], $errno = -1, $errstr = '', $timeout = 10);
136 if (! $source_socket)
138 throw new couchdb_replicator_exception("Couldn't connect to source server at {$this->configuration['source']['host']}:{$this->configuration['source']['port']}");
141 @fclose
($source_socket);
143 $target_socket = @fsockopen
($this->configuration
['target']['host'], $this->configuration
['target']['port'], $errno = -1, $errstr = '', $timeout = 20);
145 if (! $target_socket)
147 throw new couchdb_replicator_exception("Couldn't connect to target server at {$this->configuration['target']['host']}:{$this->configuration['target']['port']}");
150 @fclose
($target_socket);