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