From c6a302283cee53d113d88db89c0a242576dbc8c4 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期六, 13 六月 2020 10:15:50 +0800 Subject: [PATCH] 修改包路径 --- service/netdisk_service.c | 85 ++++++++++++++++++++++++------------------ 1 files changed, 49 insertions(+), 36 deletions(-) diff --git a/service/netdisk_service.c b/service/netdisk_service.c index c71a014..5e322dc 100644 --- a/service/netdisk_service.c +++ b/service/netdisk_service.c @@ -4,14 +4,13 @@ #include "login_store.h" #include "request_handler.h" #include "properties_config.h" +#include "netdisk_factory.h" #include <jsoncpp/json/json.h> #include <nng/nng.h> #include <nng/protocol/reqrep0/rep.h> #include <nng/protocol/reqrep0/req.h> using namespace std; - - static int work(Netdisk_DownloadRequest drequest); static int connectAndSend(const char *url, char * str); @@ -23,7 +22,7 @@ PropertiesConfig config("../data/config.txt"); -SafeQueue<Netdisk_DownloadRequest> task_queue(10); +SafeQueue<Netdisk_DownloadRequest> task_queue(100); std::map<std::string, Netdisk *> userDeviceMap; @@ -70,45 +69,57 @@ int work(Netdisk_DownloadRequest drequest) { Netdisk *netdisk = NULL; + std::vector<std::string> files; int rv; + char rmsg[MAXLINE]; + strcpy(rmsg, "success"); + Netdisk_LoginInfo loginInfo = loginStore.getLoginInfo(drequest.loginUUID); std::map<std::string, Netdisk *>::iterator userDeviceIter = userDeviceMap.find( drequest.loginUUID); if( userDeviceIter != userDeviceMap.end() ) { netdisk = userDeviceIter->second; } - Netdisk_LoginInfo loginInfo = loginStore.getLoginInfo(drequest.loginUUID); if (netdisk == NULL) { - - if(loginInfo.deviceType == "HC") { - // std::cout << "new HCNetdisk" << std::endl; - netdisk = new HCNetdisk(); + + netdisk = NetdiskFacotory::create(loginInfo.deviceType); + if(netdisk != NULL) { userDeviceMap.insert({loginInfo.loginUUID, netdisk}); } else { - err_msg(0, "鏃犳硶璇嗗埆鐨勮澶囩被鍨嬶細 %s", loginInfo.deviceType.c_str()); - + snprintf(rmsg, MAXLINE, "鏃犳硶璇嗗埆鐨勮澶囩被鍨嬶細 %s", loginInfo.deviceType.c_str()); } } if ( (rv = netdisk->login(loginInfo)) != 0 ) { - printf("涓嬭浇鐧诲綍澶辫触\n"); + snprintf(rmsg, MAXLINE, "璇烽噸鏂扮櫥褰�"); + } else if ( (rv = netdisk->downloadByTime(drequest, &files) ) != 0) { + snprintf(rmsg, MAXLINE, "涓嬭浇澶辫触"); } + - std::vector<std::string> files; - if ( (rv = netdisk->downloadByTime(drequest, &files) ) != 0) { - printf("涓嬭浇澶辫触\n"); - } + // Json::Value response; + // Json::Value payload; + // response["code"] = rv; + // response["msg"] = rmsg; - Json::Value response; - Json::Value payload; - response["rv"] = rv; + // Json::Value filelist; + // for(std::string f : files) { + // filelist.append(f); + // } + // payload["filelist"] = filelist; + // response["payload"] = payload; + Json::Value request; + request["method"] = "downloadByTimeCallBack"; + Json::Value arguments; Json::Value filelist; for(std::string f : files) { filelist.append(f); } - payload["filelist"] = filelist; - response["payload"] = payload; - std::string str = response.toStyledString(); + arguments["fileList"] = filelist; + arguments["loginUUID"] = drequest.loginUUID; + + request["arguments"] = arguments; + std::string str = request.toStyledString(); std::cout << "download finished, call back" << std::endl; std::cout << str << std::endl; @@ -124,6 +135,7 @@ { Netdisk_DownloadRequest request; task_queue.pop(request); + err_msg(0, "====take a task"); work(request); } } @@ -153,6 +165,7 @@ } jsonreader.parse(buf, request); + nng_free(buf, sz); std::string method = request["method"].asString(); std::map<std::string, RequestHandleFun>::iterator handleFunIter = requestHandleFunMap.find(method); @@ -162,23 +175,21 @@ } else { std::cerr << "Don't support " << method << std::endl; } - // if (method == "login") { - // handleLogin(sock, request); - // } else if (method == "downloadByTime") { - // handleDownloadByTimeAsync(sock, request); - // } else { - // std::cerr << "Don't support " << method << std::endl; - // } - // Unrecognized command, so toss the buffer. - nng_free(buf, sz); + + } } - +/** + * 娉ㄥ唽璇锋眰澶勭悊鐨勬柟娉� + */ void registRequestHandleFun() { requestHandleFunMap.insert({"login", handleLogin}); + requestHandleFunMap.insert({"logout", handleLogout}); requestHandleFunMap.insert({"downloadByTime", handleDownloadByTimeAsync}); + requestHandleFunMap.insert({"getDeviceInfo", handleGetDeviceInfo}); } + void initThreadPool() { pthread_t tid; @@ -188,15 +199,17 @@ int main() { - - Netdisk_EnvConfig hcEnvConfig; - hcEnvConfig.libpath = config.get("hclib"); - HCNetdisk::netdisk_init(&hcEnvConfig); - + //鐜鍙橀噺鍒濆鍖� WORKERS = config.getInt("workers"); localUrl = config.get("local_url"); remoteUrl = config.get("remote_url"); + //娴峰悍璁惧鐜鍒濆鍖� + Netdisk_EnvConfig hcEnvConfig; + hcEnvConfig.libpath = "../lib/hc"; + HCNetdisk::netdisk_init(&hcEnvConfig); + + registRequestHandleFun(); -- Gitblit v1.8.0