FTP文件管理项目(本地云)项目日报(九)

2020-08-26 11:27:17 浏览数 (1)

写了这第九篇日报,就意味着这个为期12个“工作日”的项目到了大后期啦。 这两天我去找我哥聊了聊这个项目的架构,他是做Java的,不过对我用C 讲的这个项目还是很有兴趣哈哈哈。

一路下来,只剩四个人啦,从刚开始的十个人。

剩下的四个人

1号(服务器)

日报八(系统整体重构图)

5号(测试端)

日报7(QT定时器)

日报8(日志模块)

8号(客户端)

9号(客户端)

其中5号已经交上代码功成身退了。

其实我的初衷也是如此,除去我还剩九个人,最后能留下三个足矣。 且不说我们做的咋样,只要坚持下来,就是好样的!!!

本日任务

重构

完成情况

高估了自己,任务太重,FTP服务器模块还没重构,DB服务器重构之后还没对各模块再次测试,估计还要一天。

这两天做了些动态库,把进程间通信由静态变为动态(之前代码里的备注说回头改的地方),现在可以自由拓展了。

数据库功能完善与上锁,协议包再修订(今晚还要微调)。

代码

Linux系统关了,不想再开,关之前忘了把代码拿出来,所以下面这个代码是改bug之前的。

代码语言:javascript复制
//DB_command.h

#include"DB_base.h"
#include"Mutex.h"

class DB_command
{
private:
	DB_base* My_DB;
	Mutex* DB_Mutex;

public:
	DB_command();

	//增
	bool add_user(int id,int pwd,char* tel,char* err);
	bool add_file(char* file_name,int owner_id, int pwd, int mode,int dir_id,int force,char* err);//force,是否替换原文件
	bool add_list(int id,int pwd,int father_dir,char* dir_name,char* err);
	
	//删
	bool del_user(int user_id,int pwd);
	bool del_file(int user_id,int pwd,char* file_name,int dir_id,char* err);
	bool del_list(int user_id,int pwd,int dir_id,char* err);
	
	//查
	bool login_check(int user_id,int pwd);	//登录验证,不带目录id
	int login(int user_id, int pwd,char* err);	//带上id的登录验证
	int pwd(int user_id,char* tel);
	void file_list(int user_id,int pwd,int dir_id, char* ret,char* err);
    bool download(int user_id,int pwd,char* file_name, int dir_id,int shared,char* err);
    
	//改
	bool cpwd(int user_id, int old_pwd, int new_id,char* err);
	bool cfile_addr(int user_id,int pwd,char* file_name, int old_dir, int new_dir,char* err);
	bool cdir_addr(int user_id,int pwd,char* dir_name, int new_dir,char* err);
	bool share_file(int user_id, int pwd, int shared, char* file_name, int dir_id,char* err);
	bool cfile_name(int user_id, int pwd, char* file_name, int file_id, int dir_id,char* err);
	bool cdir_name(int user_id, int pwd, char* dir_name, int dir_id,char* err);

	//适配器函数
	char* dir(int dir_id);	//将dir_id转换为dir_name
	char* file(int file_id);//将file_id转换为file_name
};
代码语言:javascript复制
//DB_command.cpp

#include "DB_command.h"

DB_command::DB_command() {
/*	DB_init* init = new DB_init();
	init->commmand();
	delete init;*/

	char* name = new char[10];
	sprintf(name, "FTP.db");
	My_DB = DB_base::instence(name);
	delete name;

	DB_Mutex = new Mutex();
}

//增
bool DB_command::add_user(int id, int pwd, char* tel,char* err) {
	
	//先查一下该用户是否已存在
	char* sql = new char[120];
	int ret = 0;
	DB_Mutex->lock();
	sprintf(sql, "select pwd from UserMsg where id = %d", id);
	if (int a = My_DB->return_serch_num(sql) == 1) {
		strcpy(err, "User already in!");
		DB_Mutex->unlock();
		delete sql;
		return false;
	}
	else {
	//查询目录id使用情况
		sprintf(sql, "select max(dir_id) from Dir_Msg");
		int max_dir = My_DB->ireturn_serch(sql,1)   1;

		char* name = new char[6];
		sprintf(name,"MyDir");
		sprintf(sql, "insert into DirMsg values(%d,'%s',%d,%d)", max_dir,name, 0, id);
		ret = My_DB->excute(sql);

		sprintf(sql, "insert into UserMsg values(%d,%d,'%s',%d)", id, pwd, tel, max_dir);
	    ret = My_DB->excute(sql);

		DB_Mutex->unlock();
		delete sql;
		return ret;
	}
}

