+static void bytarget_result_ready(void *data)
+{
+ struct http_channel *c = (struct http_channel *) data;
+ struct http_session *s = locate_session(c);
+ const char *status_message = "OK";
+ if (s) {
+ yaz_log(c->http_sessions->log_level, "Session %u: bytarget watch released", s->session_id);
+ bytarget_response(c, s, status_message);
+ release_session(c, s);
+ }
+ else {
+ yaz_log(c->http_sessions->log_level, "No Session found for released bytarget watch");
+ }
+}
+
+
+static void cmd_bytarget(struct http_channel *c)
+{
+ struct http_request *rq = c->request;
+ struct http_response *rs = c->response;
+ struct http_session *s = locate_session(c);
+ const char *block = http_argbyname(rq, "block");
+ const char *report = http_argbyname(rq, "report");
+ int report_error = 0;
+ int report_status = 0;
+ const char *status_message = "OK";
+ int no_active;
+
+ if (report && !strcmp("error", report)) {
+ report_error = 1;
+ }
+ if (report && !strcmp("status", report)) {
+ report_status = 1;
+ }
+
+ if (!s)
+ return;
+
+ no_active = session_active_clients(s->psession);
+ if (block && !strcmp("1",block) && no_active)
+ {
+ // if there is already a watch/block. we do not block this one
+ if (session_set_watch(s->psession, SESSION_WATCH_BYTARGET,
+ bytarget_result_ready, c, c) != 0)
+ {
+ yaz_log(YLOG_WARN, "Session %u: Attempt to block multiple times on bytarget block. Not supported!", s->session_id);
+ if (report_error) {
+ error(rs, PAZPAR2_ALREADY_BLOCKED, "bytarget");
+ release_session(c, s);
+ return;
+ }
+ else if (report_status) {
+ status_message = "WARNING (Already blocked on bytarget)";
+ }
+ else {
+ yaz_log(YLOG_WARN, "Session %u: Ignoring bytarget block. Return current result.", s->session_id);
+ }
+ }
+ else
+ {
+ yaz_log(c->http_sessions->log_level, "Session %u: Blocking on command bytarget", s->session_id);
+ release_session(c, s);
+ return;
+ }
+ }
+ bytarget_response(c, s, status_message);
+ release_session(c, s);