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