bool DB_command::add_file(char* file_name, int owner_id, int pwd,int mode, int dir_id,int change, char* err) {
	DB_Mutex->lock();
	if (!login_check(owner_id, pwd)) {
		strcpy(err, "Have no right!");
		DB_Mutex->unlock();
		return false;
	}
	char* sql = new char[120];
	int ret = 0;
	sprintf(sql, "select owner_id from DirMsg where dir_id = %d", dir_id);
	if (My_DB->return_serch_num(sql)) {
		//再对文件id进行安排
		sprintf(sql, "select owner_id from FileMsg where file_name = '%s' and dir_id = %d", file_name, dir_id);
		if (My_DB->ireturn_serch(sql, 1) == owner_id) {
			if (change == 0) {
				DB_Mutex->unlock();
				strcpy(err, "Duplicate file!");
				delete sql;
				return false;
			}
			else {
				sprintf(sql, "update FileMsg set file_name = '%s' and mode = %d ", file_name, mode);
				ret = My_DB->excute(sql);
				DB_Mutex->unlock();
				delete sql;
				return true;
			}
		}
		else {
			sprintf(sql, "select max(file_id) from FileMsg");
			int file_id = My_DB->ireturn_serch(sql, 1)   1;
			sprintf(sql, "insert into FileMsg values('%s',%d,%d,%d,%d,0)", file_name, owner_id, mode, dir_id, file_id);
			ret = My_DB->excute(sql);
			DB_Mutex->unlock();
			delete sql;
			return true;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "dir is not exit!");
		delete sql;
		return false;
	}
}

bool DB_command::add_list(int id, int pwd, int father_dir, char* dir_name, char* err) {
	DB_Mutex->lock();
	if (login_check(id,pwd)) {
		char* sql = new char[120];
		int ret = 0;
		sprintf(sql, "select grade from DirMsg where dir_name = '%s'", dir_name);
		ret = My_DB->ireturn_serch(sql, 1);
		if (ret == father_dir) {
			DB_Mutex->unlock();
			strcpy(err, "dir is exit!");
			delete sql;
			return false;
		}
		else {
			sprintf(sql, "select max(dir_id) from Dir_Msg");
			int max_dir = My_DB->ireturn_serch(sql, 1)   1;
			sprintf(sql, "insert into DirMsg values(%d,'%s',%d,%d)", max_dir,dir_name,father_dir,id);
			My_DB->excute(sql);
			DB_Mutex->unlock();
			delete sql;
			return true;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");	//无权
		return false;
	}
}
	

//删
bool DB_command::del_user(int user_id,int pwd) {
	DB_Mutex->lock();
	if (login_check(user_id,pwd)) {
		char* sql = new char[120];
		sprintf(sql, "delete from UserMsg where id = %d and pwd = %d", user_id, pwd);
		My_DB->excute(sql);
		sprintf(sql, "delete from FileMsg where owner_id = %d", user_id);
	    My_DB->excute(sql);
		sprintf(sql, "delete from DirMsg where owner_id = %d", user_id);
		My_DB->excute(sql);
		DB_Mutex->unlock();
		delete sql;
		return true;
	}
	else {
		DB_Mutex->unlock();
		return false;
	}
}

bool DB_command::del_file(int user_id, int pwd, char* file_name,int dir_id, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select * from FileMsg where owner_id = %d and file_name = '%s'", user_id, file_name);
		int ret = My_DB->return_serch_num(sql);
		if (ret) {
			sprintf(sql, "delete from FileMsg where dir_id = %d and file_name = '%s'", dir_id, file_name);
			My_DB->excute(sql);
			DB_Mutex->unlock();
			delete sql;
			return true;
		}
		else {
			DB_Mutex->unlock();
			strcpy(err, "Have no right!");	//无权
			delete sql;
			return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");	//无权
		return false;
	}
}

//目前不支持非空文件夹删除操作,不然子又生孙,孙又生子,怎么搞?
bool DB_command::del_list(int user_id, int pwd, int dir_id, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select * from FileMsg where owner_id = %d and dir_id = %d", user_id, dir_id);
		if (!My_DB->return_serch_num) {
			sprintf(sql, "select * from DirMsg where grade = %d", dir_id);
			if (!My_DB->return_serch_num) {
				sprintf(sql, "delete from DirMsg where dir_id = %d", dir_id);
				My_DB->excute(sql);
				DB_Mutex->unlock();
				delete sql;
				return true;
			}
			else {
				DB_Mutex->unlock();
				strcpy(err, "Directs residual!");
				delete sql;
				return false;
			}
		}
		else {
			DB_Mutex->unlock();
			strcpy(err, "Files residual!");
			delete sql;
			return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}
}

//查
bool DB_command::login_check(int user_id, int pwd) {	//哈哈,这个不能上锁
	char* sql = new char[120];
	sprintf(sql, "select pwd from UserMsg where id = %d", user_id);
	int ret = My_DB->ireturn_serch(sql,1);
	delete sql;
	if (pwd == ret)
		return true;
	else
		return false;
}

int DB_command::login(int user_id, int pwd, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select dir_id from UserMsg where id = %d", user_id);
		int ret = My_DB->ireturn_serch(sql, 1);
		DB_Mutex->unlock();
		delete sql;
		return ret;
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Login defeat!");
		return false;
	}
}

