Added object Yaz_Z_Assoc. Much more functional client.
[yazpp-moved-to-github.git] / src / yaz-z-assoc.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-z-assoc.cpp,v $
7  * Revision 1.1  1999-04-09 11:46:57  adam
8  * Added object Yaz_Z_Assoc. Much more functional client.
9  *
10  */
11
12 #include <log.h>
13 #include <yaz-z-assoc.h>
14
15 int Yaz_Z_Assoc::yaz_init_func()
16 {
17     logf (LOG_LOG, "nmem_init");
18     nmem_init();
19     logf (LOG_LOG, "done");
20     return 1;
21 }
22
23 int Yaz_Z_Assoc::yaz_init_flag = Yaz_Z_Assoc::yaz_init_func();
24
25 Yaz_Z_Assoc::Yaz_Z_Assoc(IYaz_PDU_Observable *the_PDU_Observable)
26 {
27     m_PDU_Observable = the_PDU_Observable;
28     m_odr_in = odr_createmem (ODR_DECODE);
29     m_odr_out = odr_createmem (ODR_ENCODE);
30     m_odr_print = odr_createmem (ODR_PRINT);
31 }
32
33 Yaz_Z_Assoc::~Yaz_Z_Assoc()
34 {
35     m_PDU_Observable->destroy();
36     delete m_PDU_Observable;
37     odr_destroy (m_odr_print);
38     odr_destroy (m_odr_out);
39     odr_destroy (m_odr_in);
40 }
41
42 void Yaz_Z_Assoc::recv_PDU(const char *buf, int len)
43 {
44     logf (LOG_LOG, "recv_PDU len=%d", len);
45     Z_APDU *apdu = decode_Z_PDU (buf, len);
46     if (apdu)
47         recv_Z_PDU (apdu);
48 }
49
50 Z_APDU *Yaz_Z_Assoc::create_Z_PDU(int type)
51 {
52     return zget_APDU(m_odr_out, type);
53 }
54
55 int Yaz_Z_Assoc::send_Z_PDU(Z_APDU *apdu)
56 {
57     char *buf;
58     int len;
59     logf (LOG_LOG, "Yaz_Z_Assoc:send_Z_PDU");
60     if (encode_Z_PDU(apdu, &buf, &len) > 0)
61         return m_PDU_Observable->send_PDU(buf, len);
62     return -1;
63 }
64
65 Z_APDU *Yaz_Z_Assoc::decode_Z_PDU(const char *buf, int len)
66 {
67     Z_APDU *apdu;
68
69     odr_reset (m_odr_in);
70     odr_setbuf (m_odr_in, (char*) buf, len, 0);
71
72     if (!z_APDU(m_odr_in, &apdu, 0))
73     {
74         logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
75              odr_errmsg(odr_geterror(m_odr_in)),
76              odr_offset(m_odr_in));
77         logf(LOG_LOG, "PDU dump:");
78         odr_dumpBER(log_file(), buf, len);
79         return 0;
80     }
81     else
82     {
83         logf (LOG_LOG, "decoded ok");
84         return apdu;
85     }
86 }
87
88 int Yaz_Z_Assoc::encode_Z_PDU(Z_APDU *apdu, char **buf, int *len)
89 {
90     if (!z_APDU(m_odr_out, &apdu, 0))
91     {
92         logf (LOG_LOG, "yaz_Z_Assoc::encode_Z_PDU failed");
93         return -1;
94     }
95     *buf = odr_getbuf (m_odr_out, len, 0);
96     odr_reset (m_odr_out);
97     return *len;
98 }
99
100 void Yaz_Z_Assoc::connectNotify()
101 {
102     logf (LOG_LOG, "connectNotify");
103 }
104
105 void Yaz_Z_Assoc::failNotify()
106 {
107     logf (LOG_LOG, "failNotify");
108 }
109
110 void Yaz_Z_Assoc::timeoutNotify()
111 {
112     logf (LOG_LOG, "timeoutNotify");
113 }
114
115 void Yaz_Z_Assoc::client(const char *addr)
116 {
117     m_PDU_Observable->connect (this, addr);
118 }
119
120 void Yaz_Z_Assoc::close()
121 {
122     m_PDU_Observable->close ();
123 }
124
125 void Yaz_Z_Assoc::server(const char *addr)
126 {
127     m_PDU_Observable->listen (this, addr);
128 }
129
130 ODR Yaz_Z_Assoc::odr_encode()
131 {
132     return m_odr_out;
133 }