Skip to content

Commit

Permalink
Merge pull request #1255 from fledge-iot/2.3.0RC
Browse files Browse the repository at this point in the history
2.3.0RC
  • Loading branch information
Mohit04tomar authored Jan 9, 2024
2 parents a68efef + f73b9f3 commit ded8e25
Show file tree
Hide file tree
Showing 63 changed files with 1,805 additions and 564 deletions.
74 changes: 73 additions & 1 deletion C/common/config_category.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ string ConfigCategory::getItemAttribute(const string& itemName,
return m_items[i]->m_deprecated;
case RULE_ATTR:
return m_items[i]->m_rule;
case BUCKET_PROPERTIES_ATTR:
return m_items[i]->m_bucketProperties;
default:
throw new ConfigItemAttributeNotFound();
}
Expand Down Expand Up @@ -541,6 +543,9 @@ bool ConfigCategory::setItemAttribute(const string& itemName,
case RULE_ATTR:
m_items[i]->m_rule = value;
return true;
case BUCKET_PROPERTIES_ATTR:
m_items[i]->m_bucketProperties = value;
return true;
default:
return false;
}
Expand Down Expand Up @@ -1038,6 +1043,10 @@ ConfigCategory::CategoryItem::CategoryItem(const string& name,
{
m_itemType = CodeItem;
}
if (m_type.compare("bucket") == 0)
{
m_itemType = BucketItem;
}

if (item.HasMember("deprecated"))
{
Expand Down Expand Up @@ -1083,6 +1092,33 @@ ConfigCategory::CategoryItem::CategoryItem(const string& name,
m_rule = "";
}

if (item.HasMember("properties"))
{
Logger::getLogger()->debug("item['properties'].IsString()=%s, item['properties'].IsObject()=%s",
item["properties"].IsString()?"true":"false",
item["properties"].IsObject()?"true":"false");

rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
item["properties"].Accept(writer);
m_bucketProperties = item["properties"].IsObject() ?
// use current string
strbuf.GetString() :
// Unescape the string
JSONunescape(strbuf.GetString());

Logger::getLogger()->debug("m_bucketProperties=%s", m_bucketProperties.c_str());
}
else
{
m_bucketProperties = "";
}

if (m_itemType == BucketItem && m_bucketProperties.empty())
{
throw new runtime_error("Bucket configuration item is missing the \"properties\" attribute");
}

if (item.HasMember("options"))
{
const Value& options = item["options"];
Expand All @@ -1095,7 +1131,7 @@ ConfigCategory::CategoryItem::CategoryItem(const string& name,
}
}

std:string m_typeUpperCase = m_type;
std::string m_typeUpperCase = m_type;
for (auto & c: m_typeUpperCase) c = toupper(c);

// Item "value" can be an escaped JSON string, so check m_type JSON as well
Expand Down Expand Up @@ -1377,6 +1413,7 @@ ConfigCategory::CategoryItem::CategoryItem(const CategoryItem& rhs)
m_validity = rhs.m_validity;
m_group = rhs.m_group;
m_rule = rhs.m_rule;
m_bucketProperties = rhs.m_bucketProperties;
}

/**
Expand Down Expand Up @@ -1467,6 +1504,11 @@ ostringstream convert;
convert << ", \"rule\" : \"" << JSONescape(m_rule) << "\"";
}

if (!m_bucketProperties.empty())
{
convert << ", \"properties\" : " << m_bucketProperties;
}

if (!m_group.empty())
{
convert << ", \"group\" : \"" << m_group << "\"";
Expand Down Expand Up @@ -1538,6 +1580,11 @@ ostringstream convert;
convert << ", \"rule\" : \"" << JSONescape(m_rule) << "\"";
}

if (!m_bucketProperties.empty())
{
convert << ", \"properties\" : " << m_bucketProperties;
}

if (!m_group.empty())
{
convert << ", \"group\" : \"" << m_group << "\"";
Expand Down Expand Up @@ -1587,6 +1634,31 @@ ostringstream convert;
return convert.str();
}

/**
* Parse BucketItem value in JSON dict format and return the key value pairs within that
*
* @param json JSON string representing the BucketItem value
* @return Vector with pairs of found key/value string pairs in BucketItem value
*/
vector<pair<string,string>>* ConfigCategory::parseBucketItemValue(const string & json)
{
Document document;
if (document.Parse(json.c_str()).HasParseError())
{
Logger::getLogger()->error("parseBucketItemValue(): The provided JSON string has a parse error: %s",
GetParseError_En(document.GetParseError()));
return NULL;
}

vector<pair<string,string>> *vec = new vector<pair<string,string>>;

for (const auto & m : document.GetObject())
vec->emplace_back(make_pair<string,string>(m.name.GetString(), m.value.GetString()));

return vec;
}


// DefaultConfigCategory constructor
DefaultConfigCategory::DefaultConfigCategory(const string& name, const string& json) :
ConfigCategory::ConfigCategory(name, json)
Expand Down
10 changes: 8 additions & 2 deletions C/common/include/config_category.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class ConfigCategory {
DoubleItem,
ScriptItem,
CategoryType,
CodeItem
CodeItem,
BucketItem
};

ConfigCategory(const std::string& name, const std::string& json);
Expand Down Expand Up @@ -129,13 +130,17 @@ class ConfigCategory {
GROUP_ATTR,
DISPLAY_NAME_ATTR,
DEPRECATED_ATTR,
RULE_ATTR};
RULE_ATTR,
BUCKET_PROPERTIES_ATTR
};
std::string getItemAttribute(const std::string& itemName,
ItemAttribute itemAttribute) const;

