Log request number. More configurable keepalive with pdu/bw limits.
[yazpp-moved-to-github.git] / src / yaz-proxy-main.cpp
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-proxy-main.cpp,v 1.20 2003-10-09 12:11:10 adam Exp $
6  */
7
8 #include <signal.h>
9 #include <yaz/log.h>
10 #include <yaz/options.h>
11
12 #include <yaz++/socket-manager.h>
13 #include <yaz++/pdu-assoc.h>
14 #include <yaz++/proxy.h>
15
16 void usage(char *prog)
17 {
18     fprintf (stderr, "%s: [-c config] [-a log] [-m num] [-v level] [-t target] [-i sec] "
19              "[-u auth] [-o optlevel] @:port\n", prog);
20     exit (1);
21 }
22
23 int args(Yaz_Proxy *proxy, int argc, char **argv)
24 {
25     char *addr = 0;
26     char *arg;
27     char *prog = argv[0];
28     int ret;
29
30     while ((ret = options("o:a:t:v:c:u:i:m:l:T:", argv, argc, &arg)) != -2)
31     {
32         int err;
33         switch (ret)
34         {
35         case 0:
36             if (addr)
37             {
38                 usage(prog);
39                 return 1;
40             }
41             addr = arg;
42             break;
43         case 'c':
44             err = proxy->set_config(arg);
45             if (err == -2)
46             {
47                 fprintf(stderr, "Config file support not enabled (proxy not compiled with libxml2 support)\n");
48                 exit(1);
49             }
50             else if (err == -1)
51             {
52                 fprintf(stderr, "Bad or missing file %s\n", arg);
53                 exit(1);
54             }
55             break;
56         case 'a':
57             proxy->set_APDU_log(arg);
58             break;
59         case 't':
60             proxy->set_default_target(arg);
61             break;
62         case 'u':
63             proxy->set_proxy_authentication(arg);
64             break;
65         case 'o':
66             proxy->option("optimize", arg);
67             break;
68         case 'v':
69             yaz_log_init_level (yaz_log_mask_str(arg));
70             break;
71         case 'l':
72             yaz_log_init_file (arg);
73             break;
74         case 'm':
75             proxy->set_max_clients(atoi(arg));
76             break;
77         case 'i':
78             proxy->set_client_idletime(atoi(arg));
79             break;
80         case 'T':
81             proxy->set_target_idletime(atoi(arg));
82             break;
83         default:
84             usage(prog);
85             return 1;
86         }
87     }
88     if (addr)
89     {
90         proxy->server(addr);
91     }
92     else
93     {
94         usage(prog);
95         return 1;
96     }
97     return 0;
98 }
99
100 static Yaz_Proxy *static_yaz_proxy = 0;
101 static void sighup_handler(int num)
102 {
103     if (static_yaz_proxy)
104         static_yaz_proxy->reconfig();
105 }
106
107 int main(int argc, char **argv)
108 {
109     Yaz_SocketManager mySocketManager;
110     Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
111
112     static_yaz_proxy = &proxy;
113
114     signal(SIGHUP, sighup_handler);
115
116     args(&proxy, argc, argv);
117     while (mySocketManager.processEvent() > 0)
118         ;
119     exit (0);
120     return 0;
121 }