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