Exemple #1
0
inline void duk_push_json(duk_context *ctx, Json::Value val) {
	Json::ValueType type = val.type();

	int length, obj_index;

	switch (type) {
		case Json::nullValue:
			duk_push_null(ctx);
			break;
		case Json::intValue:
		case Json::uintValue:
		case Json::realValue:
			duk_push_number(ctx, val.asDouble());
			break;
		case Json::stringValue:
			duk_push_string(ctx, val.asCString());
			break;
		case Json::booleanValue:
			duk_push_boolean(ctx, val.asBool());
			break;
		case Json::arrayValue:
			length = val.size();
			obj_index = duk_push_array(ctx);

			for (int ndx = 0; ndx < length; ndx ++) {
				duk_push_json(ctx, val[ndx]);
				duk_put_prop_index(ctx, obj_index, ndx);
			}
			break;
		case Json::objectValue:
			obj_index = duk_push_object(ctx);

			Json::Value::Members keys = val.getMemberNames();
			Json::Value::Members::iterator it = keys.begin();
			while (it != keys.end()) {
				Json::Value value = val[*it];
				duk_push_string(ctx, it->c_str());
				duk_push_json(ctx, value);

				duk_put_prop(ctx, obj_index);

				it ++;
			}
			break;
	}
}
bool CStreamManager::ReadJson(string strJson)
{
	Json::Value objRead;

	if (!m_JsonReader.parse(strJson, objRead))
	{
		return false;
	}

	Json::Value::Members members = objRead.getMemberNames();
	Json::Value::Members::iterator it;
	for (it = members.begin(); it != members.end(); it++)
	{
		string strResult = it->c_str();
		cout << "result key : " << strResult << endl;
		strResult = objRead[strResult].asString();
		cout << "result value : " << strResult << endl;
	}

	return true;
}
Exemple #3
0
engine_t::engine_t(context_t& context, const manifest_t& manifest):
    m_context(context),
    m_log(context.log(
        (boost::format("app/%1%")
            % manifest.name
        ).str()
    )),
    m_manifest(manifest),
    m_state(stopped),
    m_thread(NULL),
    m_bus(new io::channel_t(context.io(), m_manifest.name)),
    m_watcher(m_loop),
    m_processor(m_loop),
    m_check(m_loop),
    m_gc_timer(m_loop),
    m_termination_timer(m_loop),
    m_notification(m_loop)
#ifdef HAVE_CGROUPS
    , m_cgroup(NULL)
#endif
{
    int linger = 0;

    m_bus->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));

    std::string endpoint(
        (boost::format("ipc://%1%/%2%")
            % m_context.config.ipc_path
            % m_manifest.name
        ).str()
    );

    try {
        m_bus->bind(endpoint);
    } catch(const zmq::error_t& e) {
        throw configuration_error_t(std::string("invalid rpc endpoint - ") + e.what());
    }
    
    m_watcher.set<engine_t, &engine_t::message>(this);
    m_watcher.start(m_bus->fd(), ev::READ);
    m_processor.set<engine_t, &engine_t::process>(this);
    m_check.set<engine_t, &engine_t::check>(this);
    m_check.start();

    m_gc_timer.set<engine_t, &engine_t::cleanup>(this);
    m_gc_timer.start(5.0f, 5.0f);

    m_notification.set<engine_t, &engine_t::notify>(this);
    m_notification.start();

#ifdef HAVE_CGROUPS
    Json::Value limits(m_manifest.root["resource-limits"]);

    if(!(cgroup_init() == 0) || !limits.isObject() || limits.empty()) {
        return;
    }
    
    m_cgroup = cgroup_new_cgroup(m_manifest.name.c_str());

    // XXX: Not sure if it changes anything.
    cgroup_set_uid_gid(m_cgroup, getuid(), getgid(), getuid(), getgid());
    
    Json::Value::Members controllers(limits.getMemberNames());

    for(Json::Value::Members::iterator c = controllers.begin();
        c != controllers.end();
        ++c)
    {
        Json::Value cfg(limits[*c]);

        if(!cfg.isObject() || cfg.empty()) {
            continue;
        }
        
        cgroup_controller * ctl = cgroup_add_controller(m_cgroup, c->c_str());
        
        Json::Value::Members parameters(cfg.getMemberNames());

        for(Json::Value::Members::iterator p = parameters.begin();
            p != parameters.end();
            ++p)
        {
            switch(cfg[*p].type()) {
                case Json::stringValue: {
                    cgroup_add_value_string(ctl, p->c_str(), cfg[*p].asCString());
                    break;
                } case Json::intValue: {
                    cgroup_add_value_int64(ctl, p->c_str(), cfg[*p].asInt());
                    break;
                } case Json::uintValue: {
                    cgroup_add_value_uint64(ctl, p->c_str(), cfg[*p].asUInt());
                    break;
                } case Json::booleanValue: {
                    cgroup_add_value_bool(ctl, p->c_str(), cfg[*p].asBool());
                    break;
                } default: {
                    m_log->error(
                        "controller '%s' parameter '%s' type is not supported",
                        c->c_str(),
                        p->c_str()
                    );

                    continue;
                }
            }
            
            m_log->debug(
                "setting controller '%s' parameter '%s' to '%s'", 
                c->c_str(),
                p->c_str(),
                boost::lexical_cast<std::string>(cfg[*p]).c_str()
            );
        }
    }

    int rv = 0;

    if((rv = cgroup_create_cgroup(m_cgroup, false)) != 0) {
        m_log->error(
            "unable to create the control group - %s", 
            cgroup_strerror(rv)
        );

        cgroup_free(&m_cgroup);
        m_cgroup = NULL;
    }
