m2.mbcp/src/config.cc

166 lines
3 KiB
C++
Raw Permalink Normal View History

2006-02-05 17:43:00 +00:00
#include <sstream>
#include <iostream>
#include "config.h"
#include "group.h"
#include "message.h"
using namespace std;
Config::Config(int argc, char **argv) {
_port = -1;
2006-02-05 18:24:11 +00:00
_index = -1;
2006-02-05 17:43:00 +00:00
_mode = Protocol::TYPE_UNKNOWN;
2006-02-05 18:24:11 +00:00
2006-02-05 17:43:00 +00:00
int groupPort;
while (1) {
static struct option long_options[] = {
//
//{"compress1", no_argument, 0, 'c'},
{"test", no_argument, 0, 'T'},
{"abcast", no_argument, 0, 'A'},
{"cbcast", no_argument, 0, 'C'},
{"group", required_argument, 0, 'g'},
{"port", required_argument, 0, 'p'},
2006-02-05 18:24:11 +00:00
{"index", required_argument, 0, 'i'},
2006-02-05 17:43:00 +00:00
{0, 0, 0, 0}
};
int option_index = 0;
2006-02-05 18:24:11 +00:00
int c = getopt_long(argc, argv, "TACg:p:i:",
2006-02-05 17:43:00 +00:00
long_options, &option_index);
/* detect the end of options */
if (c == -1) {
break;
}
switch (c) {
case 0:
printf("case NULL\n");
break;
case 'A':
{
_mode = Protocol::TYPE_ABCAST;
break;
}
case 'C':
{
_mode = Protocol::TYPE_CBCAST;
break;
}
case 'T':
{
_mode = Protocol::TYPE_TEST;
break;
}
2006-02-05 18:24:11 +00:00
case 'i':
{
stringstream s;
printf("Index -> %s\n",optarg);
s << string(optarg);
s >> _index;
}
break;
2006-02-05 17:43:00 +00:00
case 'p':
{
stringstream s;
printf("Port -> %s\n",optarg);
s << string(optarg);
s >> _port;
}
break;
case 'g':
{
HostId g_host;
string optstr(optarg);
2006-03-04 14:24:03 +00:00
stringstream s_host, s_port;
2006-02-05 17:43:00 +00:00
int idx = optstr.find(":");
if (idx > 0){
cout << "Group -> "<< optstr <<" (: at "<< idx <<")\n";
// on oblige la forme XXXXXX:YY
2006-03-01 21:40:44 +00:00
g_host.host = optstr.substr(0,idx);
//s_host >> (g_host.host);
2006-02-05 17:43:00 +00:00
s_port << optstr.substr(idx+1, optstr.size()-idx-1);
s_port >> (g_host.port);
} else {
cerr <<"Invalid host : '"<< optstr <<"'\n";
}
_group_hosts.push_back(g_host);
}
break;
case '?':
printf("unknow\n");
break;
default:
printf("default\n");
return;
}
}
}
bool Config::isValid() {
int score = 0;
int valid = 0;
if (_group_hosts.size() > 0) {
score++;
}
valid++;
if (_port > 0) {
score++;
}
valid++;
2006-03-06 13:19:22 +00:00
if (_index >= 0){
score++;
}
valid++;
if (_index < _group_hosts.size()){
2006-02-05 18:24:11 +00:00
score++;
}
valid++;
2006-02-05 17:43:00 +00:00
return (valid == score);
}
std::list<HostId> Config::getGroupHosts(){
return _group_hosts;
}
Protocol::Type Config::getMode(){
return _mode;
}
2006-02-05 18:24:11 +00:00
short Config::getIndex(){
return _index;
}
2006-02-05 17:43:00 +00:00
int Config::getPort(){
return _port;
}
void Config::usage() {
2006-03-18 17:24:11 +00:00
printf("Usage: dabcast <mode> [options]\n");
2006-02-05 17:43:00 +00:00
printf("\n");
printf("Modes (mutualy exclusive):\n");
printf("-T, -test Test mode (simple broadcast)\n");
printf("-A, -abcast ABcast mode\n");
printf("-C, -cbcast CBcast mode\n");
printf("Mandatory options:\n");
printf("-g, -group <host:port> Add an host to the group\n");
printf("-p, -port <port> Use this port on localhost\n");
2006-03-18 17:30:36 +00:00
printf("-i, -index <int> The index of current host in the group\n");
2006-02-05 17:43:00 +00:00
}