00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "options.h"
00036 #include "node.h"
00037 #include "settings.h"
00038 #include <biosphere.h>
00039 #include <list.h>
00040
00041 #include <apr_pools.h>
00042 #include <apr_getopt.h>
00043 #include <apr_strings.h>
00044 #include <stdlib.h>
00045 #include <stdio.h>
00046 #include <string.h>
00047
00048
00049
00050 unsigned global_listen_port;
00051 unsigned global_max_connections;
00052 char *global_conffile = NULL;
00053 bs_list *global_module_names_list = NULL;
00054 char *global_modules_dir = NULL;
00055 bs_list *global_node_address_list = NULL;
00056
00057
00058 static void add_node_to_global_list(const char *optarg, apr_pool_t *mp)
00059 {
00060 char *addr;
00061 bs_uint16 port = 2915;
00062 unsigned i;
00063 bs_status rv;
00064
00065
00066 addr = apr_pstrdup(mp, optarg);
00067 for (i = 0; i < strlen(optarg); i++) {
00068 if (optarg[i] == ':') {
00069 addr = apr_pstrndup(mp, optarg, i);
00070 if (optarg[i + 1] != '\0') {
00071 const char *tmp = optarg + i + 1;
00072 port = (bs_uint16) atoi(tmp);
00073 }
00074 break;
00075 }
00076 }
00077
00078
00079 rv = add_node(addr, port);
00080 if (rv != BS_OK)
00081 fprintf(stderr, "Unable to add node: %s:%d\n", addr, port);
00082 }
00083
00084
00092 static void init_defaults(apr_pool_t *mp)
00093 {
00094
00095 global_listen_port = BIOSPHERED_PORT;
00096 global_max_connections = BIOSPHERED_MAX_CONNECTIONS;
00097
00098
00099 global_conffile = apr_pstrdup(mp, BIOSPHERED_CONFFILE);
00100 global_modules_dir = apr_pstrdup(mp, BIOSPHERED_MODULES_DIR);
00101
00102
00103 global_module_names_list = new_list(mp);
00104 global_node_address_list = new_list(mp);
00105 }
00106
00107
00108 bs_status init_options(int argc, const char *argv[], apr_pool_t *mp)
00109 {
00110 int optch;
00111 const char *optarg;
00112 apr_status_t rv;
00113 apr_getopt_t *opt;
00114
00115 static const apr_getopt_option_t opt_option[] = {
00116
00117 {"help", '?', FALSE, "print help information"},
00118 {"version", 'V', FALSE, "print version information"},
00119 {"max-clients", 'i', TRUE, "maximum number of clients"},
00120 {"with-config", 'c', TRUE, "path of config file to use"},
00121 {"with-module", 'm', TRUE, "path to module to load"},
00122 {"node", 'n', TRUE, "address of another node (address[:port]?)"},
00123 {"port", 'p', TRUE, "the port the daemon listens on"},
00124 {NULL, 0, 0, NULL}
00125 };
00126
00127 init_defaults(mp);
00128 rv = apr_getopt_init(&opt, mp, argc, argv);
00129 if (rv != APR_SUCCESS) return BS_ERROR;
00130
00131
00132 while ((rv = apr_getopt_long(opt, opt_option, &optch, &optarg)) == APR_SUCCESS) {
00133 char *tmp;
00134
00135 switch (optch) {
00136 case '?':
00137 printf("Here comes help information\n");
00138 return 1;
00139 break;
00140 case 'V':
00141 printf(BIOSPHERE_VERSION_STRING "\n");
00142 return 1;
00143 break;
00144 case 'i':
00145 global_max_connections = atoi(optarg);
00146 printf("Incoming connection limit: %d\n", global_max_connections);
00147 break;
00148 case 'c':
00149 global_conffile = apr_pstrdup(mp, optarg);
00150 printf("Reading config options from: %s\n", global_conffile);
00151 break;
00152 case 'm':
00153 tmp = apr_pstrdup(mp, optarg);
00154 list_append(global_module_names_list, tmp);
00155 printf("Using module: %s\n", tmp);
00156 break;
00157 case 'n':
00158 add_node_to_global_list(optarg, mp);
00159 printf("Adding node: %s\n", optarg);
00160 break;
00161 case 'p':
00162 global_listen_port = atoi(optarg);
00163 printf("Incoming connection port: %d\n", global_listen_port);
00164 break;
00165 default:
00166 fprintf(stderr, "Bad command-line option provided: -%c %s\n", optch, optarg);
00167 return BS_ERROR;
00168 }
00169 }
00170 if (rv != APR_EOF) {
00171 fprintf(stderr, "Bad command-line option provided\n");
00172 return BS_ERROR;
00173 }
00174
00175 return BS_OK;
00176 }