Esempio n. 1
0
//----------------------------------------------------------------------------------
bool g3d::VoxelSpace_c::load(const std::string &FileName,YAML::Node &user_doc)
{
	bool Res = true;
	
	mcv::MixedFileManager_c FManager;
	std::stringstream YamlHeader = FManager.Parse(FileName);
	if(YamlHeader.str().empty())
	{
		std::cout << "VoxelSpace_c::load() Couldn't load File: " << FileName << std::endl;
		return false;
	}
	YAML::Parser parser;
	parser.Load(YamlHeader);
	parser.GetNextDocument(user_doc);
	YAML::Node doc;
	parser.GetNextDocument(doc);
	
	doc["VoxelSize"] >> VoxelSize;
	int TotalVoxels,vXv,vYv,vZv;
	doc["Xv"] >> vXv;
	doc["Yv"] >> vYv;
	doc["Zv"] >> vZv;
	doc["TotalVoxels"] >> TotalVoxels;
	int vNbFill;
	doc["NbFill"] >> vNbFill;
	EigenBox3D vBox;
	doc["VoxBox"] >> vBox;

	//This is the load SetUp as the data is the master with its Xv,Yv,Zv
	//and the <float> is not guarantied through text save and load
	//whether use double if more precision is needed but won't correct the text conversion issue
	//or save the critical information on data, but it becomes no more editable by the user
	//the remaining issue is the float -> text -> float conversion !!! ??? is this safe ?
	Reset(vBox.Low,vXv,vYv,vZv,VoxelSize);//will get Xv = vXv ....

	doc["TypeName"] >> TypeName;
	doc["ColorTable"] >> ColorTable;

	if(doc.FindValue("ClassesNames"))
	{
		doc["ClassesNames"] >> ClassesNames;
	}
Esempio n. 2
0
void Settings::ParseInitConfig() {
    QFile launcherFile(DirectoryManager::GetInstance()->GetConfigDir() + InitFile);
    if (!launcherFile.open(QFile::ReadOnly)) {
        Logger::GetInstance()->AddLog(tr("Error read launcher config"));
        return;
    }
    YAML::Parser launcherParser;
    std::istringstream launcherStream(launcherFile.readAll().data());
    launcherParser.Load(launcherStream);
    launcherFile.close();

    YAML::Node launcherSettings;
    if (!launcherParser.GetNextDocument(launcherSettings)) {
        Logger::GetInstance()->AddLog(tr("Error parse launcher settings."));
        return;
    }

    AppsConfig newConfig;
    const YAML::Node* pTimer = launcherSettings.FindValue(CONFIG_UPDATE_INTERVAL);
    if (pTimer) {
        QString interval;
        setString(interval, pTimer);
        m_nUpdateTimer = interval.toInt() * 60 * 1000;
    }
    const YAML::Node* Launcher = launcherSettings.FindValue(LAUNCHER);
    if (Launcher) {
        const YAML::Node* LauncherVer = Launcher->FindValue(LAUNCHER_VER);
        if (LauncherVer)
            setString(newConfig.m_Launcher.m_Version, LauncherVer);
        const YAML::Node* LauncherUrl = Launcher->FindValue(LAUNCHER_UPDATE_URL);
        if (LauncherUrl)
            setString(newConfig.m_Launcher.m_Url, LauncherUrl);
    }

    QFile docFile(DirectoryManager::GetInstance()->GetDocumentsDirectory() + InitFile);
    if(docFile.open(QFile::ReadOnly)) {
        YAML::Parser docParser;
        std::istringstream docStream(docFile.readAll().data());
        docParser.Load(docStream);
        docFile.close();

        YAML::Node docSettings;
        docParser.GetNextDocument(docSettings);

        ParseAppConfig(&docSettings, STABLE, newConfig.m_Stable);
        ParseAppConfig(&docSettings, QA, newConfig.m_Test);
        ParseAppConfig(&docSettings, DEVELOPMENT, newConfig.m_Development);
        ParseAppConfig(&docSettings, DEPENDENCIES, newConfig.m_Dependencies);
    }


    m_Config = newConfig;

}
    // Called from the consumer since this class is a registered MessageListener.
    virtual void
      onMessage (const Message * message)
      throw ()
      {

        static int
          count = 0;

        try
        {
          count++;
          const TextMessage *
            textMessage = dynamic_cast < const
            TextMessage * >(message);

          const BytesMessage *
            bytesMessage = dynamic_cast < const
            BytesMessage * >(message);

          string text = "";

          if (bytesMessage != NULL)
          {
            //std::cout << bytesMessage->getBodyBytes();
            //bytesMessage->reset();

            size_t i = bytesMessage->getBodyLength ();
            printf ("%lu", i);
            ofstream ofs ("message.yaml", ofstream::out);
            for (int x = 1; x <= i; x++)
            {
              ofs << bytesMessage->readByte ();
            }
            ofs.flush ();
            ofs.close ();

            try
            {
              std::ifstream fin ("message.yaml");
              //std::stringstream fin(std::string(bytesMessage->getBodyBytes()));
              YAML::Parser parser (fin);
              YAML::Node doc;
              // We assume only the first doc, need to check with doc.size
              parser.GetNextDocument (doc);

              /*
Key: :agent
Key: :body
Key: :callerid
Key: :collectiv
Key: :filter
Key: :hash
Key: :msgtarget
Key: :msgtime
Key: :requestid
Key: :senderid
*/
              for (YAML::Iterator it = doc.begin (); it != doc.end (); ++it)
              {
                std::string key, value;
                it.first () >> key;
                std::cout << "Key: " << key << std::endl;
              }

              std::string requestid;
              std::string senderid;
              std::string msgtarget;
              doc[":msgtarget"] >> msgtarget;
              doc[":requestid"] >> requestid;
              doc[":senderid"] >> senderid;
              std::cout << msgtarget << std::endl << requestid << std::
                endl << senderid << std::endl;

              // Body seems to be multiline string of yaml
              // Parsing strings http://stackoverflow.com/questions/2813030/yaml-cpp-parsing-strings
              std::string body;
              doc[":body"] >> body;

              std::stringstream bodystream (body);
              YAML::Parser bodyparser (bodystream);
              YAML::Node bodydoc;
              std::string action;
              bodyparser.GetNextDocument (bodydoc);
              bodydoc >> action;
              std::cout << action;


              // Construct YAML body
              YAML::Emitter reply_message_body_yaml;
              reply_message_body_yaml << "pong";

              // Put it in a string
              std::string reply_message_body = reply_message_body_yaml.c_str();
              std::cout << reply_message_body << std::endl;
              // Append PSK to it
              std::string psk = "unset";
              std::string body_psk = reply_message_body;
              body_psk.append(psk);

              std::stringstream md5sumstream;
              // MD5 -  https://gist.github.com/2389719
              // but needs a real string
              // http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/e1774395-ba99-4fe6-98eb-2224a67984b9
              unsigned char md5_result[MD5_DIGEST_LENGTH];
              const unsigned char * constStr = reinterpret_cast<const unsigned char *> (body_psk.c_str());
              MD5(constStr, body_psk.length() , md5_result);
              for (i=0; i < MD5_DIGEST_LENGTH; i++)
              {
                printf("%02x",  md5_result[i]);
                char digit[2];
                sprintf(digit,"%02x",  md5_result[i]);
                md5sumstream << digit;
              }
              printf("\n");

              std::cout << "md5 stream:" << md5sumstream.str() << std::endl;
              std::string hash = md5sumstream.str();
              std::cout << "hash:" << hash << std::endl;

              YAML::Emitter reply_message_yaml;

              reply_message_yaml << YAML::BeginMap;
              reply_message_yaml << YAML::Key << ":msgtime";
              reply_message_yaml << YAML::Value << 1010101;
              reply_message_yaml << YAML::Key << ":requestid";
              reply_message_yaml << YAML::Value << requestid;
              reply_message_yaml << YAML::Key << ":body";
              reply_message_yaml << YAML::Value << reply_message_body;
              reply_message_yaml << YAML::Key << ":senderid";
              reply_message_yaml << YAML::Value << "mcpp";
              reply_message_yaml << YAML::Key << ":senderagent";
              reply_message_yaml << YAML::Value << "discovery";
              reply_message_yaml << YAML::Key << ":msgtarget";
              reply_message_yaml << YAML::Value << "/topic/mcollective.discovery.reply";

              reply_message_yaml << YAML::Key << ":hash";
              reply_message_yaml << YAML::Value << hash;
              reply_message_yaml << YAML::EndMap;


              // Put it in a string
              std::string reply_message = reply_message_yaml.c_str();
              std::cout << reply_message << std::endl;


              // Get ready to send
              try {

                Connection* connection;
                Session* session;
                Destination* destination;
                MessageProducer* producer;
                // Create a ConnectionFactory
                std::string brokerURI = "tcp://127.0.0.1:6163" "?wireFormat=stomp";

                auto_ptr<ConnectionFactory> connectionFactory(
                    ConnectionFactory::createCMSConnectionFactory( brokerURI ) );

                // Create a Connection
                connection = connectionFactory->createConnection ("mcollective", "marionette");
                connection->start();

                // Create a Session
                session = connection->createSession( Session::AUTO_ACKNOWLEDGE );
                // Create the destination (Topic or Queue)
                destination = session->createTopic( "mcollective.discovery.reply" );

                // Create a MessageProducer from the Session to the Topic or Queue
                producer = session->createProducer( destination );
                producer->setDeliveryMode( DeliveryMode::NON_PERSISTENT );

                // Create a messages
                BytesMessage* reply = session->createBytesMessage();

                reply->writeString(reply_message.c_str());
                producer->send( reply );
                printf("reply send \n");

                delete reply;

              }catch ( CMSException& e ) {
                e.printStackTrace();
              }


            }
            catch (YAML::ParserException & e)
            {
              std::cout << e.what () << "\n";
            }
          }

          if (textMessage != NULL)
          {
            text = textMessage->getText ();
          }
          else
          {
            text = "NOT A TEXTMESSAGE!";
            //std::cout << message->getCMSType();
            std::vector < string > v = message->getPropertyNames ();
            for (std::vector < string >::iterator it = v.begin ();
                it != v.end (); ++it)
            {
              std::cout << *it << std::endl;
            }
          }
          printf ("Message #%d Received: %s\n", count, text.c_str ());




        }
Esempio n. 4
0
YamlConfigurationNode *
YamlConfiguration::read_yaml_file(std::string filename, bool ignore_missing,
				  std::queue<LoadQueueEntry> &load_queue,
                                  std::string &host_file)
{
  if (access(filename.c_str(), R_OK) == -1) {
    if (ignore_missing) {
      return NULL;
    }
    throw Exception(errno, "YamlConfig: cannot access file %s", filename.c_str());
  }

#ifdef HAVE_YAMLCPP_0_5
  std::vector<YAML::Node> docs;
#else
  std::ifstream fin(filename.c_str());
  YAML::Parser parser;
  YAML::Node doc1, doc2;
#endif
  bool have_doc1 = false, have_doc2 = false;

  try {
#ifdef HAVE_YAMLCPP_0_5
    docs = YAML::LoadAllFromFile(filename);
    have_doc1 = docs.size() > 0;
    have_doc2 = docs.size() > 1;
#else
    parser.Load(fin);
    have_doc1 = parser.GetNextDocument(doc1);
    have_doc2 = parser.GetNextDocument(doc2);
#endif
  } catch (YAML::ParserException &e) {
    throw CouldNotOpenConfigException("Failed to parse %s line %i column %i: %s",
				      filename.c_str(), e.mark.line, e.mark.column,
				      e.msg.c_str());
  }

  YamlConfigurationNode *sub_root = NULL;

  if (! have_doc1) {
    //throw Exception("YamlConfig: file %s contains no document", filename.c_str());
    // empty -> ignore
  } else if (have_doc1 && have_doc2) {
    // we have a meta info and a config document
#ifdef HAVE_YAMLCPP_0_5
    read_meta_doc(docs[0], load_queue, host_file);
    read_config_doc(docs[1], sub_root);
#else
    read_meta_doc(doc1, load_queue, host_file);
    read_config_doc(doc2, sub_root);
#endif

  } else {
    // only one, assume this to be the config document
#ifdef HAVE_YAMLCPP_0_5
    read_config_doc(docs[0], sub_root);
#else
    read_config_doc(doc1, sub_root);
#endif
  }

  return sub_root;
}
int ConfigManager::readConfigFile(int fd, std::istream* stream)
{
    YAML::Parser p;
    YAML::Node doc;
    int ret = 0;
    std::vector<v4l2_ext_control> controls;

    try
    {
        p.Load(*stream);
    }
    catch(YAML::Exception& e)
    {
        ROS_FATAL("Could not parse config file: %s", e.what());
        return -1;
    }

    if(!p.GetNextDocument(doc))
    {
        ROS_FATAL("Could not get YAML document");
        return -1;
    }

    for(YAML::Iterator it = doc.begin(); it != doc.end(); ++it)
    {
        std::string key, value;

        try
        {
            (*it).begin().first() >> key;
            (*it).begin().second() >> value;
        }
        catch(YAML::Exception& e)
        {
            ROS_FATAL("Invalid parameter specification: %s", e.what());
            return -1;
        }

        int ivalue = -1;
        calibration::CameraParam* info = getParamInfo(key);
        if(!info)
        {
            ROS_FATAL("Unknown parameter '%s' in config file, ignoring...", key.c_str());
            continue;
        }

        ivalue = atoi(value.c_str());

        info->value = ivalue;
        v4l2_ext_control control;
        memset(&control, 0, sizeof(control));
        control.id = info->id;
        control.value = ivalue;

        controls.push_back(control);
    }

    v4l2_ext_controls request;
    memset(&request, 0, sizeof(request));
    request.controls = &controls[0];
    request.count = controls.size();
    request.ctrl_class = V4L2_CTRL_CLASS_USER;

    if(ioctl(fd, VIDIOC_S_EXT_CTRLS, &request) != 0)
    {
        ROS_FATAL("Control setting failed. This may have happened at control 0x%X with value %d",
                  controls[request.error_idx].id,
                  controls[request.error_idx].value
                 );
        return -1;
    }
    ROS_INFO("Controls set successfully from config file");

    return ret;
}