-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandinterpreter.cpp
More file actions
182 lines (163 loc) · 5.69 KB
/
commandinterpreter.cpp
File metadata and controls
182 lines (163 loc) · 5.69 KB
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
172
173
174
175
176
177
178
179
180
181
182
#include "commandinterpreter.h"
#include <fstream>
#include <chrono>
#include <iostream>
#include "../spolks1/helpers.h"
constexpr size_t batchSize = 145;
CommandInterpreter::CommandInterpreter(Connection& socket, int id):socket(socket)
{
maxPackageSize = socket.getMaxPackageSize();
clientId = id;
std::cerr << "Max package size: " << maxPackageSize << std::endl;
}
Command CommandInterpreter::decode(const std::string& cmd)
{
if (cmd.length() < 5)
return Command::Unknown;
if (cmd.substr(0, 5) == "echo ")
return Command::Echo;
if (cmd.substr(0, 5) == "file ")
return Command::File;
return Command::Unknown;
}
std::string CommandInterpreter::Interpret(const std::string &command)
{
Command cmd = decode(command);
std::string result = "Invalid command";
switch (cmd)
{
case Command::Echo:
if (command.length() > 5)
result = echo(command.substr(5));
break;
case Command::File:
if (command.length() > 5)
result = transferFile(command.substr(5));
break;
default:
break;
}
return result;
}
std::string CommandInterpreter::echo(const std::string& cmd)
{
Buffer buff(cmd.length() + sizeof(Header));
Header header = fillHeader(ServerCommand::Echo, cmd.length() + sizeof(Header), false);
buff.Write(header);
buff.Write(cmd.c_str(), cmd.length());
socket.Send(buff, buff.getSize());
return socket.getLine();
}
std::string CommandInterpreter::transferFile(const std::string& fileName)
{
std::fstream file;
size_t size;
FileInitPackage f;
unsigned fullChunks, lastChunkSize;
std::chrono::time_point<std::chrono::system_clock> start, end;
Buffer buff(maxPackageSize + sizeof(FileTransferPackage));
file.open(fileName, std::ios_base::in | std::ios_base::binary);
if (!file.is_open())
return "Error: cannot open file";
size = getFileSize(file);
f = fillInitPackage(size, fileName);
f.header = fillHeader(ServerCommand::FileTransferStart, sizeof(FileInitPackage), true);
fullChunks = size / maxPackageSize;
lastChunkSize = size % maxPackageSize;
socket.Send(f);
start = std::chrono::system_clock::now();
for (unsigned i = 0; i < fullChunks; i++)
{
if (i % batchSize == 0)
while (1)
{
sendMarker(i, batchSize);
if (resendMissingParts(buff, file, i, maxPackageSize, maxPackageSize))
break;
}
sendFilePart(buff, file, i, maxPackageSize, maxPackageSize);
}
if (lastChunkSize)
sendFilePart(buff, file, fullChunks, maxPackageSize, lastChunkSize);
end = std::chrono::system_clock::now();
std::chrono::duration<double> time = end - start;
return "File transfer complete, avg speed: " + std::to_string((double)size / time.count() / 1024.0 / 1024.0) + "mb/s";
}
bool CommandInterpreter::resendMissingParts(Buffer& buff, std::fstream& file, unsigned chunkId, unsigned fullChunkSize, unsigned chunkSize)
{
bool err = false;
if (socket.getData(buff, markerResponceSize, true) != OperationResult::Success)
return false;
buff.Clear();
if (chunkId == 0)
return true;
MarkerResponce state = buff;
for (size_t i = 0; i < batchSize; i++)
if (!state.bits[i])
{
err = true;
std::cerr << "Resending chunk: " << state.chunkId + i << std::endl;
sendFilePart(buff, file, state.chunkId + i, fullChunkSize, chunkSize);
}
if (err)
std::cerr << "Marker responce: " << state.bits.to_string() << std::endl;
sendMarker(chunkId, 0);
buff.Clear();
return true;
}
void CommandInterpreter::sendFilePart(Buffer& buff, std::fstream& file, unsigned chunkId, unsigned fullChunkSize, unsigned chunkSize)
{
static unsigned lastChunkId = 0;
FileTransferPackage p;
p.header = fillHeader(ServerCommand::FileTransferExecute, chunkSize + sizeof(p), true);
p.size = chunkSize;
p.chunkId = chunkId;
buff.Write(p);
if (chunkId != lastChunkId + 1)
file.seekg(fullChunkSize * chunkId, std::ios_base::beg);
file.read((char*)buff.getWritePointer(), chunkSize);
socket.Send(buff, p.header.packageSize);
buff.Clear();
lastChunkId = chunkId;
}
void CommandInterpreter::sendMarker(unsigned chunkId, size_t chunksCount)
{
FileTransferPackage package;
package.header = fillHeader(ServerCommand::FileTransferExecute, sizeof(package), false);
package.isMarker = 1;
package.chunkId = chunkId;
package.size = chunksCount;
socket.Send(package);
}
FileInitPackage CommandInterpreter::fillInitPackage(size_t fileSize, const std::string& fileName)
{
FileInitPackage f;
size_t pos;
f.chunksCount = fileSize / maxPackageSize + (fileSize % maxPackageSize ? 1 : 0);
f.chunkSize = maxPackageSize;
f.fileSize = fileSize;
if ((pos = fileName.find_last_of('/')) != fileName.npos)
memcpy(f.fileName, fileName.substr(pos + 1).c_str(), fileName.substr(pos + 1).length());
if ((pos = fileName.find_last_of('\\')) != fileName.npos)
memcpy(f.fileName, fileName.substr(pos + 1).c_str(), fileName.substr(pos + 1).length());
else
memcpy(f.fileName, fileName.c_str(), fileName.length());
return f;
}
size_t CommandInterpreter::getFileSize(std::fstream& file)
{
size_t size;
file.seekg(0, std::ios_base::end);
size = file.tellg();
file.seekg(0);
return size;
}
Header CommandInterpreter::fillHeader(ServerCommand command, size_t size, bool confirm)
{
Header header;
header.command = helpers::integral(command);
header.packageSize = size;
header.needsConfirmation = confirm ? 1 : 0;
header.id = clientId;
return header;
}