-
Notifications
You must be signed in to change notification settings - Fork 0
/
arinc615a.cpp
340 lines (329 loc) · 12.7 KB
/
arinc615a.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "arinc615a.h"
#include <iostream>
#include <fstream>
#include <memory>
#include <filesystem>
void Arinc615a::information(information_para* arg){
std::cout << "in information" << std::endl;
cond* m_cond = arg->m_cond;
//information_para* arg1 = static_cast<information_para*>(arg);
sockaddr_in addr = arg->addr;
while(arg->status != information_para::END){
switch (arg->status){
case information_para::SEND_LCI:
//<<<<<<< HEAD
jobs.push(Job(Job::send, Job::server, "02DA.LCI", addr));
//=======
//jobs.push(Job(Job::send, Job::server, "./tmp/02DA.LCI", addr));
//>>>>>>> 9953ffaca2e91362743a759e3b91c6e1c69f0979
gotNewJob.signal();
m_cond->wait();
arg->status = information_para::SEND_LCL;
break;
case information_para::SEND_LCL:{
sockaddr_in target_server_addr = addr;
target_server_addr.sin_port = htons(8888);
jobs.push(Job(Job::send, Job::client, "02DA.LCL", target_server_addr));
gotNewJob.signal();
m_cond->wait();
arg->status = information_para::END;
break;
}
case information_para::END:
break;
default:
break;
}
}
std::cout << "information finished\n";
}
static int h = 0;
void Arinc615a::upload(upload_para* arg){
std::cout << "in upload" << std::endl;
cond* m_cond = arg->m_cond;
sockaddr_in addr = arg->addr;
sockaddr_in target_server_addr = addr;
target_server_addr.sin_port = htons(8888);
while(arg->status != upload_para::END){
switch(arg->status){
case upload_para::SEND_LUI:{
jobs.push(Job(Job::send, Job::server, "02DA.LUI", addr));
gotNewJob.signal();
m_cond->wait();
arg->statusCode = 1;
arg->statusDescription = "accpeted";
arg->status = upload_para::WRITE_LUS;
break;
}
case upload_para::WRITE_LUS:{
makeLUS(arg);
jobs.push(Job(Job::send, Job::client, "02DA.LUS", target_server_addr));
gotNewJob.signal();
switch(arg->statusCode){
case 0x0001:
std::cout << "0x0001" << std::endl;
arg->status = upload_para::RECEIVE_LUR;
break;
case 0x0002:
std::cout << "0x0002" << std::endl;
arg->status = upload_para::READ_DATA_FILE;
break;
case 0x0003:
std::cout << "0x0003" << std::endl;
arg->status = upload_para::END;
break;
case 0x1003:
std::cout << "0x1003" << std::endl;
break;
case 0x1004:
std::cout << "0x1004" << std::endl;
break;
case 0x1005:
std::cout << "0x1005" << std::endl;
break;
default:
std::cout << "default" << std::endl;
break;
}
break;
}
case upload_para::RECEIVE_LUR:
std::cout << "LUR" << std::endl;
m_cond->wait();
arg->status = upload_para::READ_LUH;
++h;
break;
case upload_para::READ_LUH:
jobs.push(Job(Job::receive, Job::client, "02DA.LUH", target_server_addr));
gotNewJob.signal();
m_cond->wait();
parseLUH(arg);
arg->status = upload_para::READ_DATA_FILE;
break;
case upload_para::READ_DATA_FILE:
jobs.push(Job(Job::receive, Job::client, arg->fileList[arg->cur++], target_server_addr));
std::cout << arg->cur << " 23423" << std::endl;
gotNewJob.signal();
m_cond->wait();
if(arg->cur < arg->fileList.size()){
arg->statusCode = 0x0002;
arg->statusDescription = "upload in progress";
}
else{
arg->statusCode = 0x0003;
arg->statusDescription = "upload complete";
}
arg->status = upload_para::WRITE_LUS;
break;
case upload_para::END:
break;
}
}
std::cout << "upload finished\n";
}
void Arinc615a::operatorDownload(oDownload_para* arg){
std::cout << "in operator download" << std::endl;
cond* m_cond = arg->m_cond;
sockaddr_in addr = arg->addr;
sockaddr_in target_server_addr = addr;
target_server_addr.sin_port = htons(8888);
while(arg->status != oDownload_para::END){
switch(arg->status){
case oDownload_para::SEND_LNO:
jobs.push(Job(Job::send, Job::server, "02DA.LNO", addr));
gotNewJob.signal();
m_cond->wait();
arg->statusCode = 0x0001;
arg->statusDescription = "oDownload accpet";
arg->status = oDownload_para::WRITE_LNS;
break;
case oDownload_para::WRITE_LNS:
makeLNS(arg);
jobs.push(Job(Job::send, Job::client, "02DA.LNS", target_server_addr));
gotNewJob.signal();
switch(arg->statusCode){
case 0x0001:
arg->status = oDownload_para::WRITE_LNL;
break;
case 0x0002:
arg->status = oDownload_para::WRITE_DATA_FILE;
break;
case 0x0003:
arg->status = oDownload_para::END;
break;
default:
std::cout << "oDownload default ...." << std::endl;
break;
}
break;
case oDownload_para::WRITE_LNL:
makeLNL(arg);
jobs.push(Job(Job::send, Job::client, "02DA.LNL", target_server_addr));
gotNewJob.signal();
m_cond->wait();
arg->status = oDownload_para::RECEIVE_LNA;
break;
case oDownload_para::RECEIVE_LNA:
m_cond->wait();
parseLNA(arg);
arg->status = oDownload_para::WRITE_LNS;
arg->statusCode = 0x0002;
arg->statusDescription = "odownload is about to begin";
break;
case oDownload_para::WRITE_DATA_FILE:
jobs.push(Job(Job::send, Job::client, arg->requestFileList[arg->cur++], target_server_addr));
gotNewJob.signal();
m_cond->wait();
if(arg->cur == arg->requestFileList.size()){
arg->statusCode = 0x0003;
arg->statusDescription = "odownload complete";
}
else{
arg->statusCode = 0x0002;
arg->statusDescription = "odownload in progress";
}
arg->status = oDownload_para::WRITE_LNS;
break;
case oDownload_para::END:
break;
default:
break;
}
}
}
void Arinc615a::makeLUS(upload_para* arg){
std::ofstream os("./tmp/02DA.LUS", std::ios_base::trunc | std::ios_base::binary);
if(os){
os.seekp(4);
os << "A3";
os.write((char*)&arg->statusCode, sizeof(arg->statusCode));
os << (unsigned char)arg->statusDescription.size();
os << arg->statusDescription;
auto size = os.tellp();
os.seekp(0);
os.write(reinterpret_cast<char*>(&size), sizeof(unsigned));
os.close();
}
}
void Arinc615a::parseLUH(upload_para *arg){
std::ifstream ifs("./tmp/02DA.LUH", std::ios::in);
ifs.seekg(0, std::ios_base::end);
std::cout << ifs.tellg() << std::endl;
std::cout << "before" << std::endl;
if (!ifs.is_open()) std::cout << "failed to open " << std::endl;
ifs.seekg(0, std::ios::beg);
if(ifs){
std::cout << "after" << std::endl;
ifs.seekg(40);
unsigned short fileListSize;
ifs.read((char*)&fileListSize, sizeof(fileListSize));
//ifs >> fileListSize;
std::cout << "fileListSize " << fileListSize << std::endl;
for(int i = 0; i < fileListSize; i++){
auto now = ifs.tellg();
std::cout << "now = " << now << std::endl;
ifs.seekg(2, std::ios_base::cur);
unsigned short fileNameLen;
ifs.read((char*)&fileNameLen, sizeof(unsigned short));
//ifs >> fileNameLen;
std::string fileName(fileNameLen, '\0');
ifs.read(&fileName[0], fileNameLen);
arg->fileList.push_back(fileName);
std::cout << fileName << std::endl;
ifs.seekg(789 + now);
}
ifs.close();
}
}
void Arinc615a::makeLNS(oDownload_para *arg){
std::ofstream os("./tmp/02DA.LNS", std::ios_base::trunc | std::ios_base::trunc);
if(os){
os.seekp(4);
os << "A3";
os.write(reinterpret_cast<char*>(&arg->statusCode), sizeof(arg->statusCode));
//os.seekp(273);
os << (unsigned char)arg->statusDescription.size();
os << arg->statusDescription;
// std::filesystem::directory_entry entry("../tmp");
// if(entry.exists()){
// std::filesystem::directory_iterator list("../tmp");
// for(auto it : list){
// arg->fileList.push_back(it.path().filename());
// }
// }
// os.write(reinterpret_cast<char*>(arg->fileList.size()), sizeof(short));
// for(int i = 0; i < arg->fileList.size(); i++){
// auto cur = os.tellp();
// os << (unsigned char)arg->fileList[i].size();
// os << arg->fileList[i];
// os.seekp(cur + 514);
// }
os.seekp(273);
unsigned short requestFileListSize = arg->requestFileList.size();
os.write(reinterpret_cast<char*>(&requestFileListSize), sizeof(unsigned short));
auto size = os.tellp();
os.seekp(0);
os.write(reinterpret_cast<char*>(&size), sizeof(unsigned));
os.close();
}
}
void Arinc615a::makeLNL(oDownload_para *arg){
std::ofstream ofs("./tmp/02DA.LNL", std::ios_base::trunc);
if(ofs){
ofs.seekp(4);
ofs << "A3";
std::filesystem::directory_entry entry("./tmp");
if(entry.exists()){
std::filesystem::directory_iterator list("./tmp");
for(auto it : list){
arg->fileList.push_back(it.path().filename().string());
if(it.is_directory()){
arg->fileType.push_back("directorys");
}
else{
arg->fileType.push_back("files");
}
}
}
short fileListSize = arg->fileList.size();
ofs.write(reinterpret_cast<char*>(&fileListSize), sizeof(short));
for(int i = 0; i < arg->fileList.size(); i++){
ofs << (unsigned char)arg->fileList[i].size();
auto cur = ofs.tellp();
ofs << arg->fileList[i];
ofs.seekp(255 + cur);
ofs << (unsigned char)arg->fileType[i].size();
cur = ofs.tellp();
ofs << arg->fileType[i];
if(i < arg->fileList.size() - 1){
ofs.seekp(255 + cur);
}
}
auto size = ofs.tellp();
ofs.seekp(0);
ofs.write(reinterpret_cast<char*>(&size), sizeof(signed));
ofs.close();
}
}
void Arinc615a::parseLNA(oDownload_para *arg){
std::ifstream ifs("./tmp/02DA.LNA", std::ios_base::binary);
if(ifs){
//ifs.seekg(4);
unsigned fileLen;
ifs.read(reinterpret_cast<char*>(&fileLen), sizeof(unsigned));
std::string version(2, '\0');
ifs.read(&version[0], 2);
short fileCnt;
ifs.read(reinterpret_cast<char*>(&fileCnt), sizeof(fileCnt));
for(int i = 0; i < fileCnt; i++){
unsigned char fileNameLen;
ifs.read(reinterpret_cast<char*>(&fileNameLen), sizeof(fileNameLen));
std::string fileName(fileNameLen, '\0');
ifs.read(&fileName[0], fileNameLen);
arg->requestFileList.push_back(fileName);
std::cout << "fileName=" << fileName << std::endl;
ifs.seekg(768 - fileNameLen, std::ios_base::cur);
}
ifs.close();
}
}