Skip to content

Commit db32df4

Browse files
committed
first
0 parents  commit db32df4

18 files changed

+1576
-0
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(zip)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ../bin)
7+
add_executable(server server_test.cpp src/Client.cpp src/Client.h src/Socket.cpp src/Socket.h src/com.h src/Server.cpp src/Server.h src/LZ77.h src/Base_Text_Process.h src/Factory.cpp src/Factory.h src/com.cpp src/algorithm.cpp src/algorithm.h)
8+
add_executable(client client_test.cpp src/Client.cpp src/Client.h src/Socket.cpp src/Socket.h src/com.h src/Server.cpp src/Server.h src/LZ77.h src/Base_Text_Process.h src/Factory.cpp src/Factory.h src/com.cpp src/algorithm.cpp src/algorithm.h)

README.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
首先新建build/目录和bin/目录
2+
进入build/目录,执行 cmake .. 命令,之后执行make,可执行文件就输出到bin/目录中了
3+
4+
///
5+
mkdir build
6+
mkdir bin
7+
cd build
8+
cmake ..
9+
make
10+
cd ..
11+
bin/server
12+
bin/client
13+
///
14+
15+
支持三种命令ls、cat、send
16+
例如: ls,打印服务端目录结构(不能加参数)
17+
cat filename, 查看文件内容
18+
send -lz filename(目前可用参数 -lz,-al)

client_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
#include "src/com.h"
3+
#include "src/Client.h"
4+
int main(int argc, char** argv) {
5+
if (argc != 2 || check(argv[1])) {
6+
ASSERT(false, "help:: ./client port");
7+
}
8+
PORT = atoi(argv[1]);
9+
qml::Client* c = new qml::Client();
10+
c->Start();
11+
return 0;
12+
}

server_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
#include "src/Server.h"
3+
#include "src/com.h"
4+
5+
int main(int argc, char** argv) {
6+
if (argc != 2 || check(argv[1])) {
7+
ASSERT(false, "./server port");
8+
}
9+
PORT = atoi(argv[1]);
10+
qml::Server* s = new qml::Server();
11+
s->start();
12+
return 0;
13+
}

src/Base_Text_Process.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Created by ByteDance on 2022/4/12.
3+
//
4+
5+
#ifndef ZIP_BASE_TEXT_PROCESS_H
6+
#define ZIP_BASE_TEXT_PROCESS_H
7+
#include "com.h"
8+
namespace qml {
9+
class Encode {
10+
public:
11+
virtual bool file_encode(std::string, bool o2file, std::string) = 0;
12+
};
13+
14+
class Decode {
15+
public:
16+
virtual bool file_decode(std::string, bool o2file, std::string) = 0;
17+
};
18+
}
19+
20+
#endif //ZIP_BASE_TEXT_PROCESS_H

src/Client.cpp

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
//
2+
// Created by ByteDance on 2022/4/12.
3+
//
4+
5+
//
6+
// Created by ByteDance on 2022/4/12.
7+
//
8+
#include "Factory.h"
9+
#include "Client.h"
10+
#include "com.h"
11+
12+
namespace qml {
13+
Client::Client() {
14+
try {
15+
socket_com = new Socket("127.0.0.1", 0);
16+
} catch (std::bad_alloc &e) {
17+
std::cerr << e.what() << std::endl;
18+
}
19+
20+
bzero(cmd, sizeof cmd);
21+
encodeFactory = new EncodeFactory();
22+
decodeFactory = new DecodeFactory();
23+
}
24+
25+
void Client::Start() {
26+
NetWork* nw = new NetWork("127.0.0.1", PORT);
27+
28+
if (socket_com->Connect(nw)) {
29+
ASSERT(false, "connect error");
30+
}
31+
32+
delete nw;
33+
bzero(res, sizeof res);
34+
int num = socket_com->Recv(res, sizeof res);
35+
res[num] = 0;
36+
PORT2 = atoi(res);
37+
// std::cout << PORT2 << std::endl;
38+
handle();
39+
}
40+
41+
void Client::split() {
42+
com.clear();
43+
std::string s;
44+
int len = strlen(cmd);
45+
for (int i = 0; i < len; i++) {
46+
if (cmd[i] == ' ') {
47+
if (!s.empty()) {
48+
com.push_back(s);
49+
s.clear();
50+
}
51+
} else if (cmd[i] != '\n') {
52+
s.push_back(cmd[i]);
53+
}
54+
}
55+
if (!s.empty())
56+
com.push_back(s);
57+
}
58+
59+
60+
void Client::Send_txt() {
61+
FILE* fp = fopen(filename, "r");
62+
memset(res, 0, sizeof res);
63+
int num_read;
64+
bzero(res, sizeof res);
65+
while ((num_read = fread(res, sizeof(char), sizeof res, fp)) > 0) {
66+
socket_file->Send(res, num_read);
67+
bzero(res, sizeof res);
68+
}
69+
fclose(fp);
70+
}
71+
72+
73+
void Client::SendFile() {
74+
EncodeType type = qml::check_en(com[1]);
75+
if (com[0] != "send" || type == NO_EN) {
76+
puts("error");
77+
} else {
78+
bzero(cmd, sizeof cmd);
79+
int m = 0;
80+
for (auto i : com) {
81+
for (auto j : i) {
82+
cmd[m++] = j;
83+
}
84+
cmd[m++] = ' ';
85+
}
86+
cmd[m] = 0;
87+
socket_com->Send(cmd, sizeof cmd);
88+
init_file();
89+
Encode* en = encodeFactory->CreateEncode(type);
90+
strcpy(filename, "12123_tmp_12123");
91+
en->file_encode(com.back(), true, filename);
92+
Send_txt();
93+
system("rm 12123_tmp_12123");
94+
delete socket_file;
95+
}
96+
}
97+
void Client::init_file() {
98+
try {
99+
socket_file = new Socket("127.0.0.1", 0);
100+
} catch (std::bad_alloc &e) {
101+
std::cerr << e.what() << std::endl;
102+
}
103+
NetWork* sock = new NetWork("127.0.0.1", PORT2);
104+
socket_file->Connect(sock);
105+
delete sock;
106+
}
107+
void Client::SendCom() {
108+
int num_read;
109+
socket_com->Send(cmd, sizeof cmd);
110+
init_file();
111+
bzero(res, sizeof res);
112+
while ((num_read = socket_file->Recv(res, sizeof res)) > 0) {
113+
std::cout << res;
114+
bzero(res, sizeof res);
115+
}
116+
delete socket_file;
117+
}
118+
void Client::handle() {
119+
bzero(cmd ,sizeof cmd);
120+
std::cout << "->";
121+
while (fgets(cmd, sizeof cmd, stdin) != NULL) {
122+
split();
123+
if (com.empty()) {
124+
std::cout << "->";
125+
continue;
126+
} else if ((int)com.size() > 3) {
127+
puts("error");
128+
} else if ((int) com.size() == 3) {
129+
SendFile();
130+
} else {
131+
if (com[0] == "exit") {
132+
break;
133+
} else if (com[0] == "cls") {
134+
system("clear");
135+
} else {
136+
SendCom();
137+
}
138+
}
139+
bzero(cmd, sizeof cmd);
140+
std::cout << "->";
141+
}
142+
}
143+
144+
Client::~Client() {
145+
delete socket_file;
146+
delete socket_com;
147+
delete encodeFactory;
148+
delete decodeFactory;
149+
}
150+
151+
}
152+
153+
154+

