Skip to content

Commit

Permalink
supports file download with redirect, json parse, last version downlo…
Browse files Browse the repository at this point in the history
…ad, version comparison, md5 calculation
  • Loading branch information
zimbora committed May 10, 2024
1 parent 85f632d commit 4e97ca4
Show file tree
Hide file tree
Showing 9 changed files with 479 additions and 38 deletions.
4 changes: 3 additions & 1 deletion src/update/Dockerfile.linux-x64
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM decenomy/dsw-linux-x64-builder@sha256:49894f78ac27318ed1c018c80e674f99e6b45ac382c9b98a2aa0c94d8bd755a4
#FROM decenomy/dsw-linux-x64-builder@sha256:49894f78ac27318ed1c018c80e674f99e6b45ac382c9b98a2aa0c94d8bd755a4
FROM decenomy/local-linux-x64-builder:latest

# Set up a development tools directory
WORKDIR /home/app

COPY . /home/app

RUN make clean
RUN make docker_linux_test

RUN export PATH="/wrapped:$PATH"
Empty file added src/update/app
Empty file.
47 changes: 47 additions & 0 deletions src/update/httpclient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "httpclient.h"

HTTPClient::HTTPClient() {
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (!curl) {
std::cerr << "Error: Failed to initialize libcurl." << std::endl;
}
}

HTTPClient::~HTTPClient() {
if (curl) {
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}

std::string HTTPClient::get(const std::string& url) {

std::cout << "get info from: " << url << std::endl;
std::string response;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); // Verify the peer's SSL certificate
curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

// Set User-Agent header
curl_version_info_data *info = curl_version_info(CURLVERSION_NOW);

if (info) {
curl_easy_setopt(curl, CURLOPT_USERAGENT, info->version);
}

CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "Error: Failed to perform HTTP request: " << curl_easy_strerror(res) << std::endl;
}
}
return response;
}

size_t HTTPClient::writeCallback(void* ptr, size_t size, size_t nmemb, std::string* data) {
data->append((char*)ptr, size * nmemb);
return size * nmemb;
}
19 changes: 19 additions & 0 deletions src/update/httpclient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H

#include <string>
#include <curl/curl.h>
#include <iostream>

class HTTPClient {
public:
HTTPClient();
~HTTPClient();
std::string get(const std::string& url);

private:
CURL* curl;
static size_t writeCallback(void* ptr, size_t size, size_t nmemb, std::string* data);
};

#endif // HTTPCLIENT_H
1 change: 0 additions & 1 deletion src/update/linux-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ docker cp "$container_id":home/app/test ./test
# Remove the temporary container
docker rm "$container_id"


# To run the container use:
#docker run \
# -it \
Expand Down
8 changes: 4 additions & 4 deletions src/update/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ docker_linux_test:
-I/DSW/depends/x86_64-pc-linux-gnu/include \
-L/DSW/depends/x86_64-pc-linux-gnu/lib \
-L/DSW/depends/x86_64-pc-linux-gnu/lib64 \
-o test test.cpp logging.h tinyformat.h update.h update.cpp minizip/unzip.h minizip/unzip.c minizip/ioapi.h minizip/ioapi.c \
-lcurl -pthread -lz -lssl -lcrypto -ldl -lboost_system-mt-x64 -lboost_filesystem-mt-x64
-o test test.cpp logging.h tinyformat.h update.h update.cpp httpclient.h httpclient.cpp minizip/unzip.h minizip/unzip.c minizip/ioapi.h minizip/ioapi.c \
-lcurl -pthread -lz -lssl -lcrypto -ldl -lboost_system-mt-x64 -lboost_filesystem-mt-x64 -lboost_json-mt-x64

