-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.hpp
171 lines (153 loc) · 4.85 KB
/
Utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef UTILS_H
#define UTILS_H
#include <cstring>
#include <unistd.h> // For read
#include <sys/types.h>
#include <sys/socket.h>
#include "Message.hpp"
struct Utils
{
public:
static void check_path_exists(std::string& path);
static std::string itoa(int a);
template <typename T> static void unserialize_message(T& msg, int connection);
template <typename T> static void serialize_message(T& msg, int sockfd);
static void send_msg(int sockfd, std::string path, ssize_t file_size);
static void recursive_send(std::string path, int sockfd);
};
void Utils::recursive_send(std::string path, int sockfd)
{
std::filesystem::path p(path);
std::string upload_path(p.filename().generic_string());
bool isDir = std::filesystem::is_directory(p);
if (isDir)
{
Message::unixFile msg(std::filesystem::path(upload_path),isDir,upload_path.length());
Utils::serialize_message<Message::unixFile>(msg,sockfd);
for (const auto & entry : std::filesystem::recursive_directory_iterator(path))
{
isDir = std::filesystem::is_directory(entry.path());
if (!isDir)
{
Message::unixFile msg(std::filesystem::path(upload_path+"/"+entry.path().filename().generic_string()),isDir,entry.file_size());
Utils::serialize_message<Message::unixFile>(msg,sockfd);
Utils::send_msg(sockfd,entry.path().c_str(),entry.file_size());
}else
{
upload_path = upload_path+"/"+entry.path().filename().generic_string();
Message::unixFile msg(std::filesystem::path(upload_path),isDir,upload_path.length());
Utils::serialize_message<Message::unixFile>(msg,sockfd);
}
}
}else
{
Message::unixFile msg(std::filesystem::path(upload_path),isDir,std::filesystem::file_size(p));
Utils::serialize_message<Message::unixFile>(msg,sockfd);
Utils::send_msg(sockfd,path,std::filesystem::file_size(p));
}
Message::unixFile fin("",isDir,0);
Utils::serialize_message<Message::unixFile>(fin,sockfd);
}
void Utils::send_msg(int sockfd, std::string path, ssize_t file_size)
{
FILE * readFile = fopen(path.data(),"rb");
if (readFile == NULL)
{
std::cout<<"Unable to open File";
fclose(readFile);
close(sockfd);
}
int buffSize = 1024;
if (file_size<1024)
{
buffSize = file_size;
}
std::cout<<"\nNumber of Bytes :"<<file_size<<std::endl;
std::string FileSize = Utils::itoa(buffSize).c_str();
FileSize[buffSize] = '\0';
send(sockfd,FileSize.c_str(),buffSize,0);
double origin_size = file_size;
char buffer[buffSize];
int pourc;
int bytesReceived = 0;
int test = 0;
int last_pourc = 0;
while(file_size > 0)
{
bytesReceived = 0;
memset(buffer,0,sizeof(buffer));
if(file_size>1024)
{
fread(buffer, 1024, 1, readFile);
bytesReceived = send(sockfd, buffer, 1024, 0 );
}
else
{
fread(buffer, file_size, 1, readFile);
buffer[file_size]='\0';
bytesReceived = send( sockfd, buffer, file_size, 0 );
}
test+=bytesReceived;
file_size -= 1024;
pourc = (test/origin_size)*100;
if (pourc != last_pourc && pourc%10 == 0)
{
std::cout<<pourc<<std::endl;
last_pourc = pourc;
}
}
char recev[10];
memset(recev,0,sizeof(recev));
read(sockfd,recev,strlen("END"));
if(strcmp(recev,"END")== 0)
{
std::cout<<"END received"<<std::endl;
}
memset(recev,0,sizeof(recev));
fclose(readFile);
}
void Utils::check_path_exists(std::string& path)
{
std::filesystem::path p(path);
while (!std::filesystem::exists(p))
{
std::cout<<"This file doesn't exists. Enter a correct file path."<<std::endl;
std::cin >> path;
p = std::filesystem::path(path);
}
}
std::string Utils::itoa(int a)
{
std::string ss=""; //create empty string
while(a)
{
int x=a%10;
a/=10;
char i='0';
i=i+x;
ss=i+ss; //append new character at the front of the string!
}
return ss;
}
template <typename T>
void Utils::unserialize_message(T& msg, int connection)
{
char buffer[sizeof(msg)];
memset(buffer,0,sizeof(buffer));
std::string temp;
std::stringstream ss;
read(connection, buffer, sizeof(msg));
temp.assign(buffer);
ss << temp;
ss >> msg;
ss.clear();
}
template <typename T>
void Utils::serialize_message(T& msg, int sockfd)
{
std::stringstream ss;
ss << msg; //serialize
write (sockfd, ss.str().c_str(), sizeof(msg));
ss.clear();
}
#endif