m2.mbcp/src/config.cc
2006-03-18 17:30:36 +00:00

166 lines
3 KiB
C++

#include <sstream>
#include <iostream>
#include "config.h"
#include "group.h"
#include "message.h"
using namespace std;
Config::Config(int argc, char **argv) {
_port = -1;
_index = -1;
_mode = Protocol::TYPE_UNKNOWN;
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'},
{"index", required_argument, 0, 'i'},
{0, 0, 0, 0}
};
int option_index = 0;
int c = getopt_long(argc, argv, "TACg:p:i:",
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;
}
case 'i':
{
stringstream s;
printf("Index -> %s\n",optarg);
s << string(optarg);
s >> _index;
}
break;
case 'p':
{
stringstream s;
printf("Port -> %s\n",optarg);
s << string(optarg);
s >> _port;
}
break;
case 'g':
{
HostId g_host;
string optstr(optarg);
stringstream s_host, s_port;
int idx = optstr.find(":");
if (idx > 0){
cout << "Group -> "<< optstr <<" (: at "<< idx <<")\n";
// on oblige la forme XXXXXX:YY
g_host.host = optstr.substr(0,idx);
//s_host >> (g_host.host);
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++;
if (_index >= 0){
score++;
}
valid++;
if (_index < _group_hosts.size()){
score++;
}
valid++;
return (valid == score);
}
std::list<HostId> Config::getGroupHosts(){
return _group_hosts;
}
Protocol::Type Config::getMode(){
return _mode;
}
short Config::getIndex(){
return _index;
}
int Config::getPort(){
return _port;
}
void Config::usage() {
printf("Usage: dabcast <mode> [options]\n");
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");
printf("-i, -index <int> The index of current host in the group\n");
}