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