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