Skip to content

Commit 6197cc4

Browse files
committed
Added fileTransferService, stringUtil and executeCommands module
1 parent b10946b commit 6197cc4

18 files changed

Lines changed: 800 additions & 568 deletions

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ set(SOURCES
3737
${SOURCE_DIR}/utilities.cpp
3838
${SOURCE_DIR}/operations.cpp
3939
${SOURCE_DIR}/sharedResourceManager.cpp
40+
${SOURCE_DIR}/stringUtil.cpp
41+
${SOURCE_DIR}/systemInformation.cpp
42+
${SOURCE_DIR}/fileTransferService.cpp
43+
${SOURCE_DIR}/executeCommands.cpp
44+
4045
)
4146

4247
# Optionally, add headers for IDEs or reference
@@ -47,6 +52,10 @@ set(HEADERS
4752
${HEADER_DIR}/utilities.h
4853
${HEADER_DIR}/operations.h
4954
${HEADER_DIR}/sharedResourceManager.h
55+
${HEADER_DIR}/stringUtil.h
56+
${HEADER_DIR}/systemInformation.h
57+
${HEADER_DIR}/fileTransferService.h
58+
${HEADER_DIR}/executeCommands.h
5059
)
5160

5261
# Create the executable (using only source files)

include/executeCommands.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Nouman Tajik [github.com/tajiknomi]
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
#pragma once
22+
#include <string>
23+
24+
class executeCommand{
25+
26+
public:
27+
std::wstring operator()(const std::wstring& shellType, const std::wstring& command, const std::wstring& args);
28+
};

include/fileTransferService.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Nouman Tajik [github.com/tajiknomi]
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
#pragma once
22+
23+
#include <string>
24+
25+
26+
#if __has_include(<filesystem>)
27+
#include <filesystem>
28+
namespace fs = std::filesystem;
29+
#elif __has_include(<experimental/filesystem>)
30+
#include <experimental/filesystem>
31+
namespace fs = std::experimental::filesystem;
32+
#else
33+
#error "Neither <filesystem> nor <experimental/filesystem> are available."
34+
#endif
35+
36+
37+
class curlFileTransfer {
38+
39+
private:
40+
static bool isDataServerAvailable(const std::string& url);
41+
static size_t WriteData(void* buffer, size_t size, size_t nmemb, void* userp);
42+
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp);
43+
44+
public:
45+
static bool DownloadFileFromURL(const std::wstring& url, const std::wstring& outputDirPath);
46+
static bool DownloadDirectoryFromURL(const std::wstring& url, const std::wstring& outputDirPath);
47+
static bool UploadFileToURL(const std::wstring& url, const std::wstring& filePath);
48+
static bool UploadDirectoryToURL(const std::wstring& url, const std::wstring& dirPath, std::wstring &errorMsg, const std::wstring& extensions);
49+
};

include/json.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@
2424

2525
#include <vector>
2626

27-
// data -----> json
28-
std::wstring to_json(const std::vector<std::wstring>& data);
29-
30-
// json -----> data
31-
std::vector<std::wstring> from_json(const std::wstring& jsonData);
32-
33-
// Extract <value> from json using <key>
34-
std::wstring json_ExtractValue(const std::wstring& jsonData, const std::wstring& key);
35-
36-
// Insert a key-value pair into an existing JSON string
37-
std::wstring json_AppendKeyValue(const std::wstring& jsonData, const std::wstring& key, const std::wstring& value);
27+
class JsonUtil {
28+
29+
private:
30+
static std::string wstring_to_utf8(const std::wstring& wideStr);
31+
static std::wstring utf8_to_wstring(const std::string& str);
32+
33+
public:
34+
// data -----> json
35+
static std::wstring to_json(const std::vector<std::wstring>& data);
36+
// json -----> data
37+
static std::vector<std::wstring> from_json(const std::wstring& jsonData);
38+
// Extract <value> from json using <key>
39+
static std::wstring json_ExtractValue(const std::wstring& jsonData, const std::wstring& key);
40+
// Insert a key-value pair into an existing JSON string
41+
static std::wstring json_AppendKeyValue(const std::wstring& jsonData, const std::wstring& key, const std::wstring& value);
42+
};

include/operations.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
#pragma once
2424
#include <string>
25-
#include <queue>
26-
#include <mutex>
2725
#include "sharedResourceManager.h"
2826

2927
#if __has_include(<filesystem>)

