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