first commit
This commit is contained in:
116
CS4210/cs4210/proj1/src/client/config.c
Normal file
116
CS4210/cs4210/proj1/src/client/config.c
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "common/util.h"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#define CONFIG_PNAME_JOBCOUNT "job-count"
|
||||
#define CONFIG_PNAME_WORKERCOUNT "thread-count"
|
||||
#define CONFIG_PNAME_URLFILE "url-file"
|
||||
#define CONFIG_PNAME_CONFIGFILE "config-file"
|
||||
|
||||
#define CONFIG_DVALUE_JOBCOUNT 1
|
||||
#define CONFIG_DVALUE_WORKERCOUNT 1
|
||||
#define CONFIG_DVALUE_URLFILE stdin
|
||||
|
||||
static int m_job_count = 0;
|
||||
static int m_worker_count = 0;
|
||||
static FILE* m_url_file = NULL;
|
||||
static int m_read_config_file = 0;
|
||||
|
||||
int record_parameter(const char* name, const char* value)
|
||||
{
|
||||
if (!strcmp(name, CONFIG_PNAME_JOBCOUNT))
|
||||
{
|
||||
/* Got job count: */
|
||||
m_job_count = atoi(value);
|
||||
printf("[CONF] Overriding default %s with value %d\n",
|
||||
CONFIG_PNAME_JOBCOUNT, m_job_count);
|
||||
return 0;
|
||||
}
|
||||
else if(!strcmp(name, CONFIG_PNAME_WORKERCOUNT))
|
||||
{
|
||||
/* Got worker count: */
|
||||
m_worker_count = atoi(value);
|
||||
printf("[CONF] Overriding default %s with value %d\n",
|
||||
CONFIG_PNAME_WORKERCOUNT, m_worker_count);
|
||||
return 0;
|
||||
}
|
||||
else if(!strcmp(name, CONFIG_PNAME_URLFILE))
|
||||
{
|
||||
/* Got url file: */
|
||||
m_url_file = fopen(value, "r");
|
||||
if (m_url_file)
|
||||
{
|
||||
printf("[CONF] Overriding default %s with value \"%s\"\n",
|
||||
CONFIG_PNAME_URLFILE, value);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "[CONF] Could not open URL file \"%s\"\n", value);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else if(!strcmp(name, CONFIG_PNAME_CONFIGFILE))
|
||||
{
|
||||
/* Got config file name: */
|
||||
if (!m_read_config_file)
|
||||
{
|
||||
m_read_config_file = 1;
|
||||
return parse_file_parameters(fopen(value, "r"), record_parameter);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Unknown parameter: */
|
||||
fprintf(stderr, "[CONF] Unknown parameter \"%s\"\n", name);
|
||||
config_print_parameters(stderr);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void config_print_parameters(FILE* out)
|
||||
{
|
||||
fprintf(out, "Supported parameters:\n");
|
||||
fprintf(out, "\t%s - the number of requests to make to the server.\n"
|
||||
"\t\tDefault value is %d.\n",
|
||||
CONFIG_PNAME_JOBCOUNT, CONFIG_DVALUE_JOBCOUNT);
|
||||
fprintf(out, "\t%s - the number of threads to start for making requests.\n"
|
||||
"\t\tDefault value is %d.\n",
|
||||
CONFIG_PNAME_WORKERCOUNT, CONFIG_DVALUE_WORKERCOUNT);
|
||||
fprintf(out, "\t%s - the name of the file from which to read the list of\n"
|
||||
"\t\tURLs to request.\n"
|
||||
"\t\tDefault value is stdin.\n",
|
||||
CONFIG_PNAME_URLFILE);
|
||||
fprintf(out, "\t%s - the name of a configuration file.\n",
|
||||
CONFIG_PNAME_CONFIGFILE);
|
||||
fprintf(out, "\t\n");
|
||||
}
|
||||
|
||||
int config_init(int argc, const char** argv)
|
||||
{
|
||||
/* Initialize default values: */
|
||||
m_job_count = CONFIG_DVALUE_JOBCOUNT;
|
||||
m_worker_count = CONFIG_DVALUE_WORKERCOUNT;
|
||||
m_url_file = CONFIG_DVALUE_URLFILE;
|
||||
|
||||
return parse_command_parameters(argc, argv, record_parameter);
|
||||
}
|
||||
|
||||
void config_free()
|
||||
{
|
||||
if (m_url_file)
|
||||
{
|
||||
fclose(m_url_file);
|
||||
}
|
||||
}
|
||||
|
||||
int config_get_jobcount() { return m_job_count; }
|
||||
int config_get_workercount() { return m_worker_count; }
|
||||
FILE* config_get_urlfile() { return m_url_file; }
|
||||
Reference in New Issue
Block a user