Implemented various stuff for client and proxy. Updated calls
[yazpp-moved-to-github.git] / src / yaz-proxy.cpp
1 /*
2  * Copyright (c) 1998-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  * 
6  * $Log: yaz-proxy.cpp,v $
7  * Revision 1.4  1999-04-20 10:30:05  adam
8  * Implemented various stuff for client and proxy. Updated calls
9  * to ODR to reflect new name parameter.
10  *
11  * Revision 1.3  1999/04/09 11:46:57  adam
12  * Added object Yaz_Z_Assoc. Much more functional client.
13  *
14  * Revision 1.2  1999/01/28 13:08:46  adam
15  * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
16  * yaz-socket-manager.cc.
17  *
18  * Revision 1.1.1.1  1999/01/28 09:41:07  adam
19  * First implementation of YAZ++.
20  *
21  */
22
23 #include <assert.h>
24
25 #include <log.h>
26
27 #include <yaz-proxy.h>
28
29 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
30     Yaz_Z_Assoc(the_PDU_Observable)
31 {
32     m_PDU_Observable = the_PDU_Observable;
33     m_client = 0;
34 }
35
36 Yaz_Proxy::~Yaz_Proxy()
37 {
38 }
39
40 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
41                                     *the_PDU_Observable)
42 {
43     return new Yaz_Proxy(the_PDU_Observable);
44 }
45
46 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
47 {
48     int oid[OID_SIZE];
49     Z_OtherInformationUnit *oi;
50     struct oident ent;
51     ent.proto = PROTO_Z3950;
52     ent.oclass = CLASS_USERINFO;
53     ent.value = (oid_value) VAL_COOKIE;
54     if (oid_ent_to_oid (&ent, oid) &&
55         (oi = update_otherInformation(otherInfo, 0, oid, 1)) &&
56         oi->which == Z_OtherInfo_characterInfo)
57         return oi->information.characterInfo;
58     return 0;
59 }
60
61 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
62 {
63     int oid[OID_SIZE];
64     Z_OtherInformationUnit *oi;
65     struct oident ent;
66     ent.proto = PROTO_Z3950;
67     ent.oclass = CLASS_USERINFO;
68     ent.value = (oid_value) VAL_PROXY;
69     if (oid_ent_to_oid (&ent, oid) &&
70         (oi = update_otherInformation(otherInfo, 0, oid, 1)) &&
71         oi->which == Z_OtherInfo_characterInfo)
72         return oi->information.characterInfo;
73     return 0;
74 }
75
76 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
77 {
78     if (apdu->which == Z_APDU_initRequest)
79     {
80         assert (m_client == 0);
81         logf (LOG_LOG, "got InitRequest");
82         m_client = new Yaz_ProxyClient(m_PDU_Observable->clone());
83         m_client->m_server = this;
84
85         char *proxy_host = get_cookie(&apdu->u.initRequest->otherInfo);
86         if (proxy_host)
87             m_client->client(proxy_host);
88         else
89             m_client->client("localhost:8888");
90     }
91     assert (m_client);
92     logf (LOG_LOG, "sending PDU");
93     m_client->send_Z_PDU(apdu);
94 }
95
96 void Yaz_Proxy::failNotify()
97 {
98     logf (LOG_LOG, "failNotity server");
99     delete m_client;
100     delete this;
101 }
102
103 void Yaz_ProxyClient::failNotify()
104 {
105     logf (LOG_LOG, "failNotity client");
106     delete m_server;
107     delete this;
108 }
109
110 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
111                                           *the_PDU_Observable)
112 {
113     return new Yaz_ProxyClient(the_PDU_Observable);
114 }
115
116 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
117     Yaz_Z_Assoc (the_PDU_Observable)
118 {
119     m_cookie = 0;
120     m_next = 0;
121 }
122
123 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
124 {
125     m_server->send_Z_PDU(apdu);
126 }