Refactor parse plain serial #4715

This commit is contained in:
Jerry (Xinyu Hou) 2015-10-23 11:35:28 -07:00
parent 0429986470
commit 978c97cbc0
1 changed files with 41 additions and 47 deletions

View File

@ -118,63 +118,57 @@ void
SubscriptionManager::parsePlainSerial(const String& plainText, SubscriptionKey& key) SubscriptionManager::parsePlainSerial(const String& plainText, SubscriptionKey& key)
{ {
String serial; String serial;
bool parity = false;
String parityStart = plainText.substr(0, 1); String parityStart = plainText.substr(0, 1);
String parityEnd = plainText.substr(plainText.length() - 1, 1); String parityEnd = plainText.substr(plainText.length() - 1, 1);
// check for parity chars { and }, record parity result, then remove them. // check for parity chars { and }, record parity result, then remove them.
if (parityStart == "{" && parityEnd == "}") { if (parityStart == "{" && parityEnd == "}") {
parity = true;
serial = plainText.substr(1, plainText.length() - 2); serial = plainText.substr(1, plainText.length() - 2);
}
else {
serial = plainText;
}
// tokenize serialised subscription. // tokenize serialised subscription.
std::vector<String> parts; std::vector<String> parts;
std::string::size_type pos = 0; std::string::size_type pos = 0;
bool look = true; bool look = true;
while (look) { while (look) {
std::string::size_type start = pos; std::string::size_type start = pos;
pos = serial.find(";", pos); pos = serial.find(";", pos);
if (pos == String::npos) { if (pos == String::npos) {
pos = plainText.length(); pos = plainText.length();
look = false; look = false;
} }
parts.push_back(serial.substr(start, pos - start)); parts.push_back(serial.substr(start, pos - start));
pos += 1; pos += 1;
}
// e.g.: {v1;trial;Bob;1;1398297600;1398384000}
if (parity
&& (parts.size() == 6)
&& (parts.at(0).find("v1") != String::npos)) {
key.m_type = parts.at(1);
key.m_name = parts.at(2);
sscanf(parts.at(3).c_str(), "%d", &key.m_userLimit);
sscanf(parts.at(4).c_str(), "%d", &key.m_warnTime);
sscanf(parts.at(5).c_str(), "%d", &key.m_expireTime);
// TODO: use Arch time
if (time(0) > key.m_expireTime) {
throw XSubscription(synergy::string::sprintf(
"%s subscription has expired",
key.m_type.c_str()));
}
else if (time(0) > key.m_warnTime) {
LOG((CLOG_WARN "%s subscription will expire soon",
key.m_type.c_str()));
} }
const char* userText = (key.m_userLimit == 1) ? "user" : "users"; // e.g.: {v1;trial;Bob;1;1398297600;1398384000}
LOG((CLOG_INFO "%s subscription valid is for %d %s, registered to %s", if ((parts.size() == 6)
key.m_type.c_str(), && (parts.at(0).find("v1") != String::npos)) {
key.m_userLimit, key.m_type = parts.at(1);
userText, key.m_name = parts.at(2);
key.m_name.c_str())); sscanf(parts.at(3).c_str(), "%d", &key.m_userLimit);
sscanf(parts.at(4).c_str(), "%d", &key.m_warnTime);
sscanf(parts.at(5).c_str(), "%d", &key.m_expireTime);
return; // TODO: use Arch time
if (time(0) > key.m_expireTime) {
throw XSubscription(synergy::string::sprintf(
"%s subscription has expired",
key.m_type.c_str()));
}
else if (time(0) > key.m_warnTime) {
LOG((CLOG_WARN "%s subscription will expire soon",
key.m_type.c_str()));
}
const char* userText = (key.m_userLimit == 1) ? "user" : "users";
LOG((CLOG_INFO "%s subscription valid is for %d %s, registered to %s",
key.m_type.c_str(),
key.m_userLimit,
userText,
key.m_name.c_str()));
return;
}
} }
throw XSubscription(synergy::string::sprintf("Serial is invalid.")); throw XSubscription(synergy::string::sprintf("Serial is invalid."));