Added base version of the Ajatus PHP -library.
[ajatus.git] / plugins / ajatus / couchdb / replicator / http.php
blob6f1917de9aa6eb6dfc0978504fc5fb852086c3e9
1 <?php
2 /**
3 *
4 * Copyright (c) 2007 Jerry Jalava <jerry.jalava@gmail.com>
5 * Licensed under the GPLv3 license
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 */
10 class couchdb_replicator_http extends couchdb_replicator implements couchdb_replicator_interface
12 public function replicate()
14 $this->can_connect();
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']}",
19 ));
21 $transport = new couchdb_transport($this->local_configuration);
23 try
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}");
32 $to_results = false;
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']}",
40 ));
42 try
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}");
52 return array(
53 'from' => $from_results,
54 'to' => $to_results
58 public function check_config()
60 if (! isset($this->configuration['source']['host']))
62 if (empty($this->local_configuration['host']))
64 throw new couchdb_replicator_exception('No source host found in configuration!');
67 $this->configuration['source']['host'] = $this->local_configuration['host'];
70 if (! isset($this->configuration['source']['port']))
72 if (empty($this->local_configuration['port']))
74 throw new couchdb_replicator_exception('No source port found in configuration!');
77 $this->configuration['source']['port'] = $this->local_configuration['port'];
80 if (! isset($this->configuration['source']['db']))
82 throw new couchdb_replicator_exception('No source db found in configuration!');
85 if (! isset($this->configuration['target']['host']))
87 if (empty($this->local_configuration['host']))
89 throw new couchdb_replicator_exception('No target host found in configuration!');
92 $this->configuration['target']['host'] = $this->local_configuration['host'];
95 if (! isset($this->configuration['target']['port']))
97 if (empty($this->local_configuration['port']))
99 throw new couchdb_replicator_exception('No target port found in configuration!');
102 $this->configuration['target']['port'] = $this->local_configuration['port'];
105 if (! isset($this->configuration['target']['db']))
107 throw new couchdb_replicator_exception('No target db found in configuration!');
110 if (! isset($this->configuration['setup']))
112 $this->configuration['setup'] = array(
113 'one_way' => false
117 return true;
120 private function can_connect()
122 $source_socket = @fsockopen($this->configuration['source']['host'], $this->configuration['source']['port'], $errno = -1, $errstr = '', $timeout = 10);
124 if (! $source_socket)
126 throw new couchdb_replicator_exception("Couldn't connect to source server at {$this->configuration['source']['host']}:{$this->configuration['source']['port']}");
129 @fclose($source_socket);
131 $target_socket = @fsockopen($this->configuration['target']['host'], $this->configuration['target']['port'], $errno = -1, $errstr = '', $timeout = 20);
133 if (! $target_socket)
135 throw new couchdb_replicator_exception("Couldn't connect to target server at {$this->configuration['target']['host']}:{$this->configuration['target']['port']}");
138 @fclose($target_socket);
140 return true;