-
Notifications
You must be signed in to change notification settings - Fork 1
/
hl_rpc_client.php
147 lines (132 loc) · 4.2 KB
/
hl_rpc_client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
// PHP binding of Librarian RPC interface
//
// The functions return a $reply object:
// $reply->success: 1 if success, 0 if failure
// $reply->message: if failure, error message
// $reply->id: DB of created record if any
require_once("hera_util.inc");
$hl_config = get_client_config();
function ret_struct($success, $message) {
$ret = new StdClass;
$ret->success = $success;
$ret->message = $message;
return $ret;
}
function get_site($site_name) {
global $hl_config;
if (!array_key_exists($site_name, $hl_config->sites)) {
return null;
}
return $hl_config->sites->$site_name;
}
function hl_do_http_post($req, $site) {
$url = "$site->url/hl_rpc_handler.php";
$req->authenticator = $site->authenticator;
$req_json = json_encode($req);
$post_args = array();
$post_args['request'] = $req_json;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_args);
$reply_json = curl_exec($ch);
curl_close($ch);
$ret = json_decode($reply_json);
if ($ret) return $ret;
$ret = new StdClass;
return ret_struct(false, "can't parse JSON reply: $reply_json");
}
function create_observation(
$site_name, $obs_id, $julian_date, $polarization, $length
) {
$site = get_site($site_name);
if (!$site) return ret_struct(false, "No such site $site_name");
$req = new StdClass;
$req->operation = 'create_observation';
$req->id = $obs_id;
$req->julian_date = $julian_date;
$req->polarization = $polarization;
$req->length = $length;
return hl_do_http_post($req, $site);
}
function create_file(
$site_name, $store_name, $file_name, $type, $obs_id, $size, $md5
) {
$site = get_site($site_name);
if (!$site) return ret_struct(false, "No such site $site_name");
$req = new StdClass;
$req->operation = 'create_file';
$req->store_name = $store_name;
$req->file_name = $file_name;
$req->type = $type;
$req->obs_id = $obs_id;
$req->size = $size;
$req->md5 = $md5;
return hl_do_http_post($req, $site);
}
function delete_file(
$site_name, $file_name, $store_name
) {
$site = get_site($site_name);
if (!$site) return ret_struct(false, "No such site $site_name");
$req = new StdClass;
$req->operation = 'delete_file';
$req->name = $file_name;
$req->store_name = $store_name;
return hl_do_http_post($req, $site);
}
// get list of stores at a site
//
function get_store_list($site_name) {
$site = get_site($site_name);
if (!$site) return ret_struct(false, "No such site $site_name");
$req = new StdClass;
$req->operation = 'get_store_list';
return hl_do_http_post($req, $site);
}
// wrapper around the above to look up a particular store
//
function lookup_store($site_name, $store_name) {
$ret = get_store_list($site_name);
if (!$ret->success) return $ret;
foreach ($ret->stores as $store) {
if ($store_name == $store->name) {
$ret->store = $store;
return $ret;
}
}
$ret->success = false;
$ret->message = "no such store";
return $ret;
}
// get the recommended store for a file of given size
//
function recommended_store($site_name, $file_size) {
$site = get_site($site_name);
if (!$site) return ret_struct(false, "No such site $site_name");
$req = new StdClass;
$req->file_size = $file_size;
$req->operation = 'recommended_store';
return hl_do_http_post($req, $site);
}
function create_copy_task(
$task_type,
$local_site_name, $local_store_name, $file_name,
$remote_site_name, $remote_store_name,
$delete_when_done
) {
$site = get_site($local_site_name);
if (!$site) return ret_struct(false, "No such site $local_site_name");
$req = new StdClass;
$req->operation = 'create_copy_task';
$req->task_type = $task_type;
$req->local_store_name = $local_store_name;
$req->file_name = $file_name;
$req->remote_site_name = $remote_site_name;
$req->remote_store_name = $remote_store_name;
$req->delete_when_done = $delete_when_done?1:0;
return hl_do_http_post($req, $site);
}
?>