#endif
}
//RTK连接接收报文解析
bool ApsAppRequestMessage::ParseRequestMessage(std::string &message)
{
	//
	bool ret = m_reader.parse(message, m_root);
	if (!ret)//The message is not json
	{
		ApsLogger::Debug("Failed to parse request message!"); 
	}
	else//pares json message ok
	{
		//

		m_command = m_root["name"].asString();
		const char * cmd = m_command.c_str();
		if(!strcmp(cmd,"connect"))
		{
			m_appID= m_root["appid"].asString();
			m_cleanFlag= m_root["cleansession"].asBool();
			m_keepalive = m_root["keepalive"].asInt();
		}
		else if(!strcmp(cmd,"publish"))
		{
			m_mid= m_root["msgid"].asInt();
			m_qos= m_root["Qos"].asInt();
			m_remainFlag= m_root["remain"].asBool();
			Json::Value options = m_root["options"];
			m_timeOnLive= options["time_to_live"].asInt();
		
			Json::Value audience = m_root["audience"];
			Json::Value::Members members(audience.getMemberNames());
        	//std::sort( members.begin(), members.end() );
        	for (Json::Value::Members::iterator it = members.begin();it != members.end(); ++it)
        	{
        		if(strcmp(it->c_str(), "usrid")==0)
        		{
					m_userID= audience["usrid"].asCString();
				}
				else if(strcmp(it->c_str(), "devid")==0)
				{
					m_deviceID= audience["devid"].asCString();
				}
       		}

			Json::Value alert;
			bool alert_flag = false;
			bool ios_flag = false;
			bool android_flag = false;
			Json::Value notification = m_root["notification"];
			members = notification.getMemberNames();
        	//std::sort( members.begin(), members.end() );
        	for ( Json::Value::Members::iterator it = members.begin();it != members.end(); ++it )
        	{
        		if(strcmp(it->c_str(), "alert")==0)
        		{
					alert = notification["alert"];
					alert_flag = true;
				}
				else if(strcmp(it->c_str(), "ios")==0 )
				{
					ios_flag = true;
					m_iOS = notification["ios"];
					bool ios_flag = false;
					Json::Value::Members ios_members = m_iOS.getMemberNames();
        			//std::sort( members.begin(), members.end() );
        			for (Json::Value::Members::iterator it_ios = ios_members.begin();it_ios != ios_members.end(); ++it_ios )
        			{
        				if(strcmp(it_ios->c_str(), "alert")==0 )
        				{
							ios_flag = true;
							break;
						}
        			}
					if(!ios_flag)
					{
						if(alert_flag)
						{
							m_iOS["alert"]=alert;
						}
					}
				}
				else if(strcmp(it->c_str(), "android")==0)
				{
					android_flag = true;
					m_android= notification["android"];
					bool android_flag = false;
					Json::Value::Members android_members = m_android.getMemberNames();
        			for ( Json::Value::Members::iterator it_android = android_members.begin();it_android != android_members.end(); ++it_android )
        			{
        				if( strcmp(it_android->c_str(), "alert")==0 )
        				{
							android_flag = true;
							break;
						}
        			}
					if(!android_flag)
					{
						if(alert_flag)
						{
							m_android["alert"]=alert;
						}
					}
				}
        	}
			if(alert_flag)
			{
				if(!android_flag)
				{
					m_android["alert"]=alert;
				}
				if(!ios_flag)
				{
					m_iOS["alert"]=alert;
				}
			}
		} 
	}
	return ret;
}
cgroups_t::cgroups_t(context_t& context,
                     const std::string& name,
                     const Json::Value& args):
    category_type(context, name, args),
    m_context(context),
    m_log(new log_t(context, name))
{
    int rv = 0;

    if((rv = cgroup_init()) != 0) {
        throw configuration_error_t(
            "unable to initialize the cgroups isolate - %s",
            cgroup_strerror(rv)
        );
    }

    m_cgroup = cgroup_new_cgroup(name.c_str());

    // TODO: Check if it changes anything.
    cgroup_set_uid_gid(m_cgroup, getuid(), getgid(), getuid(), getgid());

    Json::Value::Members controllers(args.getMemberNames());

    for(Json::Value::Members::iterator c = controllers.begin();
        c != controllers.end();
        ++c)
    {
        Json::Value cfg(args[*c]);

        if(!cfg.isObject() || cfg.empty()) {
            continue;
        }

        cgroup_controller * ctl = cgroup_add_controller(m_cgroup, c->c_str());

        Json::Value::Members parameters(cfg.getMemberNames());

        for(Json::Value::Members::iterator p = parameters.begin();
            p != parameters.end();
            ++p)
        {
            switch(cfg[*p].type()) {
                case Json::stringValue: {
                    cgroup_add_value_string(ctl, p->c_str(), cfg[*p].asCString());
                    break;
                } case Json::intValue: {
                    cgroup_add_value_int64(ctl, p->c_str(), cfg[*p].asInt());
                    break;
                } case Json::uintValue: {
                    cgroup_add_value_uint64(ctl, p->c_str(), cfg[*p].asUInt());
                    break;
                } case Json::booleanValue: {
                    cgroup_add_value_bool(ctl, p->c_str(), cfg[*p].asBool());
                    break;
                } default: {
                    COCAINE_LOG_WARNING(m_log, "cgroup controller '%s' parameter '%s' type is not supported", *c, *p);
                    continue;
                }
            }

            COCAINE_LOG_DEBUG(
                m_log,
                "setting cgroup controller '%s' parameter '%s' to '%s'",
                *c,
                *p,
                boost::lexical_cast<std::string>(cfg[*p])
            );
        }
    }

    if((rv = cgroup_create_cgroup(m_cgroup, false)) != 0) {
        cgroup_free(&m_cgroup);
        throw configuration_error_t("unable to create the cgroup - %s", cgroup_strerror(rv));
    }
}