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