int DB_command::pwd(int user_id, char* tel) {
	char* sql = new char[120];
	sprintf(sql, "select pwd from UserMsg where id = %d and tel = '%s'", user_id,tel);
	DB_Mutex->lock();
	int ret = My_DB->ireturn_serch(sql,1);
	DB_Mutex->unlock();
	delete sql;
	return ret;
}

void DB_command::file_list(int user_id, int pwd, int dir_id, char* ret, char* err){
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[150];
		sprintf(sql, "select file_id,file_name,FileMsg.[dir_id],dir_name from FileMsg,DirMsg where FileMsg.[owner_id] = %d and FileMsg.[dir_id] = DirMsg.[dir_id]", user_id);

		My_DB->ret_any(sql, ret);
		DB_Mutex->unlock();
		delete sql;
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
	}
}

bool DB_command::download(int user_id, int pwd, char* file_name, int dir_id,int shared, char* err) {
	char* sql = new char[120];
	
	if (shared != 0) {
		sprintf(sql, "select shared from FileMsg where file_name = '%s' and owner_id = %d", file_name,user_id);
		DB_Mutex->lock();
		int ret = My_DB->ireturn_serch(sql,1);
		DB_Mutex->unlock();
		if (shared == ret) {
			delete sql;
			return true;
		}		
	}
	else {
		DB_Mutex->lock();
		if (login_check(user_id, pwd)) {
			sprintf(sql, "select file_id from FileMsg where file_name = '%s' and dir_id = %d", file_name,dir_id);
			if (My_DB->return_serch_num) {
				DB_Mutex->unlock();
				delete sql;
				return true;
			}		
			else {
				DB_Mutex->unlock();
				strcpy(err, "Can't find file!");
				delete sql;
				return false;
			}		
		}
		else {
			DB_Mutex->unlock();
			strcpy(err, "Have no right!");
			delete sql;
			return false;
		}
	}
}

//改
bool DB_command::cpwd(int user_id, int old_pwd, int new_id, char* err) {
	char* sql = new char[120];
	sprintf(sql, "update UserMsg set pwd = %d where id = %d and pwd = %d", user_id, old_pwd, new_id);
	DB_Mutex->lock();
	int ret = My_DB->excute(sql);
	DB_Mutex->unlock();
	delete sql;
	return ret;
}

