Exemplo n.º 1
0
Arquivo: main.cpp Projeto: alimy/tfs
int parse_param(const VSTRING& param, ComType com_type, ParamInfo& ret_param)
{
  VSTRING::const_iterator iter = param.begin();
  for (; iter != param.end(); iter++)
  {
    int32_t cmd = -1;
    int ret = TFS_SUCCESS;
    bool has_value = false;
    if (TFS_SUCCESS == get_cmd(const_cast<char*>((*iter).c_str()), cmd, has_value))
    {
      if (has_value)
      {
        if ((++iter) == param.end())
        {
          TBSYS_LOG(ERROR, "please input param value...");
          return TFS_ERROR;
        }
      }
      bool is_common = true;
      switch (cmd)
      {
        case CMD_NUM:
          ret = get_value((*iter).c_str(), ret_param.num_);
          if(0 == ret_param.num_ || ret_param.num_ > (uint64_t)MAX_READ_NUM)
          {
            TBSYS_LOG(ERROR, "please input num in [1, %d]", MAX_READ_NUM);
            return TFS_ERROR;
          }
          break;
        case CMD_COUNT:
          ret = get_value((*iter).c_str(), ret_param.count_);
          break;
        case CMD_INTERVAL:
          ret = get_value((*iter).c_str(), ret_param.interval_);
          break;
        case CMD_REDIRECT:
          ret = get_value((*iter).c_str(), ret_param.filename_);
          break;
        default:
          is_common = false;
          break;
      }
      if (is_common) continue;
      if (com_type & SERVER_TYPE)
      {
        switch (cmd)
        {
          case CMD_SERVER_ID:
            if (get_value((*iter).c_str(), ret_param.server_ip_port_) == TFS_SUCCESS)
            {
              if (ret_param.server_ip_port_.find_first_of(":") == string::npos)
              {
                ret = TFS_ERROR;
              }
            }
            break;
          case CMD_BLOCK_LIST:
            ret_param.type_ = SERVER_TYPE_BLOCK_LIST;
            break;
          case CMD_BLOCK_WRITABLE:
            ret_param.type_ = SERVER_TYPE_BLOCK_WRITABLE;
            break;
          case CMD_BLOCK_MASTER:
            ret_param.type_ = SERVER_TYPE_BLOCK_MASTER;
            break;
          case CMD_NEED_FAMILY:
            ret_param.need_family_ = true;
            break;
          default:
            ret = CMD_UNKNOWN;
            break;
        }
      }
      if (com_type & BLOCK_TYPE)
      {
        uint64_t tmp = 0;
        switch (cmd)
        {
          case CMD_BLOCK_ID:
            if ((ret = get_value((*iter).c_str(), tmp)) == TFS_SUCCESS)
            {
              ret_param.block_id_ = tmp;
            }
            break;
          case CMD_SERVER_LIST:
            g_need_cmp ? (ret_param.type_ = BLOCK_CMP_SERVER) : (ret_param.type_ = BLOCK_TYPE_SERVER_LIST);
            break;
          case CMD_BLOCK_STATUS:
            ret_param.type_ = BLOCK_TYPE_BLOCK_STATUS;
            break;
          case CMD_BLOCK_FULL:
            ret_param.type_ |= BLOCK_TYPE_BLOCK_FULL;// additional condition
            break;
          case CMD_ALL:
            ret_param.type_ = BLOCK_CMP_ALL_INFO;
            break;
          case CMD_PART:
            ret_param.type_ = BLOCK_CMP_PART_INFO;
            break;
          default:
            ret = CMD_UNKNOWN;
            break;
        }
      }
      if (com_type & MACHINE_TYPE)
      {
        switch (cmd)
        {
          case CMD_ALL:
            ret_param.type_ = MACHINE_TYPE_ALL;
            break;
          case CMD_PART:
            ret_param.type_ = MACHINE_TYPE_PART;
            break;
          case CMD_FOR_MONITOR:
            ret_param.type_ = MACHINE_TYPE_FOR_MONITOR;
            break;
          case CMD_NEED_FAMILY:
            ret_param.need_family_ = true;
            break;
          default:
            ret = CMD_UNKNOWN;
            break;
        }
      }
      if (com_type & BLOCK_DISTRIBUTION_TYPE)
      {//这里解析子命令,并记录到ret_param里面
        uint64_t tmp = 0;
        switch (cmd)
        {
          case CMD_BLOCK_ID:
            if ((ret = get_value((*iter).c_str(), tmp)) == TFS_SUCCESS)
            {
              ret_param.block_id_ = tmp;
            }
            break;
          case CMD_IP_ID:
            ret_param.type_ = BLOCK_IP_DISTRIBUTION_TYPE;
            break;
          case CMD_IP_MASK_ID:
          if(!get_value((*iter).c_str(), ret_param.rack_ip_mask_))//mask
            {
              ret = TFS_ERROR;
            }
            if(0 == Func::get_addr(ret_param.rack_ip_mask_.c_str()))
            {
              ret = TFS_ERROR;
            }
            ret_param.type_ = BLOCK_RACK_DISTRIBUTION_TYPE;
            break;
          default:
            ret = CMD_UNKNOWN;
            break;
        }
      }
      if (com_type & RACK_BLOCK_TYPE)
      {
        switch (cmd)
        {
          case CMD_IP_GROUP_ID:
            if (get_value((*iter).c_str(), ret_param.server_ip_port_) )
            {
              string::size_type index = ret_param.server_ip_port_.find_first_of(":");//允许用户输入含端口的ip_group
              if(string::npos != index)
              {
                ret_param.server_ip_port_ = ret_param.server_ip_port_.substr(0, index);
              }
              ret_param.type_ = RACK_BLOCK_TYPE_BLOCK_LIST;//默认为RACK_BLOCK_TYPE_RACK_LIST
            }
            if(0 == Func::get_addr(ret_param.server_ip_port_.c_str()))
            {
              ret = TFS_ERROR;
            }
            break;
          case CMD_IP_MASK_ID:
            if(!get_value((*iter).c_str(), ret_param.rack_ip_mask_))//mask
            {
              ret = TFS_ERROR;
            }
            if(0 == Func::get_addr(ret_param.rack_ip_mask_.c_str()))
            {
              ret = TFS_ERROR;
            }
            break;
          default:
            ret = CMD_UNKNOWN;
            break;
        }
      }
      if (com_type & FAMILY_TYPE)
      {
        uint64_t tmp = 0;
        switch (cmd)
        {
          case CMD_BLOCK_ID://CMD_FAMILY_ID:
            if ((ret = get_value((*iter).c_str(), tmp)) == TFS_SUCCESS)
            {
              ret_param.family_id_ = tmp;
            }
            break;
          case CMD_SERVER_LIST:
            ret_param.type_ = BLOCK_TYPE_SERVER_LIST;
            break;
          default:
            ret = CMD_UNKNOWN;
            break;
        }
      }
    }
    else
    {
      TBSYS_LOG(ERROR, "unknown param......");
      ret = CMD_UNKNOWN;
    }
    if (ret != TFS_SUCCESS)
    {
      TBSYS_LOG(ERROR, "unvalid value...");
      print_help();
      return ret;
    }
  }
  return TFS_SUCCESS;
}
Exemplo n.º 2
0
int parse_param(const VSTRING& param, ComType com_type, ParamInfo& ret_param)
{
  VSTRING::const_iterator iter = param.begin();
  for (; iter != param.end(); iter++)
  {
    int32_t cmd = -1;
    int ret = TFS_SUCCESS;
    bool has_value = false;
    if (TFS_SUCCESS == get_cmd(const_cast<char*>((*iter).c_str()), cmd, has_value))
    {
      if (has_value)
      {
        if ((++iter) == param.end())
        {
          TBSYS_LOG(ERROR, "please input param value...");
          return TFS_ERROR;
        }
      }
      bool is_common = true;
      switch (cmd)
      {
        case CMD_NUM:
          ret = get_value((*iter).c_str(), ret_param.num_);
          break;
        case CMD_COUNT:
          ret = get_value((*iter).c_str(), ret_param.count_);
          break;
        case CMD_INTERVAL:
          ret = get_value((*iter).c_str(), ret_param.interval_);
          break;
        case CMD_REDIRECT:
          ret = get_value((*iter).c_str(), ret_param.filename_);
          break;
        default:
          is_common = false;
          break;
      }
      if (is_common) continue;
      if (com_type & SERVER_TYPE)
      {
        switch (cmd)
        {
          case CMD_SERVER_ID:
            if (get_value((*iter).c_str(), ret_param.server_ip_port_) == TFS_SUCCESS)
            {
              if (ret_param.server_ip_port_.find_first_of(":") == string::npos)
              {
                ret = TFS_ERROR;
              }
            }
            break;
          case CMD_BLOCK_LIST:
            ret_param.type_ = SERVER_TYPE_BLOCK_LIST;
            break;
          case CMD_BLOCK_WRITABLE:
            ret_param.type_ = SERVER_TYPE_BLOCK_WRITABLE;
            break;
          case CMD_BLOCK_MASTER:
            ret_param.type_ = SERVER_TYPE_BLOCK_MASTER;
            break;
          default:
            ret = CMD_UNKNOWN;
            break;
        }
      }
      if (com_type & BLOCK_TYPE)
      {
        int32_t tmp = -1;
        switch (cmd)
        {
          case CMD_BLOCK_ID:
            if ((ret = get_value((*iter).c_str(), tmp)) == TFS_SUCCESS)
            {
              ret_param.block_id_ = static_cast< uint32_t > (tmp);
            }
            break;
          case CMD_SERVER_LIST:
            g_need_cmp ? (ret_param.type_ = BLOCK_CMP_SERVER) : (ret_param.type_ = BLOCK_TYPE_SERVER_LIST);
            break;
          case CMD_ALL:
            ret_param.type_ = BLOCK_CMP_ALL_INFO;
            break;
          case CMD_PART:
            ret_param.type_ = BLOCK_CMP_PART_INFO;
            break;
          default:
            ret = CMD_UNKNOWN;
            break;
        }
      }
      if (com_type & MACHINE_TYPE)
      {
        switch (cmd)
        {
          case CMD_ALL:
            ret_param.type_ = MACHINE_TYPE_ALL;
            break;
          case CMD_PART:
            ret_param.type_ = MACHINE_TYPE_PART;
            break;
          default:
            ret = CMD_UNKNOWN;
            break;
        }
      }
    }
    else
    {
      TBSYS_LOG(ERROR, "unknown param......");
      ret = CMD_UNKNOWN;
    }
    if (ret != TFS_SUCCESS)
    {
      TBSYS_LOG(ERROR, "unvalid value...");
      print_help();
      return ret;
    }
  }
  return TFS_SUCCESS;
}