예제 #1
0
void
Controller::startCommand(const shared_ptr<ControlCommand>& command,
                         const ControlParameters& parameters,
                         const CommandSucceedCallback& onSuccess1,
                         const CommandFailCallback& onFailure1,
                         const CommandOptions& options)
{
  const CommandSucceedCallback& onSuccess = onSuccess1 ?
    onSuccess1 : [] (const ControlParameters&) {};
  const CommandFailCallback& onFailure = onFailure1 ?
    onFailure1 : [] (const ControlResponse&) {};

  Name requestName = command->getRequestName(options.getPrefix(), parameters);
  Interest interest(requestName);
  interest.setInterestLifetime(options.getTimeout());
  m_keyChain.sign(interest, options.getSigningInfo());

  m_face.expressInterest(interest,
    [=] (const Interest&, const Data& data) {
      this->processCommandResponse(data, command, onSuccess, onFailure);
    },
    [=] (const Interest&, const lp::Nack&) {
      onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received"));
    },
    [=] (const Interest&) {
      onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out"));
    });
}
예제 #2
0
void ConfigCommand::process(const CommandOptions& options)
{
  THelper::OS::Factory osFactory;
  auto osPaths = osFactory.getPaths();
  const std::string default_config_file = osPaths->getHomeDir() + "/.guitar_learner/config.config";
  THelper::Configuration::EqualsSignConfiguratorIO confIO;
  THelper::Configuration::Configurator conf;
  {
    std::ifstream config_file(default_config_file);
    confIO.load(conf, config_file);
  }
  if (options.empty())
  {
    std::cout << "For information about possible configuration please take a look at docs/man_config.txt\n";
    return;
  }
  if (options.size() == 1)
  {
    try
    {
      std::cout << conf.loadStringParameter(options[0]) << "\n";
    }
    catch (THelper::Configuration::Configurator::NoSuchParameter &)
    {
      std::cout << "Parameter was not set\n";
    }
  }
  else if (options.size() == 2)
  {
    std::ofstream config_file(default_config_file);
    conf.storeStringParameter(options[0], options[1]);
    confIO.save(conf, config_file);
  }
}
예제 #3
0
void
RemoteRegistrator::redoRegistration()
{
  NFD_LOG_INFO("redo " << m_regEntries.size()
                       << " registration when new Hub connection is built.");

  for (auto&& entry : m_regEntries)
    {
      // make copies to avoid unreasonable overwrite.
      ControlParameters parameters = m_controlParameters;
      CommandOptions    options    = m_commandOptions;
      startRegistration(parameters.setName(entry.first),
                        options.setSigningIdentity(entry.first),
                        m_nRetries);
    }
}
예제 #4
0
bool extractFeatures(TTrainingFileList& fileList,
                     vector<TrainingExample*>& trainingSet,
                     const CommandOptions& opts) {
    Timer t("extracting features from images",opts.getVerboseFlag());
    HaarFeatures haar(opts.getVerboseFlag());
    EdgeDetectionFeatures sobel;
    Hough hough;
    IplImage *image, *smallImage;
    smallImage = cvCreateImage(cvSize(64, 64), IPL_DEPTH_8U, 1);
    vector<string> labels;
    getAllLabelStrings(labels);
    for (int i = 0; i < (int)fileList.files.size(); i++) {
        // show progress
        if (i % 1000 == 0) {
            showProgress(i, fileList.files.size());
        }
        if (find(labels.begin(),labels.end(),fileList.files[i].label)!=labels.end()) {
            // load the image
            image = cvLoadImage(fileList.files[i].filename.c_str(), 0);
            if (image == NULL) {
                cerr << "ERROR: could not load image "
                     << fileList.files[i].filename.c_str() << endl;
                continue;
            }

            // resize to 64 x 64
            cvResize(image, smallImage);
            assert(smallImage->nChannels==1); // assert its grayscale
            // CS221 TO DO: extract features from image here
            vector<double> values;
            coreExtractFeatures(smallImage, values, opts);

            trainingSet.push_back(new ImagesExample(values,
                                                    getLabel(fileList.files[i].label)));
            // free memory
            cvReleaseImage(&image);
        }
    }
    // free memory
    cvReleaseImage(&smallImage);
    return fileList.files.size()==trainingSet.size();
}
예제 #5
0
void
Controller::fetchDataset(const Name& prefix,
                         const std::function<void(const ConstBufferPtr&)>& processResponse,
                         const DatasetFailCallback& onFailure,
                         const CommandOptions& options)
{
  Interest baseInterest(prefix);
  baseInterest.setInterestLifetime(options.getTimeout());

  SegmentFetcher::fetch(m_face, baseInterest, m_validator, processResponse,
                        bind(&Controller::processDatasetFetchError, this, onFailure, _1, _2));
}
예제 #6
0
void coreExtractFeatures(IplImage* image,
                         vector<double>& values,
                         const CommandOptions& opts) {
    HaarFeatures haar(opts.getVerboseFlag());
    haar.getFeatureValues(values,image);
    if(opts.getBoolOption("circle_feature")) {
        Hough hough;
        hough.getCircles(values,image);
    }
    if(opts.getBoolOption("edge_feature")) {
        Hough hough;
        hough.getEdges(values,image);
    }
    if(opts.getBoolOption("corner_feature")) {
        Hough hough;
        hough.getCorners(values,image);
    }
    if(opts.getBoolOption("sobel_feature")) {
        EdgeDetectionFeatures sobel;
        sobel.getFeatureValues(values,image);
    }
}
예제 #7
0
void
Controller::startCommand(const shared_ptr<ControlCommand>& command,
                         const ControlParameters& parameters,
                         const CommandSucceedCallback& onSuccess,
                         const CommandFailCallback& onFailure,
                         const CommandOptions& options)
{
  Name requestName = command->getRequestName(options.getPrefix(), parameters);
  Interest interest = m_signer.makeCommandInterest(requestName, options.getSigningInfo());
  interest.setInterestLifetime(options.getTimeout());

  m_face.expressInterest(interest,
    [=] (const Interest&, const Data& data) {
      processCommandResponse(data, command, onSuccess, onFailure);
    },
    [=] (const Interest&, const lp::Nack&) {
      if (onFailure)
        onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received"));
    },
    [=] (const Interest&) {
      if (onFailure)
        onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out"));
    });
}
예제 #8
0
void
AutoPrefixPropagator::afterRibInsert(const ControlParameters& parameters,
                                     const CommandOptions& options)
{
  BOOST_ASSERT(m_propagatedEntries.find(parameters.getName()) == m_propagatedEntries.end());

  // keep valid entries although there is no connectivity to hub
  auto& entry = m_propagatedEntries[parameters.getName()]
    .setSigningIdentity(options.getSigningInfo().getSignerName());

  if (!m_hasConnectedHub) {
    NFD_LOG_INFO("no hub connected to propagate " << parameters.getName());
    return;
  }

  // NEW --> PROPAGATING
  entry.startPropagation();
  startPropagation(parameters, options, m_baseRetryWait);
}
예제 #9
0
void
Controller::fetchDataset(const Name& prefix,
                         const std::function<void(ConstBufferPtr)>& processResponse,
                         const DatasetFailCallback& onFailure,
                         const CommandOptions& options)
{
  SegmentFetcher::Options fetcherOptions;
  fetcherOptions.maxTimeout = options.getTimeout();

  auto fetcher = SegmentFetcher::start(m_face, Interest(prefix), m_validator, fetcherOptions);
  if (processResponse) {
    fetcher->onComplete.connect(processResponse);
  }
  if (onFailure) {
    fetcher->onError.connect([=] (uint32_t code, const std::string& msg) {
      processDatasetFetchError(onFailure, code, msg);
    });
  }

  auto it = m_fetchers.insert(fetcher).first;
  fetcher->onComplete.connect([this, it] (ConstBufferPtr) { m_fetchers.erase(it); });
  fetcher->onError.connect([this, it] (uint32_t, const std::string&) { m_fetchers.erase(it); });
}
예제 #10
0
void
RemoteRegistrator::unregisterPrefix(const Name& prefix)
{
  if (prefix == REMOTE_HUB_PREFIX)
    {
      NFD_LOG_INFO("disconnected to hub with prefix: " << prefix);

      // for phase 1: suppose there is at most one hub connected.
      // if the hub prefix has been unregistered locally, there may
      // be no connected hub.
      m_hasConnectedHub = false;

      clearRefreshEvents();
      return;
    }

  if (!m_hasConnectedHub)
    {
      NFD_LOG_INFO("no hub connected when unregistering " << prefix);
      return;
    }

  std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);

  if (0 == identity.second)
    {
      NFD_LOG_INFO("no proper identity found for unregistering " << prefix);
      return;
    }

  Name prefixForRegistration;
  if (identity.first.size() == identity.second)
    {
      prefixForRegistration = identity.first;
    }
  else
    {
      prefixForRegistration = identity.first.getPrefix(-1);
    }

  RegisteredEntryIt iRegEntry = m_regEntries.find(prefixForRegistration);
  if (m_regEntries.end() == iRegEntry)
    {
      NFD_LOG_INFO("no existing entry found when unregistering " << prefix);
      return;
    }

  for (auto&& entry : m_rib)
    {
      if (prefixForRegistration.isPrefixOf(entry.first) &&
          findIdentityForRegistration(entry.first) == identity)
        {
          NFD_LOG_INFO("this identity should be kept for other rib entry: "
                       << entry.first);
          return;
        }
    }

  scheduler::cancel(iRegEntry->second);
  m_regEntries.erase(iRegEntry);

  // make copies of m_controlParameters and m_commandOptions to
  // avoid unreasonable overwriting during concurrent registration
  // and unregistration.
  ControlParameters parameters = m_controlParameters;
  CommandOptions    options    = m_commandOptions;

  startUnregistration(parameters.setName(prefixForRegistration).unsetCost(),
                      options.setSigningIdentity(identity.first),
                      m_nRetries);
}
예제 #11
0
void
RemoteRegistrator::registerPrefix(const Name& prefix)
{
  if (LOCAL_REGISTRATION_PREFIX.isPrefixOf(prefix))
    {
      NFD_LOG_INFO("local registration only for " << prefix);
      return;
    }

  bool isHubPrefix = prefix == REMOTE_HUB_PREFIX;

  if (isHubPrefix)
    {
      NFD_LOG_INFO("this is a prefix registered by some hub: " << prefix);

      m_hasConnectedHub = true;

      redoRegistration();
      return;
    }

  if (!m_hasConnectedHub)
    {
      NFD_LOG_INFO("no hub connected when registering " << prefix);
      return;
    }

  std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);

  if (0 == identity.second)
    {
      NFD_LOG_INFO("no proper identity found for registering " << prefix);
      return;
    }

  Name prefixForRegistration;
  if (identity.first.size() == identity.second)
    {
      prefixForRegistration = identity.first;
    }
  else
    {
      prefixForRegistration = identity.first.getPrefix(-1);
    }

  if (m_regEntries.find(prefixForRegistration) != m_regEntries.end())
    {
      NFD_LOG_INFO("registration already in process for " << prefix);
      return;
    }

  // make copies of m_controlParameters and m_commandOptions to
  // avoid unreasonable overwriting during concurrent registration
  // and unregistration.
  ControlParameters parameters = m_controlParameters;
  CommandOptions    options    = m_commandOptions;

  startRegistration(parameters.setName(prefixForRegistration),
                    options.setSigningIdentity(identity.first),
                    m_nRetries);
}