src/Client.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Created by ByteDance on 2022/4/12.
3+
//
4+
5+
#ifndef ZIP_CLIENT_H
6+
#define ZIP_CLIENT_H
7+
8+
#include "com.h"
9+
#include "Socket.h"
10+
#include "Factory.h"
11+
#include "Base_Text_Process.h"
12+
namespace qml{
13+
class Client{
14+
public:
15+
~Client();
16+
void Start();
17+
void handle();
18+
void split();
19+
int ReadFile();
20+
Client();
21+
void init_file();
22+
void SendFile();
23+
void Send_txt();
24+
void SendCom();
25+
26+
private:
27+
Socket* socket_com;
28+
Socket* socket_file;
29+
char res[128];
30+
char cmd[128];
31+
char filename[128];
32+
std::string txt;
33+
std::vector<std::string> com;
34+
EncodeFactory* encodeFactory;
35+
DecodeFactory* decodeFactory;
36+
};
37+
}
38+
39+
#endif //ZIP_CLIENT_H

src/Factory.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Created by ByteDance on 2022/4/12.
3+
//
4+
#include "com.h"
5+
#include "Factory.h"
6+
namespace qml {
7+
EncodeType check_en(std::string x) {
8+
if (x == "-lz")
9+
return LZ_EN;
10+
else if (x == "-al")
11+
return AL_EN;
12+
else
13+
return NO_EN;
14+
}
15+
16+
DecodeType check_de(std::string x) {
17+
if (x == "-lz")
18+
return LZ_DE;
19+
else if (x == "-al")
20+
return AL_DE;
21+
else
22+
return NO_DE;
23+
}
24+
25+
Encode* EncodeFactory::CreateEncode(EncodeType et) {
26+
switch (et) {
27+
case LZ_EN:
28+
return new File2mark();
29+
case AL_EN:
30+
return new algorithmEncode();
31+
default:
32+
return NULL;
33+
}
34+
}
35+
36+
Decode* DecodeFactory::CreateDecode(DecodeType dt) {
37+
switch (dt) {
38+
case LZ_DE:
39+
return new File2char();
40+
case AL_DE:
41+
return new algorithmDecode();
42+
default:
43+
return NULL;
44+
}
45+
}
46+
47+
}

src/Factory.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Created by ByteDance on 2022/4/12.
3+
//
4+
5+
#ifndef ZIP_FACTORY_H
6+
#define ZIP_FACTORY_H
7+
#include "com.h"
8+
#include "Base_Text_Process.h"
9+
#include "LZ77.h"
10+
#include "algorithm.h"
11+
12+
namespace qml {
13+
enum EncodeType {
14+
NO_EN = 0,
15+
LZ_EN = 1,
16+
AL_EN = 2,
17+
};
18+
enum DecodeType {
19+
NO_DE = 0,
20+
LZ_DE = 1,
21+
AL_DE = 2,
22+
};
23+
EncodeType check_en(std::string x);
24+
DecodeType check_de(std::string x);
25+
26+
class EncodeFactory {
27+
public:
28+
Encode* CreateEncode(EncodeType et);
29+
};
30+
class DecodeFactory {
31+
public:
32+
Decode* CreateDecode(DecodeType dt);
33+
};
34+
}
35+
36+
37+
#endif //ZIP_FACTORY_H

0 commit comments

Comments
 (0)