Yaz_PDU_Assoc better encapsulated. Memory leak fix in
[yazpp-moved-to-github.git] / src / yaz-server.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-server.cpp,v $
7  * Revision 1.2  1999-01-28 13:08:47  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 #include <yaz-pdu-assoc.h>
19 #include <yaz-socket-manager.h>
20
21 class MyServer : public Yaz_IR_Assoc {
22 public:
23     MyServer(IYaz_PDU_Observable *the_PDU_Observable);
24     void recv_Z_PDU(Z_APDU *apdu);
25     IYaz_PDU_Observer* clone(IYaz_PDU_Observable *the_PDU_Observable);
26     void failNotify();
27 private:
28     int m_no;
29 };
30
31 static int stop = 0;
32
33 void MyServer::recv_Z_PDU(Z_APDU *apdu)
34 {
35     logf (LOG_LOG, "recv_Z_PDU");
36     switch (apdu->which)
37     {
38     case Z_APDU_initRequest:
39         logf (LOG_LOG, "got InitRequest");
40         apdu = create_Z_PDU(Z_APDU_initResponse);
41         send_Z_PDU(apdu);
42         break;
43     case Z_APDU_searchRequest:
44         logf (LOG_LOG, "got searchRequest");
45         apdu = create_Z_PDU(Z_APDU_searchResponse);
46         send_Z_PDU(apdu);
47         break;
48     case Z_APDU_presentRequest:
49         logf (LOG_LOG, "got presentRequest");
50         apdu = create_Z_PDU(Z_APDU_presentResponse);
51         send_Z_PDU(apdu);
52         stop = 1;
53         break;
54     }
55 }
56
57 IYaz_PDU_Observer *MyServer::clone(IYaz_PDU_Observable *the_PDU_Observable)
58 {
59     logf (LOG_LOG, "clone %d", m_no);
60     m_no++;
61     return new MyServer(the_PDU_Observable);
62 }
63
64 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
65     Yaz_IR_Assoc (the_PDU_Observable)
66 {
67     m_no = 0;
68 }
69
70 void MyServer::failNotify()
71 {
72     delete this;
73 }
74
75 int main(int argc, char **argv)
76 {
77     Yaz_SocketManager mySocketManager;
78
79     MyServer z(new Yaz_PDU_Assoc(&mySocketManager, 0));
80     
81     if (argc <= 1)
82         z.server("@:9999");
83     else
84     {
85         for (int i = 1; i < argc; i++)
86             z.server(argv[i]);
87     }
88     while (!stop && mySocketManager.processEvent() > 0)
89         ;
90 }