Esempio n. 1
0
void ObjectLoader::OnResult(const Reply &r)
{
	if (r.type != Reply::MULTI_BULK || r.multi_bulk.empty() || !me->redis)
	{
		delete this;
		return;
	}

	Serialize::TypeBase *type = obj->GetSerializableType();

	for (Reply *reply : r.multi_bulk)
	{
		const Anope::string &key = reply->bulk;
		Serialize::FieldBase *field = type->GetField(key);

		if (field == nullptr)
			continue;

		std::vector<Anope::string> args = { "GET", "values:" + stringify(obj->id) + ":" + key };

		me->redis->SendCommand(new FieldLoader(me, obj, field), args);
	}

	delete this;
}
Esempio n. 2
0
    void OnSaveDatabase() override
    {
        BackupDatabase();

        Anope::string db_name = Anope::DataDir + "/" + Config->GetModule(this)->Get<Anope::string>("database", "anope.db");

        if (Anope::IsFile(db_name))
            rename(db_name.c_str(), (db_name + ".tmp").c_str());

        std::fstream f(db_name.c_str(), std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);

        if (!f.is_open())
        {
            Log(this) << "Unable to open " << db_name << " for writing";
        }
        else
        {
            for (std::pair<Serialize::ID, Serialize::Object *> p : Serialize::objects)
            {
                Serialize::Object *object = p.second;
                Serialize::TypeBase *s_type = object->GetSerializableType();

                f << "OBJECT " << s_type->GetName() << "\n";
                f << "ID " << object->id << "\n";
                for (Serialize::FieldBase *field : s_type->GetFields())
                    if (field->HasFieldS(object)) // for ext
                        f << "DATA " << field->serialize_name << " " << field->SerializeToString(object) << "\n";
                f << "END\n";
            }
        }

        if (!f.is_open() || !f.good())
        {
            f.close();
            rename((db_name + ".tmp").c_str(), db_name.c_str());
        }
        else
        {
            f.close();
            unlink((db_name + ".tmp").c_str());
        }
    }
Esempio n. 3
0
void SubscriptionListener::OnResult(const Reply &r)
{
	/*
	 * message
	 * anope
	 * message
	 *
	 * set 4 email [email protected]
	 * unset 4 email
	 * create 4 NickCore
	 * delete 4
	 */

	const Anope::string &message = r.multi_bulk[2]->bulk;
	Anope::string command;
	spacesepstream sep(message);

	sep.GetToken(command);

	if (command == "set" || command == "unset")
	{
		Anope::string sid, key, value;

		sep.GetToken(sid);
		sep.GetToken(key);
		value = sep.GetRemaining();

		Serialize::ID id;
		try
		{
			id = convertTo<Serialize::ID>(sid);
		}
		catch (const ConvertException &ex)
		{
			Log(LOG_DEBUG) << "redis: unable to get id for SL update key " << sid;
			return;
		}

		Serialize::Object *obj = Serialize::GetID(id);
		if (obj == nullptr)
		{
			Log(LOG_DEBUG) << "redis: pmessage for unknown object #" << id;
			return;
		}

		Serialize::FieldBase *field = obj->GetSerializableType()->GetField(key);
		if (field == nullptr)
		{
			Log(LOG_DEBUG) << "redis: pmessage for unknown field of object #" << id << ": " << key;
			return;
		}

		Log(LOG_DEBUG_2) << "redis: Setting field " << field->serialize_name << " of object #" << obj->id << " of type " << obj->GetSerializableType()->GetName() << " to " << value;
		field->UnserializeFromString(obj, value);
	}
	else if (command == "create")
	{
		Anope::string sid, stype;

		sep.GetToken(sid);
		sep.GetToken(stype);

		Serialize::ID id;
		try
		{
			id = convertTo<Serialize::ID>(sid);
		}
		catch (const ConvertException &ex)
		{
			Log(LOG_DEBUG) << "redis: unable to get id for SL update key " << sid;
			return;
		}

		Serialize::TypeBase *type = Serialize::TypeBase::Find(stype);
		if (type == nullptr)
		{
			Log(LOG_DEBUG) << "redis: pmessage create for nonexistant type " << stype;
			return;
		}

		Serialize::Object *obj = type->Require(id);
		if (obj == nullptr)
		{
			Log(LOG_DEBUG) << "redis: require for pmessage create type " << type->GetName() << " id #" << id << " returned nullptr";
			return;
		}
	}
	else if (command == "delete")
	{
		Anope::string sid;

		sep.GetToken(sid);

		Serialize::ID id;
		try
		{
			id = convertTo<Serialize::ID>(sid);
		}
		catch (const ConvertException &ex)
		{
			Log(LOG_DEBUG) << "redis: unable to get id for SL update key " << sid;
			return;
		}

		Serialize::Object *obj = Serialize::GetID(id);
		if (obj == nullptr)
		{
			Log(LOG_DEBUG) << "redis: message for unknown object #" << id;
			return;
		}

		obj->Delete();
	}
	else
		Log(LOG_DEBUG) << "redis: unknown message: " << message;
}
Esempio n. 4
0
void FieldLoader::OnResult(const Reply &r)
{
	Log(LOG_DEBUG_2) << "redis: Setting field " << field->serialize_name << " of object #" << obj->id << " of type " << obj->GetSerializableType()->GetName() << " to " << r.bulk;
	field->UnserializeFromString(obj, r.bulk);

	delete this;
}