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