bool setItemAttribute(const std::string& itemName,
ItemAttribute itemAttribute, const std::string& value);

std::vector<std::pair<std::string,std::string>>* parseBucketItemValue(const std::string &);

protected:
class CategoryItem {
public:
Expand Down Expand Up @@ -174,6 +179,7 @@ class ConfigCategory {
std::string m_validity;
std::string m_group;
std::string m_rule;
std::string m_bucketProperties;
};
std::vector<CategoryItem *> m_items;
std::string m_name;
Expand Down
4 changes: 4 additions & 0 deletions C/common/include/service_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class ServiceRecord : public JSONProvider {
{
m_protocol = protocol;
}
const std::string& getProtocol() const
{
return m_protocol;
}
void setManagementPort(const unsigned short managementPort)
{
m_managementPort = managementPort;
Expand Down
1 change: 1 addition & 0 deletions C/common/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static char ident[80];
}
openlog(ident, LOG_PID|LOG_CONS, LOG_USER);
instance = this;
m_level = LOG_WARNING;
}

Logger::~Logger()
Expand Down
2 changes: 1 addition & 1 deletion C/common/reading_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ JSONReading::JSONReading(const Value& json)
{
if (json.HasMember("id"))
{
m_id = json["id"].GetUint();
m_id = json["id"].GetUint64();
m_has_id = true;
}
else
Expand Down
15 changes: 12 additions & 3 deletions C/common/result_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ ResultSet::ResultSet(const std::string& json)
switch (m_columns[colNo]->getType())
{
case STRING_COLUMN:
rowValue->append(new ColumnValue(string(item->value.GetString())));
if (item->value.IsBool())
{
rowValue->append(new ColumnValue(item->value.IsTrue() ? "true" : "false"));
}
else
{
rowValue->append(new ColumnValue(string(item->value.GetString())));
}
break;
case INT_COLUMN:
rowValue->append(new ColumnValue((long)(item->value.GetInt64())));
Expand All @@ -104,8 +111,10 @@ ResultSet::ResultSet(const std::string& json)
rowValue->append(new ColumnValue(item->value));
break;
case BOOL_COLUMN:
// TODO Add support
rowValue->append(new ColumnValue(string("TODO")));
if (item->value.IsString())
rowValue->append(new ColumnValue(string(item->value.GetString())));
else
rowValue->append(new ColumnValue(item->value.IsTrue() ? "true" : "false"));
break;
}
colNo++;
Expand Down
26 changes: 26 additions & 0 deletions C/plugins/common/libcurl_https.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@

using namespace std;

/**
* Creates a UTC time string for the current time
*
* @return Current UTC time
*/
static std::string CurrentTimeString()
{
time_t now = time(NULL);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
char timeString[20];
strftime(timeString, sizeof(timeString), "%F %T", &timeinfo);
return std::string(timeString);
}

/**
* Constructor: host:port, connect_timeout, request_timeout,
* retry_sleep_Time, max_retry
Expand Down Expand Up @@ -312,6 +327,7 @@ int LibcurlHttps::sendRequest(

do
{
std::chrono::high_resolution_clock::time_point tStart;
try
{
exceptionRaised = none;
Expand All @@ -334,6 +350,7 @@ int LibcurlHttps::sendRequest(
}
m_ofs << "Payload:" << endl;
m_ofs << payload << endl;
tStart = std::chrono::high_resolution_clock::now();
}

// Execute the HTTP method
Expand All @@ -346,8 +363,10 @@ int LibcurlHttps::sendRequest(
httpResponseText = httpHeaderBuffer;
if (m_log)
{
std::chrono::high_resolution_clock::time_point tEnd = std::chrono::high_resolution_clock::now();
m_ofs << "Response:" << endl;
m_ofs << " Code: " << httpCode << endl;
m_ofs << " Time: " << ((double)std::chrono::duration_cast<std::chrono::microseconds>(tEnd - tStart).count()) / 1.0E6 << " sec " << CurrentTimeString() << endl;
m_ofs << " Content: " << httpResponseText << endl << endl;
}
StringStripCRLF(httpResponseText);
Expand Down Expand Up @@ -409,6 +428,13 @@ int LibcurlHttps::sendRequest(
}
#endif

if (m_log && !errorMessage.empty())
{
std::chrono::high_resolution_clock::time_point tEnd = std::chrono::high_resolution_clock::now();
m_ofs << " Time: " << ((double)std::chrono::duration_cast<std::chrono::microseconds>(tEnd - tStart).count()) / 1.0E6 << " sec " << CurrentTimeString() << endl;
m_ofs << " Exception: " << errorMessage << endl;
}

if (retryCount < m_max_retry)
{
this_thread::sleep_for(chrono::seconds(sleepTime));
Expand Down
26 changes: 26 additions & 0 deletions C/plugins/common/simple_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@

using namespace std;

/**
* Creates a UTC time string for the current time
*
* @return Current UTC time
*/
static std::string CurrentTimeString()
{
time_t now = time(NULL);
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
char timeString[20];
strftime(timeString, sizeof(timeString), "%F %T", &timeinfo);
return std::string(timeString);
}

// Using https:/eidheim/Simple-Web-Server
using HttpClient = SimpleWeb::Client<SimpleWeb::HTTP>;

Expand Down Expand Up @@ -126,6 +141,7 @@ int SimpleHttp::sendRequest(

do
{
std::chrono::high_resolution_clock::time_point tStart;
try
{
exception_raised = none;
Expand All @@ -141,6 +157,7 @@ int SimpleHttp::sendRequest(
}
m_ofs << "Payload:" << endl;
m_ofs << payload << endl;
tStart = std::chrono::high_resolution_clock::now();
}

// Call HTTPS method
Expand All @@ -151,8 +168,10 @@ int SimpleHttp::sendRequest(

if (m_log)
{
std::chrono::high_resolution_clock::time_point tEnd = std::chrono::high_resolution_clock::now();
m_ofs << "Response:" << endl;
m_ofs << " Code: " << res->status_code << endl;
m_ofs << " Time: " << ((double)std::chrono::duration_cast<std::chrono::microseconds>(tEnd - tStart).count()) / 1.0E6 << " sec " << CurrentTimeString() << endl;
m_ofs << " Content: " << res->content.string() << endl << endl;
}

Expand Down Expand Up @@ -213,6 +232,13 @@ int SimpleHttp::sendRequest(
}
#endif

if (m_log && !exception_message.empty())
{
std::chrono::high_resolution_clock::time_point tEnd = std::chrono::high_resolution_clock::now();
m_ofs << " Time: " << ((double)std::chrono::duration_cast<std::chrono::microseconds>(tEnd - tStart).count()) / 1.0E6 << " sec " << CurrentTimeString() << endl;
m_ofs << " Exception: " << exception_message << endl;
}

if (retry_count < m_max_retry)
{
this_thread::sleep_for(chrono::seconds(sleep_time));
Expand Down
Loading

0 comments on commit ded8e25

Please sign in to comment.