X-Git-Url: http://git.indexdata.com/?p=yazpp-moved-to-github.git;a=blobdiff_plain;f=src%2Fyaz-client.cpp;h=de00fd16d5b8cd79f540e29e1edc15af689e6a4c;hp=e0c26f1752ad914e18de5068d93a841db170f079;hb=e3133d2f2c580f76c9da2e7621c74a064f0955c9;hpb=3109185a2036377894be1dd303d899a305b44281 diff --git a/src/yaz-client.cpp b/src/yaz-client.cpp index e0c26f1..de00fd1 100644 --- a/src/yaz-client.cpp +++ b/src/yaz-client.cpp @@ -1,10 +1,40 @@ /* - * Copyright (c) 1998-1999, Index Data. + * Copyright (c) 1998-2000, Index Data. * See the file LICENSE for details. - * Sebastian Hammer, Adam Dickmeiss * * $Log: yaz-client.cpp,v $ - * Revision 1.6 1999-04-20 10:30:05 adam + * Revision 1.15 2000-10-11 11:58:16 adam + * Moved header files to include/yaz++. Switched to libtool and automake. + * Configure script creates yaz++-config script. + * + * Revision 1.14 2000/09/08 10:23:42 adam + * Added skeleton of yaz-z-server. + * + * Revision 1.13 2000/09/06 14:23:45 adam + * WIN32 updates. + * + * Revision 1.12 2000/09/04 08:59:16 adam + * Changed call to logging functions (yaz_ added). + * + * Revision 1.11 2000/07/04 13:48:49 adam + * Implemented upper-limit on proxy-to-target sessions. + * + * Revision 1.10 2000/05/30 03:12:27 ian + * minor change to stop g++ 2.95.2 complaining about taking the address + * of a member function. + * + * Revision 1.9 1999/12/06 13:52:45 adam + * Modified for new location of YAZ header files. Experimental threaded + * operation. + * + * Revision 1.8 1999/11/10 10:02:34 adam + * Work on proxy. + * + * Revision 1.7 1999/04/21 12:09:01 adam + * Many improvements. Modified to proxy server to work with "sessions" + * based on cookies. + * + * Revision 1.6 1999/04/20 10:30:05 adam * Implemented various stuff for client and proxy. Updated calls * to ODR to reflect new name parameter. * @@ -26,12 +56,12 @@ * */ -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include extern "C" { #if HAVE_READLINE_READLINE_H @@ -68,16 +98,43 @@ public: void recv_genericRecord(Z_GenericRecord *r); void display_genericRecord(Z_GenericRecord *r, int level); void display_variant(Z_Variant *v, int level); + void connectNotify(); + void failNotify(); + void timeoutNotify(); + char *get_cookie (Z_OtherInformation **oi); int processCommand(const char *cmd); const char *MyClient::getCommand(); int cmd_open(char *host); + int cmd_connect(char *host); int cmd_quit(char *args); int cmd_close(char *args); int cmd_find(char *args); int cmd_show(char *args); int cmd_cookie(char *args); + int cmd_init(char *args); + int cmd_format(char *args); + int cmd_proxy(char *args); }; + +void MyClient::connectNotify() +{ + printf ("Connection accepted by target\n"); + set_lastReceived(-1); +} + +void MyClient::timeoutNotify() +{ + printf ("Connection timeout\n"); + close(); +} + +void MyClient::failNotify() +{ + printf ("Connection closed by target\n"); + set_lastReceived(-1); +} + IYaz_PDU_Observer *MyClient::clone(IYaz_PDU_Observable *the_PDU_Observable) { return new MyClient(the_PDU_Observable, m_socketManager); @@ -96,15 +153,40 @@ MyClient::MyClient(IYaz_PDU_Observable *the_PDU_Observable, void usage(char *prog) { - fprintf (stderr, "%s: [-v log] [-p proxy] [zurl]\n", prog); + fprintf (stderr, "%s: [-v log] [-c cookie] [-p proxy] [zurl]\n", prog); exit (1); } +char *MyClient::get_cookie(Z_OtherInformation **otherInfo) +{ + int oid[OID_SIZE]; + Z_OtherInformationUnit *oi; + struct oident ent; + ent.proto = PROTO_Z3950; + ent.oclass = CLASS_USERINFO; + ent.value = (oid_value) VAL_COOKIE; + + if (oid_ent_to_oid (&ent, oid) && + (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) && + oi->which == Z_OtherInfo_characterInfo) + return oi->information.characterInfo; + return 0; +} + void MyClient::recv_initResponse(Z_InitResponse *initResponse) { printf ("Got InitResponse. Status "); if (*initResponse->result) + { printf ("Ok\n"); + + const char *p = get_cookie (&initResponse->otherInfo); + if (p) + { + printf ("cookie = %s\n", p); + set_cookie(p); + } + } else printf ("Fail\n"); } @@ -302,7 +384,7 @@ void MyClient::recv_record(Z_DatabaseRecord *record, int offset, else { printf("Unknown record representation.\n"); - if (!z_External(odr_print(), &r, 0)) + if (!z_External(odr_print(), &r, 0, 0)) { odr_perror(odr_print(), "Printing external"); odr_reset(odr_print()); @@ -322,7 +404,11 @@ void MyClient::recv_namePlusRecord (Z_NamePlusRecord *zpr, int offset) void MyClient::recv_records (Z_Records *records) { +#ifdef ASN_COMPILED Z_DiagRec dr, *dr_p = &dr; +#endif + if (!records) + return; int i; switch (records->which) { @@ -358,8 +444,7 @@ void MyClient::recv_searchResponse(Z_SearchResponse *searchResponse) } printf ("Ok\n"); printf ("Hits: %d\n", *searchResponse->resultCount); - if (searchResponse->records) - recv_records (searchResponse->records); + recv_records (searchResponse->records); } void MyClient::recv_presentResponse(Z_PresentResponse *presentResponse) @@ -381,9 +466,28 @@ int MyClient::wait() #define C_PROMPT "Z>" +int MyClient::cmd_connect(char *host) +{ + client (host); + timeout (10); + wait (); + timeout (0); + return 1; +} + int MyClient::cmd_open(char *host) { client (host); + timeout (10); + wait (); + timeout (0); + send_initRequest(); + wait (); + return 1; +} + +int MyClient::cmd_init(char *args) +{ if (send_initRequest() >= 0) wait(); else @@ -413,6 +517,8 @@ int MyClient::cmd_find(char *args) } if (send_searchRequest(&query) >= 0) wait(); + else + printf ("Not connected\n"); return 1; } @@ -421,14 +527,29 @@ int MyClient::cmd_show(char *args) int start = m_setOffset, number = 1; sscanf (args, "%d %d", &start, &number); + m_setOffset = start; if (send_presentRequest(start, number) >= 0) wait(); + else + printf ("Not connected\n"); return 1; } int MyClient::cmd_cookie(char *args) { - set_cookie(args); + set_cookie(*args ? args : 0); + return 1; +} + +int MyClient::cmd_format(char *args) +{ + set_preferredRecordSyntax(args); + return 1; +} + +int MyClient::cmd_proxy(char *args) +{ + set_proxy(args); return 1; } @@ -442,12 +563,16 @@ int MyClient::processCommand(const char *commandLine) int (MyClient::*fun)(char *arg); char *ad; } cmd[] = { - {"open", &cmd_open, "[':'][/]"}, - {"quit", &cmd_quit, ""}, - {"close", &cmd_close, ""}, - {"find", &cmd_find, ""}, - {"show", &cmd_show, "[ []]"}, - {"cookie", &cmd_cookie, ""}, + {"open", &MyClient::cmd_open, "[':'][/]"}, + {"connect", &MyClient::cmd_connect, "[':'][/]"}, + {"quit", &MyClient::cmd_quit, ""}, + {"close", &MyClient::cmd_close, ""}, + {"find", &MyClient::cmd_find, ""}, + {"show", &MyClient::cmd_show, "[ []]"}, + {"cookie", &MyClient::cmd_cookie, ""}, + {"init", &MyClient::cmd_init, ""}, + {"format", &MyClient::cmd_format, ""}, + {"proxy", &MyClient::cmd_proxy, ":[':']"}, {0,0,0} }; @@ -529,7 +654,7 @@ int MyClient::args(Yaz_SocketManager *socketManager, int argc, char **argv) char *prog = argv[0]; int ret; - while ((ret = options("p:v:q", argv, argc, &arg)) != -2) + while ((ret = options("c:p:v:q", argv, argc, &arg)) != -2) { switch (ret) { @@ -549,8 +674,11 @@ int MyClient::args(Yaz_SocketManager *socketManager, int argc, char **argv) } set_proxy(arg); break; + case 'c': + set_cookie(arg); + break; case 'v': - log_init_level (log_mask_str(arg)); + yaz_log_init_level (yaz_log_mask_str(arg)); break; case 'q': m_interactive_flag = 0; @@ -563,6 +691,9 @@ int MyClient::args(Yaz_SocketManager *socketManager, int argc, char **argv) if (host) { client (host); + timeout (10); + wait (); + timeout (0); send_initRequest(); wait (); } @@ -572,7 +703,7 @@ int MyClient::args(Yaz_SocketManager *socketManager, int argc, char **argv) int main(int argc, char **argv) { Yaz_SocketManager mySocketManager; - Yaz_PDU_Assoc *some = new Yaz_PDU_Assoc(&mySocketManager, 0); + Yaz_PDU_Assoc *some = new Yaz_PDU_Assoc(&mySocketManager); MyClient z(some, &mySocketManager); @@ -580,4 +711,5 @@ int main(int argc, char **argv) exit (1); if (z.interactive(&mySocketManager)) exit (1); + return 0; }