This is the documentation for the C++ BLUEPIRAT Client library which is compatible with all Microsoft compilers. The library's interface class IBPNGClient uses only base data type parameters like int, long and char, pointers to those types and pointers to complex proprietary data objects that are entirely defined within the library. To access the data of such objects the library comes with own interface definitions for all of those complex data types (like e.g. IConversionSet, see BPNGDefines.h). All library functions are blocking functions. Status and progress information is processed via listener callbacks (see IBPNGClientListener). Errors are processed by the functions' return values (see section Error handling for more details).
Besides that there are several more functions for deleting data, setting the logger's/TSL time and marker, scanning the network for available loggers/TSL, etc.
All errors are processed by the functions' return values. If the return value states an error a call to getLastError() provides details about the error(s) occurred. Warnings are not intended to abort a process. That's why they are reported via the function IBPNGClientListener::onWarning(). It's up to the user to handle them or not.
Progress and status information is also processed via listener callbacks. You have to derive your own class from IBPNGClientListener and implement all functions you need. Register an object of your listener class at the executing IBPNGClient with IBPNGClient::addListener().
The library is build with Microsoft Visual C++ and is linked to the C-Runtime Library with the Multi-threaded resp. Multi-threaded Debug compiler switch (/MT resp. /MTd). The user's project must have the same settings. Applications with mixed runtime library linkage may cause errors that are difficult to diagnose and to handle. The debug version of the library is named with a "_d" suffix.
The "sample" directory contains a demo project for the BLUEPIRAT Client Library.
#include <sys/stat.h>
#include <cerrno>
#include <ctime>
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#ifndef _MSC_VER
#include <fileutils.hh>
#endif
#include "windirent.h"
#include "miniz.h"
#ifndef _MSC_VER
#include <sys/stat.h>
#else
#include <direct.h>
#define mkdir(a, b) _mkdir(a)
#endif
using namespace std;
static void sampleFunctionOfflineConversion();
static void sampleFunctionOfflineTSLConversion();
static string getLocalDateString();
static vector<string> readZipsFromDirectory(string dir);
static bool extractZipToDirectory(string zipPath, string targetPath);
int main()
{
vector<TSLClusterImpl> tsls = detector.
getTSLs(devices);
bool found = false;
bool tslFound = false;
for (size_t i = 0; i < devices.size(); ++i)
{
if (strcmp(devices[i].ip, "192.168.0.233") == 0)
{
device = devices[i];
found = true;
break;
}
}
for (size_t i = 0; i < tsls.size(); ++i)
{
cout <<
"Found TSL [" << tsl.
getTSLName() <<
"] with devices\n";
cout << "\n";
{
targetTsl = tsl;
tslFound = true;
}
}
if (found)
{
}
if (tslFound)
{
}
int ret = system("pause");
if (ret == -1)
cerr << "error in system(\"pause\"): " << strerror(errno) << endl;
}
#ifndef _MSC_VER
static void sampleFunctionDownload(
OnlineLoggerInfo device) __attribute__((unused));
#endif
{
if (ret == 0)
{
cout << "Failed to connect logger. " << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (ret == 0)
{
cout << "Failed to init online." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (eventList.size() == 0)
{
cout << "Empty event list" << endl;
return;
}
uint64_t startupId = 0;
uint64_t endId = -1;
for (int i = eventList.size() - 1; i >= 0; --i)
{
{
startupId = eventList[i].uniqueID;
break;
}
}
vector<DataSpan> spanVec;
spanVec.push_back(span);
ret = mkdir("..\\testoutdir", 0x777);
if (ret != 0 && errno != EEXIST)
{
cout << "Failed to create output directory" << endl;
return;
}
ret = client->
downloadDataSpans(spanVec.size(), &spanVec[0],
"..\\testoutdir\\BP2_Offline.zip", 0);
if (ret == 0)
{
cout << "Failed to dowload data." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
}
#ifndef _MSC_VER
static void sampleFunctionTSLDownload(
TSLClusterImpl tsl) __attribute__((unused));
#endif
{
if (ret == 0)
{
cout << "Failed to connect tsl. " << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (ret == 0)
{
cout << "Failed to init online." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (eventList.size() == 0)
{
cout << "Empty event list" << endl;
return;
}
uint64_t startupId = 0;
uint64_t endId = -1;
for (int i = eventList.size() - 1; i >= 0; --i)
{
{
startupId = eventList[i].uniqueID;
break;
}
}
vector<DataSpan> spanVec;
spanVec.push_back(span);
ret = mkdir("..\\testoutdir", 0x777);
if (ret != 0 && errno != EEXIST)
{
cout << "Failed to create output directory" << endl;
return;
}
ret = client->
downloadDataSpans(spanVec.size(), &spanVec[0],
"..\\testoutdir\\TSL_Offline.zip", 0);
if (ret == 0)
{
cout << "Failed to dowload data." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
}
#ifndef _MSC_VER
static void sampleFunctionOnlineConversion(
OnlineLoggerInfo device) __attribute__((unused));
#endif
{
if (ret == 0)
{
cout << "Failed to connect logger." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (ret == 0)
{
cout << "Failed to init online." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (eventList.size() == 0)
{
cout << "Empty event list" << endl;
return;
}
uint64_t markerTimeStamp = 0;
for (int i = eventList.size() - 1; i >= 0; --i)
{
if (eventList[i].type ==
MARKER)
{
markerTimeStamp = eventList[i].timeStamp;
break;
}
}
if (markerTimeStamp == 0)
{
cout << "No marker found." << endl;
return;
}
ret = mkdir("..\\testoutdir", 0x777);
if (ret != 0 && errno != EEXIST)
{
cout << "Failed to create output directory" << endl;
return;
}
uint64_t startTime = markerTimeStamp - 60 * 1000000;
uint64_t endTime = markerTimeStamp + 60 * 1000000;
for (
int i = 0; i < channels->
getSize(); ++i)
{
continue;
if (index == 0 || index == 1)
{
index,
CANOE,
-1,
}
else if (index == 2 || index == 3)
{
index,
CANOE,
10,
}
else
{
index,
BLF,
20,
}
}
ret = client->
convertData(conversionSet,
"..\\testoutdir");
if (ret == 0)
{
cout << "Failed to convert data." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
}
#ifndef _MSC_VER
static void sampleFunctionTSLOnlineConversion(
TSLClusterImpl tsl) __attribute__((unused));
#endif
{
if (ret == 0)
{
cout << "Failed to connect tsl." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (ret == 0)
{
cout << "Failed to init online." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (eventList.size() == 0)
{
cout << "Empty event list" << endl;
return;
}
uint64_t markerTimeStamp = 0;
for (int i = eventList.size() - 1; i >= 0; --i)
{
if (eventList[i].type ==
MARKER)
{
markerTimeStamp = eventList[i].timeStamp;
break;
}
}
if (markerTimeStamp == 0)
{
cout << "No marker found." << endl;
return;
}
ret = mkdir("..\\testoutdir", 0x777);
if (ret != 0 && errno != EEXIST)
{
cout << "Failed to create output directory" << endl;
return;
}
uint64_t startTime = markerTimeStamp - 60 * 1000000;
uint64_t endTime = markerTimeStamp + 60 * 1000000;
for (
int i = 0; i < channels->
getSize(); ++i)
{
continue;
if (index == 0 || index == 1)
{
index,
CANOE,
-1,
}
else if (index == 2 || index == 3)
{
index,
CANOE,
10,
}
else
{
index,
BLF,
20,
}
}
ret = client->
convertData(conversionSet,
"..\\testoutdir");
if (ret == 0)
{
cout << "Failed to convert data." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
}
#ifndef _MSC_VER
static void sampleFunctionConfiguration(
OnlineLoggerInfo device) __attribute__((unused));
#endif
{
if (ret == 0)
{
cout << "Failed to connect logger." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
stringstream targetPath;
targetPath <<
"..\\testoutdir\\bpng_[" << device.
mbnr <<
"]_[" << getLocalDateString() <<
"].zip";
ret = client->
getConfig(targetPath.str().c_str());
if (ret == 0)
{
cout << "Failed to download configuration." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
string newConfigPath = targetPath.str();
pair.
value = newConfigPath.c_str();
if (ret == 0)
{
cout << "Failed to reconfigurate the logger." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (ret == 0)
{
cout << "Failed to set default config to the logger." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
}
#ifndef _MSC_VER
static void sampleFunctionTSLConfiguration(
TSLClusterImpl tsl) __attribute__((unused));
#endif
{
if (ret == 0)
{
cout << "Failed to connect TSL." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
string targetPath =
"..\\testoutdir\\tsl_[" + tsl.
getTSLName() +
"]_[" + getLocalDateString() +
"]";
string zipPath = targetPath + ".zip";
if (ret == 0)
{
cout << "Failed to download configurations." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
targetPath += "\\";
extractZipToDirectory(zipPath, targetPath);
vector<string> configZips = readZipsFromDirectory(targetPath);
vector<string> paths;
vector<OnlineLoggerInfoStringPair> pairs;
for (vector<OnlineLoggerInfo>::iterator iter = tsl.
begin(); iter != tsl.
end(); ++iter)
{
for (size_t i = 0; i != configZips.size(); ++i)
{
string configZip = configZips[i];
if (configZip.find(device.
mbnr) != string::npos)
{
paths.push_back(targetPath + configZip);
pair.
value = paths.back().c_str();
pairs.push_back(pair);
break;
}
}
}
if (ret == 0)
{
cout << "Failed to reconfigurate the logger." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (ret == 0)
{
cout << "Failed to set default config to the logger." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
}
#ifndef _MSC_VER
static void sampleFunctionOfflineConversion() __attribute__((unused));
#endif
static void sampleFunctionOfflineConversion()
{
if (ret == 0)
{
cout << "Failed to init offline." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
if (eventList.size() == 0)
{
cout << "Empty event list" << endl;
return;
}
uint64_t markerTimeStamp = 0;
for (int i = eventList.size() - 1; i >= 0; --i)
{
if (eventList[i].type ==
MARKER)
{
markerTimeStamp = eventList[i].timeStamp;
break;
}
}
if (markerTimeStamp == 0)
{
cout << "No marker found." << endl;
return;
}
ret = mkdir("..\\testoutdir", 0x777);
if (ret != 0 && errno != EEXIST)
{
cout << "Failed to create output directory" << endl;
return;
}
uint64_t startTime = markerTimeStamp - 60 * 1000000;
uint64_t endTime = markerTimeStamp + 60 * 1000000;
for (
int i = 0; i < channels->
getSize(); ++i)
{
continue;
if (index == 0 || index == 1)
{
index,
CANOE,
-1,
}
else if (index == 2 || index == 3)
{
index,
CANOE,
10,
}
else
{
index,
BLF,
20,
}
}
ret = client->
convertData(conversionSet,
"..\\testoutdir");
if (ret == 0)
{
cout << "Failed to convert data." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
}
#ifndef _MSC_VER
static void sampleFunctionOfflineTSLConversion() __attribute__((unused));
#endif
static void sampleFunctionOfflineTSLConversion()
{
string offlineDataSetPath = "..\\testoutdir\\TSL_Offline.zip";
if (ret == 0)
{
cout << "Failed to init offline." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
#ifdef _MSC_VER
ret = _mkdir("..\\testoutdir");
#else
ret = mkdirPath("..\\testoutdir");
#endif
if (ret != 0 && errno != EEXIST)
{
cout << "Failed to create output directory" << endl;
return;
}
for (
int i = 0; i < channels->
getSize(); ++i)
{
{
XTMT,
-1,
}
}
ret = client->
convertData(conversionSet,
"..\\testoutdir");
if (ret == 0)
{
cout << "Failed to convert data." << endl;
cout <<
"BPNGErrCode: " << err.
code <<
", " << err.
msg << endl;
return;
}
}
static string getLocalDateString()
{
time_t timeObj;
time(&timeObj);
tm *pTime = localtime(&timeObj);
char buffer[100];
sprintf(buffer, "%d-%02d-%02d_%02d-%02d-%02d", pTime->tm_year + 1900, pTime->tm_mon + 1, pTime->tm_mday, pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
return buffer;
}
static vector<string> readZipsFromDirectory(string dir)
{
vector<string> output;
DIR* directory = nullptr;
directory = opendir(dir.c_str());
struct dirent* entry = readdir(directory);
string file;
while (entry != nullptr)
{
file = entry->d_name;
if (file.find(".zip") != string::npos)
{
output.push_back(file);
}
entry = readdir(directory);
}
closedir(directory);
return output;
}
static bool extractZipToDirectory(string zipPath, string destination)
{
mz_zip_archive zip_archive;
memset(&zip_archive, 0, sizeof(zip_archive));
mz_bool status = mz_zip_reader_init_file(&zip_archive, zipPath.c_str(), 0);
if (!status)
{
cout << "mz_zip_reader_init_file() failed!" << endl;
return false;
}
#ifdef _MSV_VER
mkdir(destination.c_str());
#else
mkdir(destination.c_str(), 0777);
#endif
for (size_t i = 0; i < (int)mz_zip_reader_get_num_files(&zip_archive); i++)
{
mz_zip_archive_file_stat file_stat;
if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat))
{
cout << "mz_zip_reader_file_stat() failed!\n" << endl;
mz_zip_reader_end(&zip_archive);
return false;
}
size_t uncomp_size;
void* p = mz_zip_reader_extract_file_to_heap(&zip_archive, file_stat.m_filename, &uncomp_size, 0);
if (!p)
{
printf("mz_zip_reader_extract_file_to_heap() failed!\n");
mz_zip_reader_end(&zip_archive);
return false;
}
ofstream outFile;
string fn = destination + file_stat.m_filename;
outFile.open(fn.c_str(), ios::binary);
if (!outFile.is_open())
{
printf("Failed to write extracted file.\n");
mz_zip_reader_end(&zip_archive);
return false;
}
outFile.write((const char*)p, uncomp_size);
outFile.close();
}
mz_zip_reader_end(&zip_archive);
}