Added second part of helper functions for CharSetandLanguageNegotaiation-3 Model.
authoroleg <oleg>
Sun, 19 May 2002 15:39:54 +0000 (15:39 +0000)
committeroleg <oleg>
Sun, 19 May 2002 15:39:54 +0000 (15:39 +0000)
Work with client/zoomsh.

client/client.c
include/yaz/charneg.h
zoom/zoom-c.c
zutil/charneg.c

index 4aa5ea6..85063a5 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 1995-2002, Index Data
  * See the file LICENSE for details.
  *
- * $Id: client.c,v 1.152 2002-05-18 09:52:37 oleg Exp $
+ * $Id: client.c,v 1.153 2002-05-19 15:39:54 oleg Exp $
  */
 
 #include <stdio.h>
@@ -218,9 +218,9 @@ static void send_initRequest(const char* type_and_host)
                
                p0->which = Z_OtherInfo_externallyDefinedInfo;
                p0->information.externallyDefinedInfo =
-                       yaz_set_charset_and_lang(out, CLASS_NEGOT, VAL_CHARNEG3,
+                       yaz_set_proposal_charneg(out,
                                (const char**)&yazCharset, (yazCharset)?1:0,
-                               (const char**)&yazLang, (yazLang)?1:0);
+                               (const char**)&yazLang, (yazLang)?1:0, 1);
        }
     }
     
@@ -301,6 +301,24 @@ static int process_initResponse(Z_InitResponse *res)
     if (ODR_MASK_GET(res->options, Z_Options_queryType104))
         printf (" queryType104");
     printf ("\n");
+    
+    if (ODR_MASK_GET(res->options, Z_Options_negotiationModel)) {
+    
+       Z_CharSetandLanguageNegotiation *p =
+               yaz_get_charneg_record(res->otherInfo);
+       
+       if (p) {
+       
+               char *charset, *lang;
+               int selected;
+               
+               yaz_get_response_charneg(session_mem, p, &charset, &lang, &selected);
+               
+               printf("Accepted character set : `%s'\n", charset);
+               printf("Accepted code language : `%s'\n", lang);
+               printf("Accepted records in ...: %d\n", selected );
+       }
+    }
     fflush (stdout);
     return 0;
 }
index bc5d5e7..fb91ef1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: charneg.h,v 1.1 2002-05-18 09:41:36 oleg Exp $
+ * $Id: charneg.h,v 1.2 2002-05-19 15:39:54 oleg Exp $
  */
 #ifndef CHARNEG_H
 #define CHARNEG_H
@@ -8,9 +8,44 @@
 
 YAZ_BEGIN_CDECL
 
-YAZ_EXPORT Z_External *yaz_set_charset_and_lang(ODR odr, int oid_class, int oid_value,
-       const char **charsets, int num_charsets,
-       const char **langs, int num_langs);
+/*
+ * Character set and language negotiation model - 3 (*)
+ *
+ * These a set helper functions for support (*). There are
+ * client and server parts. At that moment the (*) is based on
+ * Z_PrivateCharacterSet_externallySpecified method.
+ *
+ */  
+
+/*
+ * Get (*) record from request
+ */
+YAZ_EXPORT Z_CharSetandLanguageNegotiation *
+       yaz_get_charneg_record(Z_OtherInformation *p);
+
+/*
+ * Client's part. 
+ */
+YAZ_EXPORT Z_External *yaz_set_proposal_charneg(ODR odr,
+       const char **charsets, int num_charsets, const char **langs,
+               int num_langs, int selected);
+
+YAZ_EXPORT void yaz_get_response_charneg(NMEM mem,
+       Z_CharSetandLanguageNegotiation *p, char **charset, char **lang,
+               int *selected);
+
+/*
+ * Server's part
+ */
+YAZ_EXPORT Z_External *yaz_set_response_charneg(ODR odr,
+       const char *charset, const char *lang, int selected);
+
+
+YAZ_EXPORT void yaz_get_proposal_charneg(NMEM mem,
+       Z_CharSetandLanguageNegotiation *p, char **charsets, int *num_charsets,
+               char **langs, int *num_langs, int *selected);
+
 
 YAZ_END_CDECL
 