bool DB_command::cfile_addr(int user_id, int pwd, char* file_name, int old_dir, int new_dir, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select owner_id from DirMsg where dir_id = %d", new_dir);

		int id = My_DB->ireturn_serch(sql,1);
		int ret = 0;
		if (user_id == id) {
			sprintf(sql, "select owner_id from FileMsg where file_name = '%s'", file_name);
			ret = My_DB->ireturn_serch(sql, 1);
			if (user_id == id) {
				sprintf(sql, "update FileMsg set dir_id = %d where owner_id = %d and dir_id = %d and file_name = '%s'", new_dir, user_id, old_dir, file_name);
				My_DB->excute(sql);
				DB_Mutex->unlock();
				delete sql;
				return true;
			}
			else {
				DB_Mutex->unlock();
				strcpy(err, "Have no right!");
				delete sql;
				return false;
			}
		}
		else {
			DB_Mutex->unlock();
			strcpy(err, "Dir is not exit!");
			delete sql;
			return false;
		}	
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}
}

bool DB_command::cdir_addr(int user_id, int pwd, char* dir_name, int new_dir, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[150];
		sprintf(sql, "select grade from DirMsg where dir_id = %d and owner_id = (select owner_id from DirMsg where dir_name = '%s')",new_dir,dir_name);
		if (My_DB->return_serch_num == 1) {
			sprintf(sql, "update DirMsg set where grade = %d", new_dir);
			My_DB->excute(sql);
			DB_Mutex->unlock();
			delete sql;
			return true;		
		}
		else {
			DB_Mutex->unlock();
			strcpy(err, "Have no right!");
			delete sql;
			return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}		
}

bool DB_command::share_file(int user_id, int pwd, int shared,char* file_name,int dir_id, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select owner_id from FileMsg where file_name = '%s'",file_name);
		int id = My_DB->ireturn_serch(sql, 1);
		if (user_id == id) {
			sprintf(sql, "update FileMsg set shared = %d where file_name = '%s' and dir_id = %d", shared, file_name, dir_id);
			My_DB->excute(sql);
			DB_Mutex->unlock();
			delete sql;
			return true;
		}
		else {
			DB_Mutex->unlock();
			strcpy(err, "Have no right!");
			delete sql;
			return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}	
}

bool DB_command::cfile_name(int user_id, int pwd, char* file_name, int file_id,int dir_id, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select owner_id from FileMsg where file_id = %d", file_id);
		int id = My_DB->ireturn_serch(sql, 1);
		if (user_id == id) {
			sprintf(sql, "update FileMsg set file_name = '%s' where file_id = %d and dir_id = %d", file_name, file_id, dir_id);
			My_DB->excute(sql);
			DB_Mutex->unlock();
			delete sql;
			return true;
		}
		else {
			DB_Mutex->unlock();
			strcpy(err, "Have no right!");
			delete sql;
			return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}
}

bool DB_command::cdir_name(int user_id, int pwd, char* dir_name, int dir_id, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select owner_id from DirMsg where dir_name = '%s'", dir_name);
		int id = My_DB->ireturn_serch(sql, 1);
		if (user_id == id) {
			sprintf(sql, "update DirMsg set dir_name = '%s' where dir_id = %d", dir_name, dir_id);
			My_DB->excute(sql);
			DB_Mutex->unlock();
			delete sql;
			return true;
		}
		else {
			DB_Mutex->unlock();
			strcpy(err, "Have no right!");
			delete sql;
			return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}
}

char* DB_command::dir(int dir_id) {
	char* sql = new char[80];
	char* ret = new char[60];
	strcpy(sql, "select dir_name from DirMsg where dir_id = %d", dir_id);
	DB_Mutex->lock();
	ret = My_DB->creturn_serch(sql, 1);
	DB_Mutex->unlock();
	return ret;
}

char* DB_command::file(int file_id) {
	char* sql = new char[80];
	char* ret = new char[60];
	strcpy(sql, "select file_name from FileMsg where file_id = %d", file_id);
	DB_Mutex->lock();
	ret = My_DB->creturn_serch(sql, 1);
	DB_Mutex->unlock();
	return ret;
}

接下来是为DB服务器与FTP服务器对接而设计的包

代码语言:javascript复制
//Packet3.h

#pragma once

typedef struct CF {
	char cip[16];
	char filename[60];	
}cf_t;	//Client to FTP

typedef struct FC {
	char cip[16];
	char filename[60];	
}fc_t;	//FTO to Client

typedef struct DF {
	char filename[60];
}df_t;	//Delete File

