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