index 647774f..267bb2c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: zoom-c.c,v 1.29 2002-05-18 09:52:37 oleg Exp $
+ * $Id: zoom-c.c,v 1.30 2002-05-19 15:39:54 oleg Exp $
  *
  * ZOOM layer for C, connections, result sets, queries.
  */
@@ -735,10 +735,9 @@ static int ZOOM_connection_send_init (ZOOM_connection c)
                
                oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
                oi_unit->information.externallyDefinedInfo =
-                       yaz_set_charset_and_lang(c->odr_out,
-                               CLASS_NEGOT, VAL_CHARNEG3,
+                       yaz_set_proposal_charneg(c->odr_out,
                                (const char **)&c->charset, (c->charset) ? 1:0,
-                               (const char **)&c->lang, (c->lang) ? 1:0);
+                               (const char **)&c->lang, (c->lang) ? 1:0, 1);
        }
     }
     assert (apdu);
@@ -1624,6 +1623,24 @@ static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
             }
            ZOOM_connection_exec_task (c);
        }
+       if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
+       {
+               NMEM tmpmem = nmem_create();
+               Z_CharSetandLanguageNegotiation *p =
+                       yaz_get_charneg_record(initrs->otherInfo);
+               
+               if (p)
+               {
+                       char *charset, *lang;
+                       int selected;
+                       
+                       yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected);
+                       yaz_log(LOG_DEBUG, "Target accepted: charset - %s, language - %s, select - %d",
+                               charset, lang, selected);
+                       
+                       nmem_destroy(tmpmem);
+               }
+       }       
        break;
     case Z_APDU_searchResponse:
        handle_search_response (c, apdu->u.searchResponse);
index 7e5ac4d..25c99c6 100644 (file)
@@ -1,10 +1,10 @@
 /* 
- $ $Id: charneg.c,v 1.1 2002-05-18 09:41:11 oleg Exp $
+ $ $Id: charneg.c,v 1.2 2002-05-19 15:39:54 oleg Exp $
  * Helper functions for Character Set and Language Negotiation - 3
  */
 
 #include <stdio.h>
-#include <yaz/proto.h>
+#include <yaz/otherinfo.h>
 #include <yaz/z-charneg.h>
 #include <yaz/charneg.h>
 