typedef struct MF {
	char oldpath[60];
	char newpath[60];
}mf_t;	//Move File Path

typedef struct CFN {
	char oldname[60];
	char newname[60];
}cfn_t;	//Change File Name


//Ŀ¼²¿·Ö
typedef struct ND {
	char dirname[60];
}nd_t;	//New Direct 

typedef struct DD {
	char dirname[60];
}dd_t;	//Delete Direct

typedef struct MD {
	char olddir[60];
	char newdir[60];
}md_t;	//Move Direct

typedef struct CDN {
	char olddir[60];
	char newdir[60];
}cdn_t;	//Change Direct Name
代码语言:javascript复制
//PacketCommand3.h
#pragma once
//用于DB服务器向FTP服务器发包
#include "PacketBase.h"
#include "Packet3.h"

class PacketCommand3 :
	public PacketBase
{
public:
	bool replyCF(char* filename,char* cip);
	bool replyFC(char* filename,char* cip);
	bool replyDF(char* filename);
	bool replyMF(char* oldpath, char* newpath);
	bool replyCFN(char* oldpath, char* newpath);
	bool replyND(char* diename);
	bool replyDD(char* dirname);
	bool replyMD(char* oldpath, char* newpath);
	bool replyCDN(char* oldpath, char* newpath);
};
代码语言:javascript复制
//PacketCommand3.cpp

#include "PacketCommand3.h"

bool PacketCommand3::replyCF(char* filename, char* cip) {
    int sz = sizeof(cf_t);
    this->setBodySize(sz);
    this->Body = new char[sz];

    Head.funcId = 0x12;
    Head.optid = 0x01;
    Head.usrlenth = sz;
    Head.syn = 0x04;
    strcpy(Head.to_fd, "FTP");

    cf_t* body = (cf_t*)Body;
    strcpy(body->filename, filename);
    strcpy(body->cip, cip); //到时候大不了转回string

    Tail.pack_tail = 0x05;

    return this->pack();
}

bool PacketCommand3::replyFC(char* filename, char* cip) {
    int sz = sizeof(fc_t);
    this->setBodySize(sz);
    this->Body = new char[sz];

    Head.funcId = 0x13;
    Head.optid = 0x01;
    Head.usrlenth = sz;
    Head.syn = 0x04;
    strcpy(Head.to_fd, "FTP");

    fc_t* body = (fc_t*)Body;
    strcpy(body->filename, filename);
    strcpy(body->cip, cip); //到时候大不了转回string

    Tail.pack_tail = 0x05;

    return this->pack();
}

bool PacketCommand3::replyDF(char* filename) {
    int sz = sizeof(df_t);
    this->setBodySize(sz);
    this->Body = new char[sz];

    Head.funcId = 0x17;
    Head.optid = 0x01;
    Head.usrlenth = sz;
    Head.syn = 0x04;
    strcpy(Head.to_fd, "FTP");

    df_t* body = (df_t*)Body;
    strcpy(body->filename, filename);

    Tail.pack_tail = 0x05;

    return this->pack();
}
bool PacketCommand3::replyMF(char* oldpath, char* newpath) {
    int sz = sizeof(mf_t);
    this->setBodySize(sz);
    this->Body = new char[sz];

    Head.funcId = 0x18;
    Head.optid = 0x01;
    Head.usrlenth = sz;
    Head.syn = 0x04;
    strcpy(Head.to_fd, "FTP");

    mf_t* body = (mf_t*)Body;
    strcpy(body->oldpath, oldpath);
    strcpy(body->newpath, newpath);

    Tail.pack_tail = 0x05;

    return this->pack();
}

bool PacketCommand3::replyCFN(char* oldpath, char* newpath) {
    int sz = sizeof(cfn_t);
    this->setBodySize(sz);
    this->Body = new char[sz];

    Head.funcId = 0x20;
    Head.optid = 0x01;
    Head.usrlenth = sz;
    Head.syn = 0x04;
    strcpy(Head.to_fd, "FTP");

    cfn_t* body = (cfn_t*)Body;
    strcpy(body->oldname, oldpath);
    strcpy(body->oldname, newpath);

    Tail.pack_tail = 0x05;

    return this->pack();
}