docker_linux_app:
$(CXX) -DUPDATETEST -DNEWVERSION="$(VERSION)" \
-I/DSW/depends/x86_64-pc-linux-gnu/include \
-L/DSW/depends/x86_64-pc-linux-gnu/lib \
-L/DSW/depends/x86_64-pc-linux-gnu/lib64 \
-o app test.cpp logging.h tinyformat.h update.h update.cpp minizip/unzip.h minizip/unzip.c minizip/ioapi.h minizip/ioapi.c \
-lcurl -pthread -lz -lssl -lcrypto -ldl -lboost_system-mt-x64 -lboost_filesystem-mt-x64
-lcurl -pthread -lz -lssl -lcrypto -ldl -lboost_system-mt-x64 -lboost_filesystem-mt-x64 -lboost_json-mt-x64

docker_windows_test:
/usr/bin/x86_64-w64-mingw32-g++ -DUPDATETEST -static \
Expand All @@ -91,4 +91,4 @@ docker_windows_app:


clean:
rm test app test.exe
rm test
56 changes: 48 additions & 8 deletions src/update/test.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
#define COIN "777"

#include <boost/json.hpp>
#include "update.h"
//#include <boost/json/src.hpp>
#include "httpclient.h"
//#include "clientversion.h"

#define CLIENT_VERSION_MAJOR 1
#define CLIENT_VERSION_MINOR 0
#define CLIENT_VERSION_REVISION 2
#define CLIENT_VERSION_BUILD 0

namespace fs = boost::filesystem;
namespace json = boost::json;

// get it from clientversion.h
static const int CLIENT_VERSION =
1000000 * CLIENT_VERSION_MAJOR ///
+ 10000 * CLIENT_VERSION_MINOR ///
+ 100 * CLIENT_VERSION_REVISION ///
+ 1 * CLIENT_VERSION_BUILD;

// Define the progress callback function
static int downloadProgressCallback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) {
static int DownloadProgressCallback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) {

// Calculate progress percentage
double progress = (dlnow > 0) ? (dlnow / dltotal) * 100.0 : 0.0;
Expand All @@ -29,6 +45,30 @@ int main() {

std::cout << "Let's update our app" << std::endl;

HTTPClient client;
Update update;

const std::string url = std::string(UPDATE_URL)+std::string(TICKER)+"/releases/latest";
std::string response = client.get(url);

json::value jv = json::parse(response);
update.ParseVersionRequest(jv);
int version = update.GetRemoteVersion();
std::cout << "remote version: " << version << "\n";

if(version > CLIENT_VERSION){
std::cout << "update app" << std::endl;
update.GetLatestVersion();
}else{
std::cout << "current version is the most recent" << std::endl;
}
//std::cout << "Parsed JSON: " << jv << std::endl;
//std::cout << "Serialized JSON: " << boost::json::serialize(jv) << std::endl;

//std::cout << "Response from " << url << ":\n" << response << std::endl;
return 0;


/*
std::cout << "current version is: " << std::string(VERSION) << std::endl;
const char* currentApp = "test";
Expand All @@ -48,17 +88,17 @@ int main() {
while(1);
*/

/*
const std::string url = std::string(UPDATE_URL)+std::string(TICKER)+"/releases/latest";
const std::string outputFileName = "bootstrap.zip";
const std::string extractPath = "bootstrap_";
std::cout << "Download: " << url << std::endl;
if(Bootstrap::isDirectory(extractPath))
Bootstrap::rmDirectory(extractPath);
if(Bootstrap::IsDirectory(extractPath))
Bootstrap::RemoveDirectory(extractPath);
if (Bootstrap::DownloadFile(url, outputFileName, downloadProgressCallback)) {
if (Bootstrap::DownloadFile(url, outputFileName, DownloadProgressCallback)) {
std::cout << "File downloaded successfully." << std::endl;
if (Bootstrap::extractZip(outputFileName, extractPath)) {
Expand All @@ -67,8 +107,8 @@ int main() {
std::cerr << "Error extracting zip file." << std::endl;
}
if(Bootstrap::isDirectory(extractPath))
Bootstrap::rmDirectory(extractPath);
if(Bootstrap::IsDirectory(extractPath))
Bootstrap::RmDirectory(extractPath);
} else {
std::cerr << "Error downloading file." << std::endl;
Expand Down
Loading

0 comments on commit 4e97ca4

Please sign in to comment.