List<std::string> ConfigurationManager::getPluginsList (const std::string& path_to_file)
throw (BadConfigurationException)
{
    ConfigurationImpl conf;

    std::string fname =
        path_to_file.substr(path_to_file.rfind("/")+1, path_to_file.size());
    iParamFactory* param_factory = iParamFactory::makeParamFactory(fname);
    std::ifstream conf_stream((path_to_file).c_str());
    if (!conf_stream)
        THROW(BadConfigurationException, std::string("Bad conf filename: ")
              + (path_to_file).c_str());

    while ( !conf_stream.eof() )
    {
        std::string buffer_line;
        getline (conf_stream, buffer_line);
        if (buffer_line.empty() && !conf_stream.eof())
            continue;

        iParam* param = param_factory->makeParam(buffer_line);
        if (param) {
            param->fill(&conf);
            delete param;
        }
        param = NULL;
    }
    return conf.getStringList("PluginName");
}
void ConfigurationManager::loadConfiguration (iConfiguration* conf,
        const List<std::string>& paths)
throw (BadConfigurationException)
{
    try {
        List<std::string>::const_iterator path;
        for (path = paths.begin(); path != paths.end(); path++)
        {
            iParamFactory* param_factory;
            iParam* param = NULL;

            List<std::string> fnames = getFilenames(*path);
            List<std::string>::iterator filename;
            for (filename = fnames.begin(); filename != fnames.end(); filename++)
            {
                param_factory = iParamFactory::makeParamFactory(*filename);
                std::ifstream conf_stream((*path + *filename).c_str());
                if (!conf_stream)
                    THROW(BadConfigurationException, std::string("Bad conf filename: ")
                          + (*path + *filename).c_str());

                while ( !conf_stream.eof() )
                {
                    std::string buffer_line;
                    getline (conf_stream, buffer_line);
                    if (buffer_line.empty() && !conf_stream.eof())
                        continue;

                    param = param_factory->makeParam(buffer_line);
                    if (param != NULL) {
                        param->fill(conf);
                        delete param;
                    }
                    param = NULL;
                }
            }
        }
    }
    catch (const std::exception& ex) {
        throw BadConfigurationException(ex.what());
    }
}
Exemplo n.º 3
0
int LoadGroupConfig(
    const std::string &conf_path,
    ::bbts::GroupConfig *group_config) {
  if (conf_path.empty() || group_config == NULL) {
    return -1;
  }

  // load configure file
  int conf_fd = open(conf_path.c_str(), O_RDONLY);
  if (conf_fd < 0) {
    fprintf(stderr, "open conf file(%s) failed, errno(%d): %s\n",
               conf_path.c_str(), errno, strerror(errno));
    return -1;
  }
  ::google::protobuf::io::FileInputStream conf_stream(conf_fd);
  conf_stream.SetCloseOnDelete(true);
  if (!::google::protobuf::TextFormat::Parse(&conf_stream, group_config)) {
    fprintf(stderr, "prase conf file(%s) failed!\n", conf_path.c_str());
    return -1;
  }

  // set absolute_path
  std::string absolute_path = ::bbts::complete_full_path(::bbts::parent_path(conf_path));
  group_config->set_absolute_path(absolute_path);

  // check if param out of range, so terrible...
  group_config->set_download_rate_limit(SetLimitIntValue(kMinDownloadLimit, kMaxDownloadLimit, group_config->download_rate_limit()));
  group_config->set_upload_rate_limit(SetLimitIntValue(kMinUploadLimit, kMaxUploadLimit, group_config->upload_rate_limit()));
  group_config->set_connection_limit(SetLimitIntValue(kMinConnectionLimit, kMaxConnectionLimit, group_config->connection_limit()));

  group_config->set_max_metadata_size(SetLimitIntValue(kMinMetadataSize, kMaxMetadataSize, group_config->max_metadata_size()));
  group_config->set_peers_num_want(SetLimitIntValue(kMinPeersNumWant, kMaxPeersNumWant, group_config->peers_num_want()));
  group_config->set_seed_announce_interval(SetLimitIntValue(kMinSeedAnnounceInterval, kMaxSeedAnnounceInterval, group_config->seed_announce_interval()));
  group_config->set_min_reconnect_time(SetLimitIntValue(kMinReconnectTime, kMaxReconnectTime, group_config->min_reconnect_time()));
  group_config->set_max_queued_disk_bytes(SetLimitIntValue(kMinQueuedDiskBytes, kMaxQueuedDiskBytes, group_config->max_queued_disk_bytes()));
  group_config->set_max_out_request_queue(SetLimitIntValue(kMinOutRequestQueue, kMaxOutRequestQueue, group_config->max_out_request_queue()));
  group_config->set_max_allowed_in_request_queue(SetLimitIntValue(kMinAllowedInRequestQueue, kMaxAllowedInRequestQueue, group_config->max_allowed_in_request_queue()));
  group_config->set_whole_pieces_threshold(SetLimitIntValue(kMinWholePiecesThreshold, kMaxWholePiecesThreshold, group_config->whole_pieces_threshold()));
  group_config->set_request_queue_time(SetLimitIntValue(kMinRequestQueueTime, kMaxRequestQueueTime, group_config->request_queue_time()));
  group_config->set_cache_size(SetLimitIntValue(kMinCacheSize, kMaxCacheSize, group_config->cache_size()));
  group_config->set_cache_expiry(SetLimitIntValue(kMinCacheExpiry, kMaxCacheExpiry, group_config->cache_expiry()));
  group_config->set_read_cache_line_size(SetLimitIntValue(kMinReadCacheLineSize, kMaxReadCacheLineSize, group_config->read_cache_line_size()));
  group_config->set_write_cache_line_size(SetLimitIntValue(kMinWriteCacheLineSize, kMaxWriteCacheLineSize, group_config->write_cache_line_size()));
  group_config->set_file_pool_size(SetLimitIntValue(kMinFilePoolSize, kMaxFilePoolSize, group_config->file_pool_size()));
  group_config->set_send_buffer_watermark(SetLimitIntValue(kMinSendBufferWatermark, kMaxSendBufferWatermark, group_config->send_buffer_watermark()));
  group_config->set_send_buffer_low_watermark(SetLimitIntValue(kMinSendBufferLowWatermark, kMaxSendBufferLowWatermark, group_config->send_buffer_low_watermark()));
  group_config->set_send_socket_buffer_size(SetLimitIntValue(kMinSendSocketBufferSize, kMaxSendSocketBufferSize, group_config->send_socket_buffer_size()));
  group_config->set_recv_socket_buffer_size(SetLimitIntValue(kMinRecvSocketBufferSize, kMaxRecvSocketBufferSize, group_config->recv_socket_buffer_size()));
  group_config->set_active_seeds(SetLimitIntValue(kMinActiveSeeds, kMaxActiveSeeds, group_config->active_seeds()));
  group_config->set_active_limit(SetLimitIntValue(kMinActiveLimit, kMaxActiveLimit, group_config->active_limit()));
  group_config->set_active_downloads(SetLimitIntValue(kMinActiveDownloads, kMaxActiveDownloads, group_config->active_downloads()));
  group_config->set_listen_port(SetLimitIntValue(kMinListenPort, kMaxListenPort, group_config->listen_port()));
  group_config->set_peer_connection_timeout(SetLimitIntValue(kMinPeerConnectionTimeout, kMaxPeerConnectionTimeout, group_config->peer_connection_timeout()));

  // change unit format
  group_config->set_download_rate_limit(group_config->download_rate_limit() * 1024 * 1024);
  group_config->set_upload_rate_limit(group_config->upload_rate_limit() * 1024 * 1024);
  group_config->set_max_metadata_size(group_config->max_metadata_size() * 1024 * 1024);
  group_config->set_max_queued_disk_bytes(group_config->max_queued_disk_bytes() * 1024 * 1024);
  group_config->set_cache_size(group_config->cache_size() * 1024 * 1024);
  group_config->set_send_buffer_watermark(group_config->send_buffer_watermark() * 1024 * 1024);

  group_config->set_send_buffer_low_watermark(group_config->send_buffer_low_watermark() * 1024);
  group_config->set_send_socket_buffer_size(group_config->send_socket_buffer_size() * 1024);
  group_config->set_recv_socket_buffer_size(group_config->recv_socket_buffer_size() * 1024);

  return 0;
}