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