@@ -56,38 +56,43 @@ static Z_OriginProposal_0 *z_get_OriginProposal_0(ODR o, const char *charset)
        return p0;
 }
 static Z_OriginProposal *z_get_OriginProposal(ODR o, const char **charsets,
-       int num_charsets, const char **langs, int num_langs)
+       int num_charsets, const char **langs, int num_langs, int selected)
 {      
        int i;
        Z_OriginProposal *p = (Z_OriginProposal *) odr_malloc(o, sizeof(*p));
                
        memset(p, 0, sizeof(*p));
-               
-       p->num_proposedCharSets = num_charsets;
-       p->num_proposedlanguages = num_langs;
+
        p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
-       *p->recordsInSelectedCharSets = 1;
+       *p->recordsInSelectedCharSets = (selected) ? 1:0;
+
+       if (charsets && num_charsets) {         
        
-       p->proposedCharSets = 
+               p->num_proposedCharSets = num_charsets;
+               p->proposedCharSets = 
                        (Z_OriginProposal_0**) odr_malloc(o,
                                num_charsets*sizeof(Z_OriginProposal_0*));
 
-       for (i = 0; i<num_charsets; i++) {
+               for (i = 0; i<num_charsets; i++) {
 
-               p->proposedCharSets[i] =
-                       z_get_OriginProposal_0(o, charsets[i]);
+                       p->proposedCharSets[i] =
+                               z_get_OriginProposal_0(o, charsets[i]);
                        
+               }
        }
-       p->proposedlanguages = 
+       if (langs && num_langs) {
+       
+               p->num_proposedlanguages = num_langs;
+
+               p->proposedlanguages = 
                        (char **) odr_malloc(o, num_langs*sizeof(char *));
 
-       for (i = 0; i<num_langs; i++) {
-               char *plang = (char *)langs[i];
+               for (i = 0; i<num_langs; i++) {
 
-               p->proposedlanguages[i] = plang;
+                       p->proposedlanguages[i] = (char *)langs[i];
                        
+               }
        }
-
        return p;
 }
 static Z_CharSetandLanguageNegotiation *z_get_CharSetandLanguageNegotiation(ODR o)
@@ -99,9 +104,8 @@ static Z_CharSetandLanguageNegotiation *z_get_CharSetandLanguageNegotiation(ODR
        
        return p;
 }
-Z_External *yaz_set_charset_and_lang(ODR o, int oid_class, int oid_value,
-       const char **charsets, int num_charsets,
-       const char **langs, int num_langs)
+Z_External *yaz_set_proposal_charneg(ODR o, const char **charsets, int num_charsets,
+       const char **langs, int num_langs, int selected)
 {
        Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
        oident oid;
@@ -110,8 +114,8 @@ Z_External *yaz_set_charset_and_lang(ODR o, int oid_class, int oid_value,
        p->indirect_reference = 0;      
 
        oid.proto = PROTO_Z3950;
-       oid.oclass = oid_class;
-       oid.value = oid_value;
+       oid.oclass = CLASS_NEGOT;
+       oid.value = VAL_CHARNEG3;
        p->direct_reference = odr_oiddup(o, oid_getoidbyent(&oid));
 
        p->which = Z_External_charSetandLanguageNegotiation;
@@ -119,7 +123,156 @@ Z_External *yaz_set_charset_and_lang(ODR o, int oid_class, int oid_value,
        p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_proposal;
        p->u.charNeg3->u.proposal =
                z_get_OriginProposal(o, charsets, num_charsets,
-                       langs, num_langs);
+                       langs, num_langs, selected);
+
+       return p;
+}
+static Z_TargetResponse *z_get_TargetResponse(ODR o, const char *charset,
+       const char *lang, int selected)
+{      
+       int i;
+       Z_TargetResponse *p = (Z_TargetResponse *) odr_malloc(o, sizeof(*p));
+       Z_PrivateCharacterSet *pc =
+                               (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
+               
+       memset(p, 0, sizeof(*p));
+       memset(pc, 0, sizeof(*pc));
+       
+       p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
+       *p->recordsInSelectedCharSets = (selected) ? 1:0;
+       p->selectedLanguage = (char *)odr_strdup(o, lang);
+       
+       p->which = Z_TargetResponse_private;
+       p->u.zprivate = pc;
+       
+       pc->which = Z_PrivateCharacterSet_externallySpecified;
+       pc->u.externallySpecified =
+                       z_ext_record2(o, CLASS_RECSYN, VAL_NOP, charset, (strlen(charset)+1));
+       return p;
+}
+Z_External *yaz_set_response_charneg(ODR o, const char *charset,
+       const char *lang, int selected)
+{
+       Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
+       oident oid;
+       
+       p->descriptor = 0;
+       p->indirect_reference = 0;      
+
+       oid.proto = PROTO_Z3950;
+       oid.oclass = CLASS_NEGOT;
+       oid.value = VAL_CHARNEG3;
+       p->direct_reference = odr_oiddup(o, oid_getoidbyent(&oid));
+
+       p->which = Z_External_charSetandLanguageNegotiation;
+       p->u.charNeg3 = z_get_CharSetandLanguageNegotiation(o);
+       p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_response;
+       p->u.charNeg3->u.response = z_get_TargetResponse(o, charset, lang, selected);
 
        return p;
 }
+Z_CharSetandLanguageNegotiation *yaz_get_charneg_record(Z_OtherInformation *p)
+{
+       Z_External *pext;
+       int i;
+       
+       if(!p)
+               return 0;
+       
+       for (i=0; i<p->num_elements; i++) {
+       
+               if ((p->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
+                                       (pext = p->list[i]->information.externallyDefinedInfo)) {
+                                       
+                       oident *ent = oid_getentbyoid(pext->direct_reference);
+                       
+                       if (ent && ent->value == VAL_CHARNEG3 && ent->oclass == CLASS_NEGOT &&
+                               pext->which == Z_External_charSetandLanguageNegotiation) {
+                               
+                               return pext->u.charNeg3;
+                       }
+               }
+       }
+       
+       return 0;
+}
+void yaz_get_proposal_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p, char **charsets,
+       int *num_charsets, char **langs, int *num_langs, int *selected)
+{
+       int i;
+       Z_OriginProposal *pro = p->u.proposal;
+       
+       if (pro->num_proposedCharSets && charsets && num_charsets) {
+       
+               *num_charsets = pro->num_proposedCharSets;
+       
+               charsets = (char **)nmem_malloc(mem, pro->num_proposedCharSets * sizeof(char *));
+               
+               for (i=0; i<pro->num_proposedCharSets; i++) {
+               
+                       if (pro->proposedCharSets[i]->which == Z_OriginProposal_0_private &&
+                               pro->proposedCharSets[i]->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified) {
+                                       
+                               Z_External *pext =
+                                       pro->proposedCharSets[i]->u.zprivate->u.externallySpecified;
+                               
+                               if (pext->which == Z_External_octet) {
+                       
+                                       charsets[i] = (char *)nmem_malloc(mem, pext->u.octet_aligned->len * sizeof(char));
+                                       
+                                       memcpy (charsets[i], pext->u.octet_aligned->buf, pext->u.octet_aligned->len);
+                                       
+                               }
+                       }
+               }
+       }
+
+       if (pro->num_proposedlanguages && langs && num_langs) {
+
+               *num_langs = pro->num_proposedlanguages;
+       
+               langs = (char **)nmem_malloc(mem, pro->num_proposedlanguages * sizeof(char *));
+               
+               for (i=0; i<pro->num_proposedlanguages; i++) {
+
+                       langs[i] = (char *)nmem_malloc(mem, strlen(pro->proposedlanguages[i])+1);
+                       memcpy (langs[i], pro->proposedlanguages[i], strlen(pro->proposedlanguages[i])+1);              
+               }
+       }
+       
+       if(pro->recordsInSelectedCharSets && selected) {
+       
+               *selected = *pro->recordsInSelectedCharSets;
+               
+       }
+}
+void yaz_get_response_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
+       char **charset, char **lang, int *selected)
+{
+       Z_TargetResponse *res = p->u.response;
+       
+       if (charset && res->which == Z_TargetResponse_private &&
+               res->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified) {
+
+               Z_External *pext = res->u.zprivate->u.externallySpecified;
+                               
+               if (pext->which == Z_External_octet) {
+                       
+                       *charset = (char *)nmem_malloc(mem, pext->u.octet_aligned->len * sizeof(char));
+                       memcpy (*charset, pext->u.octet_aligned->buf, pext->u.octet_aligned->len);
+               }       
+       }
+
+       if (lang && res->selectedLanguage) {
+       
+               *lang = (char *)nmem_malloc(mem, strlen(res->selectedLanguage)+1);
+               memcpy (*lang, res->selectedLanguage, strlen(res->selectedLanguage)+1);
+       
+       }
+
+       if(selected && res->recordsInSelectedCharSets) {
+       
+               *selected = *res->recordsInSelectedCharSets;
+
+       }
+}