bool PacketCommand3::replyND(char* diename) {
    int sz = sizeof(nd_t);
    this->setBodySize(sz);
    this->Body = new char[sz];

    Head.funcId = 0x15;
    Head.optid = 0x01;
    Head.usrlenth = sz;
    Head.syn = 0x04;
    strcpy(Head.to_fd, "FTP");

    nd_t* body = (nd_t*)Body;
    strcpy(body->dirname, diename);

    Tail.pack_tail = 0x05;

    return this->pack();
}

bool PacketCommand3::replyDD(char* dirname) {
    int sz = sizeof(dd_t);
    this->setBodySize(sz);
    this->Body = new char[sz];

    Head.funcId = 0x16;
    Head.optid = 0x01;
    Head.usrlenth = sz;
    Head.syn = 0x04;
    strcpy(Head.to_fd, "FTP");

    dd_t* body = (dd_t*)Body;
    strcpy(body->dirname, dirname);

    Tail.pack_tail = 0x05;

    return this->pack();
}

bool PacketCommand3::replyMD(char* oldpath, char* newpath) {
    int sz = sizeof(md_t);
    this->setBodySize(sz);
    this->Body = new char[sz];

    Head.funcId = 0x19;
    Head.optid = 0x01;
    Head.usrlenth = sz;
    Head.syn = 0x04;
    strcpy(Head.to_fd, "FTP");

    md_t* body = (md_t*)Body;
    strcpy(body->olddir, oldpath);
    strcpy(body->newdir, newpath);

    Tail.pack_tail = 0x05;

    return this->pack();
}
bool PacketCommand3::replyCDN(char* oldpath, char* newpath) {
    int sz = sizeof(cdn_t);
    this->setBodySize(sz);
    this->Body = new char[sz];

    Head.funcId = 0x22;
    Head.optid = 0x01;
    Head.usrlenth = sz;
    Head.syn = 0x04;
    strcpy(Head.to_fd, "FTP");

    cdn_t* body = (cdn_t*)Body;
    strcpy(body->olddir, oldpath);
    strcpy(body->newdir, newpath);

    Tail.pack_tail = 0x05;

    return this->pack();
}

接下来是中间操控的主类:

代码语言:javascript复制
//BackServer.h
#pragma once

#include"BtoC.h"
#include"Pthread_Pool.h"
#include"PacketCommand1.h"
#include"PacketCommand3.h"
#include"DB_command.h"

#include<stdlib.h>
#include<string.h>
#include <stdio.h>
#include <iostream>
using namespace std;

class DoTask : public Task
{
public:
    DoTask(BtoC& send, PacketCommand1& packet);

    int run();
private:
    DB_command* task_db;
    BtoC* m_send;
    PacketCommand1 m_packet;
    PacketCommand3* f_packet;
};


class BackServer
{
public:
    BackServer(char* IPnum);
    ~BackServer() {}
    int run();
private:
    PacketCommand1 m_packet;
    BtoC m_send;
    Pthread_Pool* m_pool;
};
代码语言:javascript复制
//BackServer.cpp
#include "BackServer.h"

BackServer::BackServer(char* IPnum)
{
    m_pool = new Pthread_Pool(50, 30, 5);
    m_send.Start(IPnum);
    char* name = new char[6];
    strcpy(name, "DB");
    m_send.send_name(name);
    delete name;
}

int BackServer::run()
{
    int n = 0;
    while (1)
    {
        n = m_send.Read_date(m_packet.getData());
        m_packet.setSize(n);
        DoTask* t = new DoTask(m_send, m_packet);
        m_pool->addTask(t);
    }
    return 0;
}

DoTask::DoTask(BtoC& send, PacketCommand1& packet)
    :m_send(&send), m_packet(packet)
{
    task_db = new DB_command();
    f_packet = new PacketCommand3();
}

