+proc init-m-response {i} {
+ global zstatus
+ global zleft
+
+ wlog debug "init-m-response"
+
+ set zstatus($i) 1
+ incr zleft -1
+}
+
+proc connect-m-response {i} {
+ global zstatus
+ global zleft
+
+ wlog debug "connect-m-response"
+ z39$i callback [list init-m-response $i]
+ if {[catch {z39$i init}]} {
+ set zstatus($i) -1
+ incr zleft -1
+ }
+}
+
+proc fail-m-response {i} {
+ global zstatus
+ global zleft
+
+ wlog debug "fail-m-response"
+ set zstatus($i) -1
+ incr zleft -1
+}
+
+proc search-m-response {setNo i} {
+ global zleft
+ global zstatus
+
+ incr zleft -1
+ set zstatus($i) 2
+}
+
+proc z39msearch {setNo piggy} {
+ global zleft
+ global zstatus
+ global hist
+
+ set not $hist($setNo,0,host)
+
+ for {set i 1} {$i <= $not} {incr i} {
+ set host $hist($setNo,$i,host)
+ if {[catch {z39 failback fail-response}]} {
+ ir z39$i
+ }
+ if {[catch {set oldHost [z39$i connect]}]} {
+ set oldHost ""
+ }
+ if {$oldHost != $host} {
+ catch {z39$i disconnect}
+ }
+ z39$i callback [list connect-m-response $i]
+ z39$i failback [list fail-m-response $i]
+ }
+ set zleft 0
+ for {set i 1} {$i <= $not} {incr i} {
+ set oldHost [z39$i connect]
+ set host $hist($setNo,$i,host)
+ if {$oldHost == $host} {
+ set zstatus($i) 1
+ continue
+ }
+ html "Connecting to target " $host " <br>\n"
+ set zstatus($i) -1
+ if {![catch {z39$i connect $host}]} {
+ incr zleft
+ }
+ }
+ while {$zleft > 0} {
+ wlog debug "Waiting for init response"
+ if {[catch {zwait zleft 10}]} {
+ break
+ }
+ }
+ set zleft 0
+ for {set i 1} {$i <= $not} {incr i} {
+ html "host " $hist($setNo,$i,host) ": "
+ if {$zstatus($i) >= 1} {
+ html "ok <br>\n"
+ ir-set z39$i.$setNo z39$i
+ set hist($setNo,$i,offset) 0
+ eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
+ z39$i.$setNo preferredRecordSyntax USMARC
+ z39$i callback [list search-m-response $setNo $i]
+
+ if {$piggy} {
+ z39$i.$setNo largeSetLowerBound 999999
+ z39$i.$setNo smallSetUpperBound 0
+ z39$i.$setNo mediumSetPresentNumber $hist($setNo,maxPresent)
+ } else {
+ z39$i.$setNo largeSetLowerBound 2
+ z39$i.$setNo smallSetUpperBound 0
+ z39$i.$setNo mediumSetPresentNumber 0
+ }
+ set zstatus($i) 1
+ wlog debug "search " $hist($setNo,$i,query)
+ z39$i.$setNo search $hist($setNo,$i,query)
+ incr zleft
+ } else {
+ html "fail <br>\n"
+ }
+ }
+ while {$zleft > 0} {
+ wlog debug "Waiting for search response"
+ if {[catch {zwait zleft 30}]} {
+ break
+ }
+ }
+ for {set i 1} {$i <= $not} {incr i} {
+ if {$zstatus($i) != 2} continue
+ set status [z39$i.$setNo responseStatus]
+ if {[lindex $status 0] != "NSD"} {
+ set hist($setNo,$i,offset) [z39$i.$setNo numberOfRecordsReturned]
+ }
+ }
+}
+