Minor changes.
[egate.git] / www / z39util.tcl
index 19b9a0e..6b56df4 100644 (file)
@@ -1,5 +1,5 @@
 #
-# $Id: z39util.tcl,v 1.1 1995/11/06 17:44:23 adam Exp $
+# $Id: z39util.tcl,v 1.5 1995/11/08 18:07:23 adam Exp $
 #
 proc saveState {} {
     uplevel #0 {
@@ -8,15 +8,18 @@ proc saveState {} {
         if {$var == "f"} continue
         if {$var == "sessionId"} continue
         if {$var == "errorInfo"} continue
-       set names [array names $var]
-        if {$names != ""} {
+       if {[catch {set names [array names $var]}]} {
+            eval "set v \$${var}"
+            puts $f "set ${var} \{$v\}"
+        } else {
            foreach n $names {
                 eval "set v \$${var}(\$n)"
                puts $f "set ${var}($n) \{$v\}"
            }
-        } else {
-            eval "set v \$${var}"
-            puts $f "set ${var} \{$v\}"
+            catch {
+                eval "set v \$${var}"
+                puts $f "set ${var} \{$v\}"
+            }
         }
     }
     close $f
@@ -71,18 +74,18 @@ proc display-brief {zset no} {
     set rtype [$zset recordType $no]
     if {$rtype == "SUTRS"} {
         html [join [$zset getSutrs $no]]
-        htmlr {<br>}
+        html "<br>\n"
         return
     } 
     if {![catch {
         set title [lindex [$zset getMarc $no field 245 * a] 0]
         set year [lindex [$zset getMarc $no field 260 * c] 0]
     } ] } {
-        html {<a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME) /
+        html {<a href="http:} $env(SCRIPT_NAME) /
         html $sessionId {/showfull.egw/} $setNo + $no {"> } $title {</a>}
         html " <i> ${year} </i>"
     }
-    htmlr {<br>}
+    html "<br>\n"
 }
 
 proc display-full {zset no} {
@@ -93,21 +96,21 @@ proc display-full {zset no} {
         if {$add != {}} {
             set add " :${add}"
         }
-        htmlr "<hr> ${no} <br>"
-        htmlr "Error ${err}${add} <br>"
+        html "<h3>${no}</h3>\n"
+        html "Error ${err}${add} <br>\n"
         return
     }
     if {$type != "DB"} {
         return
     }
-    htmlr "<hr> ${no} <br>"
+    html "<h3>${no}</h3>\n"
     set rtype [$zset recordType $no]
     if {$rtype == "SUTRS"} {
-        htmlr [join [$zset getSutrs $no]]
+        html [join [$zset getSutrs $no]] "<br>\n"
         return
     } 
     if {[catch {set r [$zset getMarc $no line * * *]}]} {
-        htmlr "Unknown record type: $rtype"
+        html "Unknown record type: $rtype <br>\n"
         return
     }
     foreach line $r {
@@ -118,8 +121,8 @@ proc display-full {zset no} {
         html "$tag "
         if {$l > 0} {
             for {set i 0} {$i < $l} {incr i} {
-                if {[string index $tag $i] == " "} {
-                    html "_"
+                if {[string index $indicator $i] == " "} {
+                    html "-"
                 } else {
                     html [string index $tag $i]
                 }
@@ -137,18 +140,17 @@ proc display-full {zset no} {
     }
 }
 
-proc display-rec {from to dfunc} {
+proc display-rec {from to dfunc zz} {
     global setNo
 
     while {$from <= $to} { 
-        eval "$dfunc z39.$setNo $from"
+        eval "$dfunc $zz.$setNo $from"
         incr from
     }
 }
 
-proc build-query {} {
+proc build-query {t} {
     global targets
-    global t
 
     set op {}
     set q {}
@@ -206,14 +208,20 @@ proc z39search {setNo piggy} {
                 return 0
             }
         }
+        z39 idAuthentication $hist($setNo,idAuthentication)
         set sessionWait 0
         if {[catch {z39 init}]} {
             html "Cannot initialize with target ${host} <br>\n"
             return 0
         }
-        zwait sessionWait
+        if {[catch {zwait sessionWait 60}]} {
+            html "Cannot initialize with target ${host} <br>\n"
+           z39 disconnect
+            return 0
+        }
         if {$sessionWait != "1"} {
             html "Cannot initialize with target ${host} <br>\n"
+           z39 disconnect
             return 0
         }
     }
@@ -238,9 +246,15 @@ proc z39search {setNo piggy} {
     set sessionWait 0
     z39.$setNo search $hist($setNo,query)
 
-    zwait sessionWait
+    if {[catch {zwait sessionWait 600}]} {
+        html "</body></html>\n"
+        z39 disconnect
+        return 0
+    }
+        
     if {$sessionWait != 1} {
         html "</body></html>\n"
+        z39 disconnect
         return 0
     }
     set status [z39.$setNo responseStatus]
@@ -255,6 +269,129 @@ proc z39search {setNo piggy} {
     return 1
 }
 
+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]
+        }
+    }
+}
+
 proc z39present {setNo setOffset setMax dfunc} {
     global hist
     global sessionWait
@@ -269,13 +406,16 @@ proc z39present {setNo setOffset setMax dfunc} {
         if {$got < $toGet} {
             set sessionWait 0
             z39.$setNo present $setOffset $toGet
-            zwait sessionWait
+            if {[catch {zwait sessionWait 300}]} {
+               z39 disconnect
+                break
+           }
             if {$sessionWait != "1"} {
                 break
             }
             set got [z39.$setNo numberOfRecordsReturned]
         }
-        display-rec $setOffset [expr $got + $setOffset - 1] $dfunc
+        display-rec $setOffset [expr $got + $setOffset - 1] $dfunc z39
         set setOffset [expr $got + $setOffset]
         set toGet [expr 1 + $setMax - $setOffset]
         wflush
@@ -287,16 +427,17 @@ proc z39history {} {
     global hist
     global env
     global sessionId
+    global targets
 
     if {![info exists nextSetNo]} {
         return
     }
     html "<hr><h3>History</h3><dl>\n"
     for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
-        html {<dt> <a href="http://} $env(SERVER_NAME) $env(SCRIPT_NAME)
+        html {<dt> <a href="http:} $env(SCRIPT_NAME)
         html / $sessionId {/search.egw/} $setNo + 1
         html + [expr $hist($setNo,maxPresent) - 1]
-        html {"> } $hist($setNo,host)
+        html {"> } [lindex $targets($hist($setNo,host)) 0]
         if {[llength $hist($setNo,database)] > 1} {
             html ": "
             foreach b $hist($setNo,database) {
@@ -313,4 +454,4 @@ proc z39history {} {
         html "\n"
     }
     html "</dl>\n"
-}
\ No newline at end of file
+}