More work on high-level server.
[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.9  2000-09-12 12:09:53  adam
7  * More work on high-level server.
8  *
9  * Revision 1.8  2000/09/08 10:23:42  adam
10  * Added skeleton of yaz-z-server.
11  *
12  * Revision 1.7  1999/12/06 13:52:45  adam
13  * Modified for new location of YAZ header files. Experimental threaded
14  * operation.
15  *
16  * Revision 1.6  1999/04/21 12:09:01  adam
17  * Many improvements. Modified to proxy server to work with "sessions"
18  * based on cookies.
19  *
20  * Revision 1.5  1999/04/09 11:46:57  adam
21  * Added object Yaz_Z_Assoc. Much more functional client.
22  *
23  * Revision 1.4  1999/03/23 14:17:57  adam
24  * More work on timeout handling. Work on yaz-client.
25  *
26  * Revision 1.3  1999/02/02 14:01:22  adam
27  * First WIN32 port of YAZ++.
28  *
29  * Revision 1.2  1999/01/28 13:08:47  adam
30  * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
31  * yaz-socket-manager.cc.
32  *
33  * Revision 1.1.1.1  1999/01/28 09:41:07  adam
34  * First implementation of YAZ++.
35  *
36  */
37
38 #include <yaz/log.h>
39 #include <yaz-z-server.h>
40 #include <yaz-pdu-assoc.h>
41 #include <yaz-socket-manager.h>
42
43 class MyServer : public Yaz_Z_Server {
44 public:
45     MyServer(IYaz_PDU_Observable *the_PDU_Observable);
46     void recv_Z_init (Z_InitRequest *initRequest,
47                       Z_InitResponse *initResponse);
48     void recv_Z_search (Z_SearchRequest *searchRequest,
49                         Z_SearchResponse *searchResponse);
50     void recv_Z_present (Z_PresentRequest *presentRequest,
51                          Z_PresentResponse *presentResponse);
52
53     void recv_Z_record (const char *resultSetName,
54                         int position,
55                         int *format,
56                         Z_RecordComposition *comp,
57                               Z_NamePlusRecord *namePlusRecord,
58                         Z_DefaultDiagFormat *diagnostics);
59     IYaz_PDU_Observer* clone(IYaz_PDU_Observable *the_PDU_Observable);
60     void failNotify();
61     void timeoutNotify();
62     void connectNotify();
63 private:
64     int m_no;
65 };
66
67 static int stop = 0;
68
69 void MyServer::recv_Z_init (Z_InitRequest *initRequest,
70                             Z_InitResponse *initResponse)
71 {
72     logf (LOG_LOG, "MyServer::recv_Z_init");
73 }
74
75 void MyServer::recv_Z_search (Z_SearchRequest *searchRequest,
76                               Z_SearchResponse *searchResponse)
77 {
78     logf (LOG_LOG, "MyServer::recv_Z_search");
79     delete this;
80     stop = 1;
81 }
82
83 void MyServer::recv_Z_present (Z_PresentRequest *presentRequest,
84                                Z_PresentResponse *presentResponse)
85 {
86     logf (LOG_LOG, "MyServer::recv_Z_present");
87 }
88
89 void MyServer::recv_Z_record (const char *resultSetName,
90                               int position,
91                               int *format,
92                               Z_RecordComposition *comp,
93                               Z_NamePlusRecord *namePlusRecord,
94                               Z_DefaultDiagFormat *diagnostics)
95 {
96
97 }
98
99 IYaz_PDU_Observer *MyServer::clone(IYaz_PDU_Observable *the_PDU_Observable)
100 {
101     MyServer *new_server;
102     logf (LOG_LOG, "child no %d", m_no);
103     m_no++;
104     new_server = new MyServer(the_PDU_Observable);
105     new_server->timeout(60);
106     return new_server;
107 }
108
109 MyServer::MyServer(IYaz_PDU_Observable *the_PDU_Observable) :
110     Yaz_Z_Server (the_PDU_Observable)
111 {
112     m_no = 0;
113 }
114
115 void MyServer::timeoutNotify()
116 {
117     logf (LOG_LOG, "connection timed out");
118     delete this;
119 }
120
121 void MyServer::failNotify()
122 {
123     logf (LOG_LOG, "connection closed by client");
124     delete this;
125 }
126
127 void MyServer::connectNotify()
128 {
129 }
130
131 int main(int argc, char **argv)
132 {
133     while (1)
134     {
135         stop = 0;
136         Yaz_SocketManager mySocketManager;
137         Yaz_PDU_Assoc *my_PDU_Assoc = new Yaz_PDU_Assoc(&mySocketManager);
138         
139         MyServer *z = new MyServer(my_PDU_Assoc);
140         
141         if (argc <= 1)
142             z->server("@:9999");
143         else
144         {
145             for (int i = 1; i < argc; i++)
146                 z->server(argv[i]);
147         }
148         while (!stop && mySocketManager.processEvent() > 0)
149             ;
150         logf (LOG_LOG, "bailing out");
151         delete z;
152     }
153     return 0;
154 }