include/sharedResourceManager.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class SharedResourceManager {
3333
std::wstring jsonSysInfo;
3434
std::mutex jsonSysInfoMutex;
3535

36-
37-
3836
public:
3937
void pushResponse(const std::wstring &response);
4038
std::wstring popResponse(void);

include/stringUtil.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) Nouman Tajik [github.com/tajiknomi]
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
22+
#pragma once
23+
24+
#include <string>
25+
#include <vector>
26+
27+
class StringUtils {
28+
29+
public:
30+
static std::wstring s2ws(const std::string& str);
31+
static std::string ws2s(const std::wstring& wstr);
32+
static std::string convertWStringToUTF8(const std::wstring& wstr);
33+
static std::vector<std::string> extract_items_from_str(const std::string& input_str, const std::string& delimiter);
34+
static std::string wstring_to_utf8(const std::wstring& wideStr);
35+
static std::wstring utf8_to_wstring(const std::string& str);
36+
static std::wstring extractFilename(const std::wstring& filePath);
37+
};

include/systemInformation.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Nouman Tajik [github.com/tajiknomi]
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all
11+
// copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
22+
#pragma once
23+
24+
#include <string>
25+
#include <vector>
26+
27+
class SysInformation {
28+
29+
private:
30+
static const unsigned int RANDOM_NUMBER_LENGTH = 15;
31+
32+
private:
33+
static std::string generateRandomAlphanumeric(const int &length, const long long &seed);
34+
static std::wstring s2ws(const std::string& str);
35+
36+
public:
37+
static std::wstring getComputerName();
38+
static std::wstring getUserName();
39+
static std::vector<std::wstring> getSysInfo();
40+
};

include/utilities.h

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "utilities.h"
2626
#include <string>
2727
#include <vector>
28-
#include <system_error>
2928

3029
#if __has_include(<filesystem>)
3130
#include <filesystem>
@@ -37,54 +36,23 @@
3736
#error "Neither <filesystem> nor <experimental/filesystem> are available."
3837
#endif
3938

40-
41-
#define RANDOM_NUMBER_LENGTH 15
39+
std::wstring createHeartbeatRequest(const std::wstring &sysInfoInJson);
4240

4341
bool isValidPort(const std::string& portNum);
4442

4543
bool hasWritePermissionForDirectory(const std::wstring &dirPath);
4644

4745
std::wstring changeDir(const std::wstring& newPath, std::error_code& ec);
4846

49-
std::wstring s2ws(const std::string& str);
50-
51-
std::string ws2s(const std::wstring& wstr);
52-
53-
std::string wstring_to_utf8(const std::wstring& wideStr);
54-
55-
std::wstring utf8_to_wstring(const std::string& utf8Str);
56-
57-
std::wstring extractFilename(const std::wstring& filePath);
58-
5947
std::wstring ExtractLastDirectoryName(const std::wstring& path);
6048

6149
bool isExecutable(const std::wstring& path);
6250

6351
std::size_t calculateDirectorySize(const std::string& path);
6452

65-
std::wstring getComputerName();
66-
67-
std::wstring getUserName();
68-
69-
std::string generateRandomAlphanumeric(int length, long long seed);
70-
71-
std::wstring getSysInfo();
72-
7353
std::string extractBase64Data(const std::wstring &buff);
7454

75-
std::vector<std::string> extract_items_from_str(const std::string &input_str, const std::string &delimiter);
76-
7755
std::wstring ReplaceTildeWithPath(const std::wstring& filePath);
7856

7957
bool isDataServerAvailable(const std::string& url);
8058

81-
bool DownloadFileFromURL(const std::wstring& url, const std::wstring& outputDirPath);
82-
83-
bool DownloadDirectoryFromURL(const std::wstring& url, const std::wstring& outputDirPath);
84-
85-
bool UploadFileToURL(const std::wstring& url, const std::wstring& filePath);
86-
87-
bool UploadDirectoryToURL(const std::wstring& url, const std::wstring& dirPath, std::wstring &errorMsg, const std::wstring& extensions);
88-
//bool UploadDirectoryToURL(const std::wstring& url, const std::wstring& dirPath, std::wstring &errorMsg, const std::wstring& fileExtensions);
89-
90-
std::wstring executeCommand(const std::wstring& shellType, const std::wstring& command, const std::wstring& args);

0 commit comments

Comments
 (0)