int DoTask::run()
{
    printf("task get data n");
    int tru = 0;
    //1. unpack
    m_packet.unpack();

    //2. deal the packet by funid
    void* pData = m_packet.getBody();
    char* err = new char[40];

    switch (m_packet.getHead()->funcId)
    {
    case 0x01://登陆
    {
        err = new char[40];
        Login_t* plogin = (Login_t*)(pData);

        tru = task_db->login(plogin->id, plogin->pwd,err);
     
        m_packet.replyLogin(tru, tru,err, m_packet.getHead()->fd);

        m_send->Write_date(m_packet.getData(), m_packet.getSize());

    }break;
    case 0x02://注册
    {
        Register_t* pregister = (Register_t*)(pData);

        tru = task_db->add_user(pregister->id, pregister->pwd, pregister->tel,err);

        m_packet.replyRegist(tru,err,m_packet.getHead()->fd);

        m_send->Write_date(m_packet.getData(), m_packet.getSize());
    }break;
    case 0x03://找回密码
    {
        F_Pwd_t* Fpwd = (F_Pwd_t*)(pData);

        tru = task_db->pwd(Fpwd->id, Fpwd->tel);

        m_packet.replyFpwd(tru, tru, m_packet.getHead()->fd);

        m_send->Write_date(m_packet.getData(), m_packet.getSize());
    }
    case 0x04://修改密码
    {
        c_pwd_t* pc_pwd = (c_pwd_t*)(pData);

        tru = task_db->cpwd(pc_pwd->id, pc_pwd->pwd, pc_pwd->newpwd,err);

        m_packet.replyCpwd(tru,err,m_packet.getHead()->fd);

        m_send->Write_date(m_packet.getData(), m_packet.getSize());
    }break;
    case 0x11:  //获取文件列表
    {
        FileList_t* pfl = (FileList_t*)(pData);
        char* getList = new char[800];
        
        task_db->file_list(pfl->user_id, pfl->pwd, pfl->dir_id, getList, err);

        m_packet.replyFileList(err, getList, m_packet.getHead()->fd);

        m_send->Write_date(m_packet.getData(), m_packet.getSize());
    }break;
    case 0x12:  //上传文件
    {
        UpFile_t* pul = (UpFile_t*)(pData);

        tru = task_db->add_file(pul->file_name, pul->user_id, pul->pwd, pul->mode, pul->dir_id, pul->force, err);

        //不论成功与否,客户端都应该知道
        m_packet.replyAddList(tru, err, m_packet.getHead()->fd);
        m_send->Write_date(m_packet.getData(), m_packet.getSize());

        //如果成功,那就发往FTP处理
        if (true == tru) {
            f_packet->replyCF(pul->file_name, pul->ip);
            m_send->Write_date(f_packet->getData(),f_packet->getSize());
        }
    }break;
    case 0x13:  //下载文件
    {
        Download_t* pdf = (Download_t*)(pData);

        tru = task_db->download(pdf->user_id, pdf->pwd, pdf->file_name, pdf->user_id, pdf->shared, err);

        //不论成功与否,客户端都应该知道
        m_packet.replyDownFile(tru, err, m_packet.getHead()->fd);
        m_send->Write_date(m_packet.getData(), m_packet.getSize());

        //如果成功,那就发往FTP处理
        if (true == tru){
            f_packet->replyFC(pdf->file_name, pdf->ip);
            m_send->Write_date(f_packet->getData(), f_packet->getSize());
        }
    }break;
    case 0x14:  //共享文件
    {
        C_mode_t* pcm = (C_mode_t*)(pData);

        tru = task_db->share_file(pcm->id, pcm->pwd, pcm->share_num, pcm->file_name, pcm->dir_id, err);

        m_packet.replyShareFile(tru, err, m_packet.getHead()->fd);
        m_send->Write_date(m_packet.getData(), m_packet.getSize());
    }break;
    case 0x15:  //新增目录
    {
        A_filelist_t* pafl = (A_filelist_t*)(pData);

        tru = task_db->add_list(pafl->id, pafl->pwd, pafl->father_dir, pafl->dirname, err);

        if (true == tru) {
            f_packet->replyND(pafl->dirname);
            m_send->Write_date(f_packet->getData(), f_packet->getSize());
        }
        else {
            m_packet.replyAddList(tru, err, m_packet.getHead()->fd);
            m_send->Write_date(m_packet.getData(), m_packet.getSize());
        }
    }break;
    case 0x16:  //删除目录
    {
        D_list_t* pdl = (D_list_t*)(pData);

        tru = task_db->del_list(pdl->id, pdl->pwd, pdl->dir_id, err);

        if (true == tru) {
            char* name = new char[60];
            name = task_db->dir(pdl->dir_id);
            f_packet->replyDD(name);
            m_send->Write_date(f_packet->getData(), f_packet->getSize());
        }
        else {
            m_packet.replyDelDir(tru, err, m_packet.getHead()->fd);
            m_send->Write_date(m_packet.getData(), m_packet.getSize());
        }
    }break;
    case 0x17:  //删除文件
    {
        D_file_t* pdfl = (D_file_t*)(pData);

        tru = task_db->del_file(pdfl->id, pdfl->pwd, pdfl->file_name, pdfl->dir_id, err);

        if (true == tru) {
            f_packet->replyDF(pdfl->file_name);
            m_send->Write_date(f_packet->getData(), f_packet->getSize());
        }
        else {
            m_packet.replyDelFile(tru, err, m_packet.getHead()->fd);
            m_send->Write_date(m_packet.getData(), m_packet.getSize());
        }
    }break;
    case 0x18:  //文件移动
    {
        C_filelist_t* pcf = (C_filelist_t*)(pData);

        tru = task_db->cfile_addr(pcf->id, pcf->pwd, pcf->file_name,pcf->old_dir,pcf->new_dir,err);

        if (true == tru) {
            char* oldname = new char[60];
            char* newname = new char[60];
            oldname = task_db->dir(pcf->old_dir);
            newname = task_db->dir(pcf->new_dir);
            f_packet->replyMF(oldname,newname);   //这里,需要有id向名字转换的数据库操作函数
            m_send->Write_date(f_packet->getData(), f_packet->getSize());
        }
        else {
            m_packet.replyMoveFile(tru, err, m_packet.getHead()->fd);
            m_send->Write_date(m_packet.getData(), m_packet.getSize());
        }
    }break;
    case 0x19:  //目录移动
    {
        C_Dirlist_t* pcl = (C_Dirlist_t*)(pData);

        tru = task_db->cdir_addr(pcl->id, pcl->pwd,pcl->dir_name,pcl->new_father, err);

        if (true == tru) {
            char* name = new char[60];
            name = task_db->dir(pcl->new_father);
            f_packet->replyMD(pcl->dir_name,name);   //这里
            m_send->Write_date(f_packet->getData(), f_packet->getSize());
        }
        else {
            m_packet.replyMoveDir(tru, err, m_packet.getHead()->fd);
            m_send->Write_date(m_packet.getData(), m_packet.getSize());
        }
    }break;
    case 0x20:  //文件重命名
    {
        C_fileName_t* pfn = (C_fileName_t*)(pData);

        tru = task_db->cfile_name(pfn->user_id, pfn->pwd, pfn->filename, pfn->file_id,pfn->dir_id,err);

        if (true == tru) {
            char* name = new char[60];
            name = task_db->file(pfn->file_id);
            f_packet->replyCFN(name,pfn->filename);   //这里
            m_send->Write_date(f_packet->getData(), f_packet->getSize());
        }
        else {
            m_packet.replyChangeFileName(tru, err, m_packet.getHead()->fd);
            m_send->Write_date(m_packet.getData(), m_packet.getSize());
        }
    }break;
    case 0x22:  //目录重命名
    {
        C_dirName_t* pdn = (C_dirName_t*)(pData);

        tru = task_db->cdir_name(pdn->user_id, pdn->pwd, pdn->dirname, pdn->dir_id,err);

        if (true == tru) {
            char* name = new char[60];
            name = task_db->dir(pdn->dir_id);
            f_packet->replyCDN(name, pdn->dirname);   //这里
            m_send->Write_date(f_packet->getData(), f_packet->getSize());
        }
        else {
            m_packet.replyChangeDirName(tru, err, m_packet.getHead()->fd);
            m_send->Write_date(m_packet.getData(), m_packet.getSize());
        }
    }break;
    return 0;
    }

场景类相对来说就很简单

代码语言:javascript复制
//BackCommand.cpp
#include"BackServer.h"

int main(int argc, char* argv[]) {
	BackServer* bk = new BackServer(argv[1]);
	bk->run();
}
ftp

0 人点赞