You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

312 lines
8.0 KiB

#include "mojoDBConnector.h"
#define LOG(X) m_pLog->Log("MDBC", this, X)
#ifdef DEBUG
#define LOG_ERROR(X) \
{ \
char _tBfrErrMSG2w34[256]; \
sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
m_pLog->Log("MDBC", this, _LOG_ERROR, _tBfrErrMSG2w34); \
}
#else
#define LOG_ERROR(X) m_pLog->Log("MDBC", this, _LOG_ERROR, X);
#endif
mojoDBC * mojoDBC::_instance = NULL;
/**
* Returns the single instance of the object.
*/
mojoDBC * mojoDBC::getInstance(config_setting_t * Settings)
{
// check if the instance has been created yet
if (0 == _instance)
{
// if not, then create it
_instance = new mojoDBC(Settings);
};
// return the single instance
return _instance;
}
mojoDBC::mojoDBC(config_setting_t * Settings)
{
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "DATABASE");
m_bIsConnected = false;
m_bIsServerLocal = true;
m_pDataStorage = NULL;
m_pDataMemory = NULL;
}
mojoDBC::~mojoDBC()
{
if (m_pDataStorage)
{
delete(m_pDataStorage);
}
if (m_pDataMemory)
{
delete(m_pDataMemory);
}
_instance = NULL;
}
bool mojoDBC::connect(void)
{
if (m_pSettings == NULL)
{
return false;
}
bool bRecreateMemStructures = false;
config_setting_t * pSetting;
const char * tBuff;
// Disconnect if Connected
m_bIsConnected = false;
char web_access_user[64];
char web_access_group[64];
// Apply New Config
#ifdef DEBUG
LOG("Reading DB Settings ... ");
#endif
if (! config_setting_lookup_string(m_pSettings, "DATA_FILE_NAME", (const char **) &tBuff))
{
pSetting = config_setting_add(m_pSettings, "DATA_FILE_NAME", CONFIG_TYPE_STRING);
sprintf(m_pDbFile, "data/mysqlitedb.db");
config_setting_set_string(pSetting, m_pDbFile);
basicSettingsProvider::getInstance()->compromise();
LOG("Data Storage not set - using default");
}
else
{
sprintf(m_pDbFile, tBuff);
};
if (! config_setting_lookup_string(m_pSettings, "WEB_ACCESS_USER", (const char **) &tBuff))
{
pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_USER", CONFIG_TYPE_STRING);
sprintf(web_access_user, "www-data");
config_setting_set_string(pSetting, web_access_user);
basicSettingsProvider::getInstance()->compromise();
LOG("Web Access User not set - using default");
}
else
{
sprintf(web_access_user, tBuff);
};
if (! config_setting_lookup_string(m_pSettings, "WEB_ACCESS_GROUP", (const char **) &tBuff))
{
pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_GROUP", CONFIG_TYPE_STRING);
sprintf(web_access_group, "www-data");
config_setting_set_string(pSetting, web_access_group);
basicSettingsProvider::getInstance()->compromise();
LOG("Web Access Group not set - using default");
}
else
{
sprintf(web_access_group, tBuff);
};
if (! fileExist(DB_SHMEM_POINT))
{
bRecreateMemStructures = true;
mkdir(DB_SHMEM_POINT, 0775); // S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
chown(DB_SHMEM_POINT, web_access_user, web_access_group); // To make Accessed from PHP
chmod(DB_SHMEM_POINT, 0775);
};
if (! config_setting_lookup_string(m_pSettings, "DATA_MEMORY_NAME", (const char **) &tBuff))
{
pSetting = config_setting_add(m_pSettings, "DATA_MEMORY_NAME", CONFIG_TYPE_STRING);
sprintf(m_pDbMemory, "mem_db.db");
config_setting_set_string(pSetting, m_pDbMemory);
basicSettingsProvider::getInstance()->compromise();
LOG("Memory Storage not set - using default");
};
sprintf(m_pDbMemory, "%s/%s", DB_SHMEM_POINT, tBuff);
basicSettingsProvider::getInstance()->validate();
// Kill Connection if any
if (m_pDataStorage)
{
delete(m_pDataStorage);
}
if (m_pDataMemory)
{
delete(m_pDataMemory);
}
#ifdef DEBUG
LOG(m_pDbFile);
#endif
// Create New Connections
m_pDataStorage = new SQLite_Connector();
if (m_pDataStorage)
{
if (m_pDataStorage->Connect(m_pDbFile, 0, true))
{
#ifdef DEBUG
LOG("DB File Opened");
#endif
chown(m_pDbFile, web_access_user, web_access_group);
chmod(m_pDbFile, 0775);
}
else
{
LOG_ERROR("Cannot Connect to DB File");
delete(m_pDataStorage);
m_pDataStorage = NULL;
LOG("Storage Deleted!");
return false;
};
};
#ifdef DEBUG
LOG(m_pDbMemory);
#endif
if (! fileExist(m_pDbMemory))
{
bRecreateMemStructures = true;
};
m_pDataMemory = new SQLite_Connector();
if (m_pDataMemory)
{
if (m_pDataMemory->Connect(m_pDbMemory, 0, true))
{
#ifdef DEBUG
LOG("DB Memory Opened");
#endif
chown(m_pDbMemory, web_access_user, web_access_group);
chmod(m_pDbMemory, 0775);
if (bRecreateMemStructures)
{
initMemDB();
}
}
else
{
LOG_ERROR("Cannot Connect to DB Memory");
delete(m_pDataStorage);
delete(m_pDataMemory);
m_pDataStorage = NULL;
m_pDataMemory = NULL;
return false;
};
};
m_bIsConnected = true;
return m_bIsConnected;
}
void mojoDBC::initMemDB(void)
{
m_pDataMemory->Query(
"CREATE TABLE \"nodes_qcq\" ( \
\"id\" INTEGER NOT NULL,\
\"node_id\" INTEGER NOT NULL,\
\"cmd\" INTEGER NOT NULL,\
\"status\" INTEGER DEFAULT 0,\
\"parameter\" TEXT,\
PRIMARY KEY(\"id\" AUTOINCREMENT)\
);");
m_pDataMemory->Query(
"CREATE TABLE \"servers\" (\
\"id\" INTEGER NOT NULL,\
\"online\" INTEGER DEFAULT 0,\
\"ts\" INTEGER DEFAULT 0,\
\"version_string\" TEXT,\
\"identity\" TEXT,\
\"pid\" INTEGER DEFAULT 0,\
\"cpu_cores\" INTEGER DEFAULT 0.0,\
\"stat_cpu1\" REAL DEFAULT 0.0,\
\"stat_cpu5\" REAL DEFAULT 0.0,\
\"stat_cpu15\" REAL DEFAULT 0.0,\
\"stat_mem\" REAL DEFAULT 0.0,\
\"stat_disk\" REAL DEFAULT 0.0,\
\"val_mem_tottal\" INTEGER DEFAULT 0,\
\"val_mem_current\" INTEGER DEFAULT 0,\
\"val_disk_tottal\" INTEGER DEFAULT 0,\
\"val_disk_current\" INTEGER DEFAULT 0,\
\"temp0\" REAL DEFAULT 0.0,\
PRIMARY KEY(\"id\" AUTOINCREMENT)\
);");
}
DBC_PUSHED_CMD * mojoDBC::getNodePushedCMD(uint32_t NodeID)
{
DBC_PUSHED_CMD * RetVal = NULL;
if (m_bIsConnected)
{
char QS[MAX_PATH];
AAssoc * result;
sprintf(QS, "select * FROM `nodes_qcq` WHERE `node_id`=%d AND `status`=0 ORDER BY `id` LIMIT 1", NodeID);
result = m_pDataMemory->Query(QS);
if (result)
{
if (result->size() > 0)
{
RetVal = (DBC_PUSHED_CMD *) malloc(sizeof(DBC_PUSHED_CMD));
if (RetVal)
{
Assoc * cArr;
Assoc::iterator it;
memset(RetVal, 0, sizeof(DBC_PUSHED_CMD));
cArr = result->at(0);
it = cArr->find("id");
if (it != cArr->end())
{
RetVal->id = atoi(it->second.c_str());
};
it = cArr->find("cmd");
if (it != cArr->end())
{
RetVal->CMD = atoi(it->second.c_str());
};
it = cArr->find("parameter");
if (it != cArr->end())
{
strncpy(RetVal->PARAM, it->second.c_str(), 256);
RetVal->PARAM[255] = (char) 0;
};
};
};
};
m_pDataMemory->deleteResult(result);
};
return RetVal;
}
bool mojoDBC::updateNodePushedCMD(uint32_t CmdID, uint8_t status)
{
if (m_bIsConnected)
{
char QS[MAX_PATH];
sprintf(QS, "UPDATE `nodes_qcq` SET `status`=%d WHERE `id`=%d ", status, CmdID);
return m_pDataMemory->SilentQuery(QS);
};
return false;
}
bool mojoDBC::cleanNodePushedCMD(void)
{
if (m_bIsConnected)
{
return m_pDataMemory->SilentQuery("DELETE FROM `nodes_qcq` WHERE `status` > 200");
};
return false;
}
/********************************************************************
* *
* Slow DB Functions *
* *
********************************************************************/
DBC_NET_IF_DATA * mojoDBC::getNIF_Data(uint32_t ID) {}
uint32_t mojoDBC::getNIF_ID(CSTR key) {}
uint32_t mojoDBC::getNIF_IDbyDevice(CSTR device) {}
#undef LOG
#undef LOG_ERROR