예제 #1
0
boost::shared_ptr<Requirement> PowerdProxy::InstantiateRequirement(
	boost::shared_ptr<Activity> activity, const std::string& name,
	const MojObject& value)
{
	LOG_TRACE("Entering function %s", __FUNCTION__);
	LOG_DEBUG("Instantiating [Requirement %s] for [Activity %llu]",
		name.c_str(), activity->GetId());

	if (name == "charging") {
		if ((value.type() != MojObject::TypeBool) || !value.boolValue()) {
			throw std::runtime_error("A \"charging\" requirement must specify "
				"'true' if present");
		}

		boost::shared_ptr<ListedRequirement> req =
			boost::make_shared<BasicCoreListedRequirement>(
				activity, m_chargingRequirementCore,
				m_chargingRequirementCore->IsMet());

		m_chargingRequirements.push_back(*req);
		return req;
	} else if (name == "docked") {
		if ((value.type() != MojObject::TypeBool) || !value.boolValue()) {
			throw std::runtime_error("A \"docked\" requirement must specify "
				"'true' if present");
		}

		boost::shared_ptr<ListedRequirement> req =
			boost::make_shared<BasicCoreListedRequirement>(
				activity, m_dockedRequirementCore,
				m_dockedRequirementCore->IsMet());

		m_dockedRequirements.push_back(*req);
		return req;
	} else if (name == "battery") {
		if ((value.type() != MojObject::TypeInt) ||
			(value.intValue() < 0) || (value.intValue() > 100)) {
			throw std::runtime_error("A \"battery\" requirement must specify "
				"a value between 0 and 100");
		}

		MojInt64 percent = value.intValue();

		boost::shared_ptr<BatteryRequirement> req =
			boost::make_shared<BatteryRequirement>(activity, percent,
				boost::dynamic_pointer_cast<PowerdProxy,
					RequirementManager>(shared_from_this()),
				(m_batteryPercent >= percent));
		m_batteryRequirements.insert(*req);
		return req;
	} else {
		LOG_ERROR(MSGID_UNKNOW_REQUIREMENT,3,
			PMLOGKS("MANAGER",GetName().c_str()),
			PMLOGKS("REQUIREMENT",name.c_str()),
			PMLOGKFV("Activity","%llu",activity->GetId()),
			"does not know how to instantiate ");
		throw std::runtime_error("Attempt to instantiate unknown requirement");
	}
}
예제 #2
0
boost::shared_ptr<Requirement> PowerdProxy::InstantiateRequirement(
	boost::shared_ptr<Activity> activity, const std::string& name,
	const MojObject& value)
{
	MojLogTrace(s_log);
	MojLogInfo(s_log, _T("Instantiating [Requirement %s] for [Activity %llu]"),
		name.c_str(), activity->GetId());

	if (name == "charging") {
		if ((value.type() != MojObject::TypeBool) || !value.boolValue()) {
			throw std::runtime_error("A \"charging\" requirement must specify "
				"'true' if present");
		}

		boost::shared_ptr<ListedRequirement> req =
			boost::make_shared<BasicCoreListedRequirement>(
				activity, m_chargingRequirementCore,
				m_chargingRequirementCore->IsMet());

		m_chargingRequirements.push_back(*req);
		return req;
	} else if (name == "docked") {
		if ((value.type() != MojObject::TypeBool) || !value.boolValue()) {
			throw std::runtime_error("A \"docked\" requirement must specify "
				"'true' if present");
		}

		boost::shared_ptr<ListedRequirement> req =
			boost::make_shared<BasicCoreListedRequirement>(
				activity, m_dockedRequirementCore,
				m_dockedRequirementCore->IsMet());

		m_dockedRequirements.push_back(*req);
		return req;
	} else if (name == "battery") {
		if ((value.type() != MojObject::TypeInt) ||
			(value.intValue() < 0) || (value.intValue() > 100)) {
			throw std::runtime_error("A \"battery\" requirement must specify "
				"a value between 0 and 100");
		}

		MojInt64 percent = value.intValue();

		boost::shared_ptr<BatteryRequirement> req =
			boost::make_shared<BatteryRequirement>(activity, percent,
				boost::dynamic_pointer_cast<PowerdProxy,
					RequirementManager>(shared_from_this()),
				(m_batteryPercent >= percent));
		m_batteryRequirements.insert(*req);
		return req;
	} else {
		MojLogError(s_log, _T("[Manager %s] does not know how to instantiate "
			"[Requirement %s] for [Activity %llu]"), GetName().c_str(),
			name.c_str(), activity->GetId());
		throw std::runtime_error("Attempt to instantiate unknown requirement");
	}
}
예제 #3
0
MojErr MojObjectTest::emptyTest(MojObject& obj)
{
    MojObject obj2;
    MojString str1;
    MojString str2;

    bool found = false;

    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());

    MojTestAssert(obj.begin() == obj.end());
    MojTestAssert(!obj.contains(_T("hello")));
    MojTestAssert(!obj.get(_T("hi"), obj2));
    MojErr err = obj.del(_T("hello"), found);
    MojTestErrCheck(err);

    obj2.assign(obj);
    MojTestAssert(obj2.type() == obj.type());
    MojTestAssert(obj2 == obj);
    MojTestAssert(!(obj2 != obj));

    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    err = obj2.stringValue(str2);
    MojTestErrCheck(err);
    MojTestAssert(str1 == str2);

    return MojErrNone;
}
예제 #4
0
MojErr MojDbKindState::initKindToken(MojDbReq& req)
{
	MojAssertMutexLocked(m_lock);

	// load tokens
	MojObject token;
	bool created = false;
	MojErr err = id(m_kindId, KindTokensKey, req, token, created);
	MojErrCheck(err);
	m_kindToken = token.intValue();

	return MojErrNone;
}
예제 #5
0
MojErr MojDbQuotaEngine::getUsage(const MojString& kindId, MojDbStorageTxn* txn, bool forUpdate, MojInt64& usageOut, MojRefCountedPtr<MojDbStorageItem>& itemOut)
{
	MojAssert(txn);

	usageOut = 0;
	MojErr err = m_usageDb->get(kindId, txn, forUpdate, itemOut);
	MojErrCheck(err);
	if (itemOut.get()) {
		MojObject val;
		err = itemOut->toObject(val, *m_db->kindEngine(), false);
		MojErrCheck(err);
		usageOut = val.intValue();
	}
	return MojErrNone;
}
예제 #6
0
MojErr MojObjectTest::typeTest()
{
    MojObject obj;
    MojObject obj2;
    MojString str1;
    MojString str2;
    MojHashMap<MojObject, MojObject> map;

    // null
    MojTestAssert(obj.type() == MojObject::TypeUndefined);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    MojErr err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("null"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    MojTestAssert(obj == obj2);
    MojTestAssert(obj.compare(obj2) == 0);
    err = obj.coerce(MojObject::TypeNull);
    MojTestErrCheck(err);
    MojTestAssert(obj.type() == MojObject::TypeNull);
    err = obj.coerce(MojObject::TypeString);
    MojTestErrCheck(err);
    MojTestAssert(obj == str1);
    err = obj.coerce(MojObject::TypeBool);
    MojTestErrCheck(err);
    MojTestAssert(obj == true);
    err = obj.coerce(MojObject::TypeInt);
    MojTestErrCheck(err);
    MojTestAssert(obj == 1);
    err = obj.coerce(MojObject::TypeDecimal);
    MojTestErrCheck(err);
    MojTestAssert(obj.type() == MojObject::TypeDecimal && obj == MojDecimal(1, 0));
    err = obj.coerce(MojObject::TypeObject);
    MojTestErrCheck(err);
    MojTestAssert(obj.type() == MojObject::TypeObject);
    err = obj.coerce(MojObject::TypeArray);
    MojTestErrCheck(err);
    MojTestAssert(obj.type() == MojObject::TypeArray);

    // object
    err = obj.put(_T("hello"), 5);
    MojTestErrCheck(err);
    MojTestAssert(obj.type() == MojObject::TypeObject);
    MojTestAssert(obj.size() == 1);
    MojTestAssert(!obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("{\"hello\":5}"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    obj.clear(MojObject::TypeObject);
    MojTestAssert(obj.type() == MojObject::TypeObject);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("{}"));
    // array
    for (int i = 0; i < 1000; ++i) {
        err = obj.push(i);
        MojTestErrCheck(err);
    }
    MojTestAssert(obj.type() == MojObject::TypeArray);
    MojTestAssert(obj.size() == 1000);
    MojTestAssert(!obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    for (int i = 0; i < 1000; ++i) {
        MojTestAssert(obj.at(i, obj2));
        MojTestAssert(obj2 == i);
    }
    MojTestAssert(!obj.at(1000, obj2));
    err = obj.setAt(1001, 1001);
    MojTestErrCheck(err);
    MojTestAssert(obj.size() == 1002);
    MojTestAssert(obj.at(1000, obj2));
    MojTestAssert(obj2.type() == MojObject::TypeUndefined);
    obj.clear(MojObject::TypeArray);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("[]"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    // string
    err = str1.assign(_T("yo"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.type() == MojObject::TypeString);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str2);
    MojTestErrCheck(err);
    MojTestAssert(str1 == str2);
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    obj.clear(MojObject::TypeString);
    MojTestAssert(obj.boolValue() == false);
    err = str1.assign(_T("12345"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == 12345);
    MojTestAssert(obj.decimalValue() == MojDecimal(12345, 0));
    err = str1.assign(_T("-67890"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == -67890);
    MojTestAssert(obj.decimalValue() == MojDecimal(-67890, 0));
    err = str1.assign(_T("12345000000000"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == 12345000000000LL);
    err = str1.assign(_T("12345.6789"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == 12345);
    MojTestAssert(obj.decimalValue() == MojDecimal(12345, 678900));
    err = str1.assign(_T("1.0e3"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == 1);
    MojTestAssert(obj.decimalValue() == MojDecimal(1000, 0));
    err = str1.assign(_T("45hello"));
    MojTestErrCheck(err);
    obj = str1;
    MojTestAssert(obj.intValue() == 45);
    MojTestAssert(obj.decimalValue() == MojDecimal(45, 0));
    // bool
    obj = true;
    MojTestAssert(obj.type() == MojObject::TypeBool);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == 1);
    MojTestAssert(obj.decimalValue() == MojDecimal(1, 0));
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("true"));
    obj.clear(MojObject::TypeBool);
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("false"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    // MojDecimal
    obj = MojDecimal(3, 140000);
    MojTestAssert(obj.type() == MojObject::TypeDecimal);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == 3);
    MojTestAssert(obj.decimalValue() == MojDecimal(3.14));
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("3.14"));
    obj.clear(MojObject::TypeDecimal);
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("0.0"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));
    // MojDecimal
    obj = -987654321;
    MojTestAssert(obj.type() == MojObject::TypeInt);
    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == true);
    MojTestAssert(obj.intValue() == -987654321);
    MojTestAssert(obj.decimalValue() == MojDecimal(-987654321, 0));
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("-987654321"));
    obj.clear(MojObject::TypeInt);
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());
    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    MojTestAssert(str1 == _T("0"));
    err = map.put(obj, obj);
    MojTestErrCheck(err);
    MojTestAssert(map.contains(obj));

    return MojErrNone;
}
예제 #7
0
void EmailAdapter::ParseDatabaseObject(const MojObject& obj, Email& email)
{
	MojErr err;
	
	MojObject folderId;
	err = obj.getRequired(FOLDER_ID, folderId);
	ErrorToException(err);
	email.SetFolderId(folderId);
	
	MojString subject;
	err = obj.getRequired(SUBJECT, subject);
	ErrorToException(err);
	email.SetSubject( std::string(subject) );
	
	MojObject fromAddress;
	err = obj.getRequired(FROM, fromAddress);
	ErrorToException(err);
	email.SetFrom( ParseAddress(fromAddress) );
	
	// Optional replyTo address
	MojObject replyTo;
	if(obj.get(REPLY_TO, replyTo) && !replyTo.null()) {
		email.SetReplyTo( ParseAddress(replyTo) );
	}

	MojInt64 timestamp;
	err = obj.getRequired(TIMESTAMP, timestamp);
	ErrorToException(err);
	email.SetDateReceived(timestamp);
	
	// Parse recipients
	MojObject recipients;
	err = obj.getRequired(RECIPIENTS, recipients);
	ErrorToException(err);
	ParseRecipients(recipients, email);
	
	// Parse flags
	MojObject flags;
	if (obj.get(FLAGS, flags)) {
		ParseFlags(flags, email);
	}

	// Parse parts
	MojObject parts;
	err = obj.getRequired(PARTS, parts);
	ErrorToException(err);

	EmailPartList partList;
	ParseParts(parts, partList);
	email.SetPartList(partList);

	MojObject originalMsgId;
	if (obj.get(ORIGINAL_MSG_ID, originalMsgId)) {
		email.SetOriginalMsgId(originalMsgId);
	}

	MojString draftType;
	bool hasDraftType = false;
	err = obj.get(DRAFT_TYPE, draftType, hasDraftType);
	ErrorToException(err);
	if (hasDraftType) {
		email.SetDraftType( ParseDraftType(draftType) );
	}

	MojString priority;
	bool hasPriority = false;
	err = obj.get(PRIORITY, priority, hasPriority);
	ErrorToException(err);
	// If no priority exists, this will default to normal
	email.SetPriority(ParsePriority(priority));

	email.SetMessageId( DatabaseAdapter::GetOptionalString(obj, MESSAGE_ID) );
	email.SetInReplyTo( DatabaseAdapter::GetOptionalString(obj, IN_REPLY_TO) );

	// SendStatus
	// NOTE: This is not being serialized back to the database in SerializeToDatabaseObject
	MojObject sendStatus;
	if (obj.get(SEND_STATUS, sendStatus)) {
		bool isSent = false;
		if (sendStatus.get(SendStatus::SENT, isSent)) {
			email.SetSent(isSent);
		}
		bool hasFatalError = false;
		if (sendStatus.get(SendStatus::FATAL_ERROR, hasFatalError)) {
			email.SetHasFatalError(hasFatalError);
		}
		MojInt64 retryCount;
		if (sendStatus.get(SendStatus::RETRY_COUNT, retryCount)) {
			email.SetRetryCount(retryCount);
		}
		MojObject sendError;
		if (sendStatus.get(SendStatus::ERROR, sendError)) {
			MojObject errorCode;
			if (sendError.get(SendStatusError::ERROR_CODE, errorCode) && !errorCode.null() && !errorCode.undefined()) {
				email.SetSendError(static_cast<MailError::ErrorCode>(errorCode.intValue()));
			}
		}
	}
}