#include "request_handler.h" #include "netdisk_factory.h" extern SafeQueue task_queue; extern std::map userDeviceMap; extern LoginStore loginStore; static inline void fatal(const char *func, int rv) { fprintf(stderr, "%s: %s\n", func, nng_strerror(rv)); //exit(1); } int handleLogin(nng_socket sock, Json::Value request) { std::cout << "===accepted login request" << std::endl; int rv; char rmsg[MAXLINE]; strcpy(rmsg, "success"); // char *buf; Netdisk *netdisk = NULL; Json::Value arguments = request["arguments"]; //登录 Netdisk_LoginInfo loginInfo; loginInfo.loginUUID=arguments["loginUUID"].asString(); loginInfo.deviceType = arguments["deviceType"].asString(); loginInfo.host = arguments["host"].asString(); loginInfo.port = arguments["port"].asInt(); loginInfo.username = arguments["username"].asString(); loginInfo.password = arguments["password"].asString(); std::map::iterator userDeviceIter = userDeviceMap.find(loginInfo.loginUUID); if( userDeviceIter != userDeviceMap.end() ) { netdisk = userDeviceIter->second; } if (netdisk == NULL) { netdisk = NetdiskFacotory::create(loginInfo.deviceType); if(netdisk != NULL) { userDeviceMap.insert({loginInfo.loginUUID, netdisk}); } else { //rvout << "无法识别的设备类型:" << loginInfo.deviceType.c_str(); snprintf(rmsg, MAXLINE, "无法识别的设备类型: %s", loginInfo.deviceType.c_str()); } } rv = netdisk->login(loginInfo); if (rv == 0) { loginStore.saveLoginInfo(loginInfo); // std::cout << "起始通道:" << netdisk->getDeviceInfo().startChannel << ", 最大通道号:" << netdisk->getDeviceInfo().maxChannels << std::endl; } Json::Value response; Json::Value payload; payload["loginUUID"] = loginInfo.loginUUID; response["code"] = rv; response["msg"] = rmsg; response["payload"] = payload; const std::string str = response.toStyledString(); // nng内部会释放buf rv = nng_send(sock, strdup(str.c_str()), str.length(), NNG_FLAG_ALLOC); //free(buf); if (rv != 0) { fatal("nng_send", rv); return rv; } return 0; } int handleLogout(nng_socket sock, Json::Value request) { std::cout << "===accepted logout request" << std::endl; int rv; char rmsg[MAXLINE]; strcpy(rmsg, "success"); // char *buf; Netdisk *netdisk = NULL; Json::Value arguments = request["arguments"]; std::string loginUUID=arguments["loginUUID"].asString(); Netdisk_LoginInfo loginInfo = loginStore.getLoginInfo(loginUUID); std::map::iterator userDeviceIter = userDeviceMap.find(loginUUID); if( userDeviceIter != userDeviceMap.end() ) { netdisk = userDeviceIter->second; } if (netdisk == NULL) { netdisk = NetdiskFacotory::create(loginInfo.deviceType); if(netdisk != NULL) { userDeviceMap.insert({loginInfo.loginUUID, netdisk}); } else { //rvout << "无法识别的设备类型:" << loginInfo.deviceType.c_str(); snprintf(rmsg, MAXLINE, "无法识别的设备类型: %s", loginInfo.deviceType.c_str()); } } rv = netdisk->logout(); Json::Value response; response["code"] = rv; response["msg"] = rmsg; Json::Value payload; payload["loginUUID"] = loginInfo.loginUUID; response["payload"] = payload; const std::string str = response.toStyledString(); // nng内部会释放buf rv = nng_send(sock, strdup(str.c_str()), str.length(), NNG_FLAG_ALLOC); if (rv != 0) { fatal("nng_send", rv); return rv; } return 0; } int handleGetDeviceInfo(nng_socket sock, Json::Value request) { std::cout << "===accepted getDeviceInfo request" << std::endl; int rv; char rmsg[MAXLINE]; strcpy(rmsg, "success"); // char *buf; Netdisk *netdisk = NULL; DeviceInfo deviceInfo; Json::Value arguments = request["arguments"]; std::string loginUUID=arguments["loginUUID"].asString(); Netdisk_LoginInfo loginInfo = loginStore.getLoginInfo(loginUUID); std::map::iterator userDeviceIter = userDeviceMap.find(loginUUID); if( userDeviceIter != userDeviceMap.end() ) { netdisk = userDeviceIter->second; } if (netdisk == NULL) { netdisk = NetdiskFacotory::create(loginInfo.deviceType); if(netdisk != NULL) { userDeviceMap.insert({loginInfo.loginUUID, netdisk}); } else { //rvout << "无法识别的设备类型:" << loginInfo.deviceType.c_str(); snprintf(rmsg, MAXLINE, "无法识别的设备类型: %s", loginInfo.deviceType.c_str()); } } if ( (rv = netdisk->login(loginInfo)) != 0 ) { snprintf(rmsg, MAXLINE, "请重新登录"); } else { deviceInfo = netdisk->getDeviceInfo(); } Json::Value response; response["code"] = rv; response["msg"] = rmsg; Json::Value payload; payload["loginUUID"] = loginUUID; Json::Value jsonDeviceInfo; jsonDeviceInfo["startChannel"] = deviceInfo.startChannel; jsonDeviceInfo["maxChannels"] = deviceInfo.maxChannels; payload["deviceInfo"] = jsonDeviceInfo; response["payload"] = payload; const std::string str = response.toStyledString(); // nng内部会释放buf rv = nng_send(sock, strdup(str.c_str()), str.length(), NNG_FLAG_ALLOC); if (rv != 0) { fatal("nng_send", rv); return rv; } return 0; } int handleDownloadByTimeAsync(nng_socket sock, Json::Value request) { std::cout << "received downloadByTime request" << std::endl; int rv; char rmsg[MAXLINE]; strcpy(rmsg, "received downloadByTime request"); // char *buf; Json::Value arguments = request["arguments"]; Netdisk_DownloadRequest drequest; drequest.loginUUID = arguments["loginUUID"].asString(); Json::Value start = arguments["start"]; drequest.start.tm_year = start["year"].asInt()-1900; // 这个时间类型从1900开始算作第一年 drequest.start.tm_mon = start["mon"].asInt()-1; // 0是第一个月 drequest.start.tm_mday = start["day"].asInt(); drequest.start.tm_hour = start["hour"].asInt(); drequest.start.tm_min = start["min"].asInt(); drequest.start.tm_sec = start["sec"].asInt(); Json::Value end = arguments["end"]; drequest.end.tm_year = end["year"].asInt()-1900; // 这个时间类型从1900开始算作第一年 drequest.end.tm_mon = end["mon"].asInt()-1; // 0是第一个月 drequest.end.tm_mday = end["day"].asInt(); drequest.end.tm_hour = end["hour"].asInt(); drequest.end.tm_min = end["min"].asInt(); drequest.end.tm_sec = end["sec"].asInt(); drequest.channel = arguments["channel"].asInt(); drequest.destpath = arguments["destpath"].asString(); task_queue.push(drequest); Json::Value response; response["code"] = 0; response["msg"] = rmsg; std::string str = response.toStyledString(); std::cout << str << std::endl; if ((rv = nng_send(sock, strdup(str.c_str()), str.length(), NNG_FLAG_ALLOC)) != 0) { fatal("nng_send", rv); return rv; } return 0; } int handleDownloadByTime(nng_socket sock, Json::Value request) { std::cout << "accepted handleDownloadByTime request" << std::endl; int rv, code; // char *buf; Netdisk *netdisk = NULL; Json::Value arguments = request["arguments"]; Netdisk_DownloadRequest drequest; drequest.loginUUID = arguments["loginUUID"].asString(); Json::Value start = arguments["start"]; drequest.start.tm_year = start["year"].asInt()-1900; // 这个时间类型从1900开始算作第一年 drequest.start.tm_mon = start["mon"].asInt()-1; // 0是第一个月 drequest.start.tm_mday = start["day"].asInt(); drequest.start.tm_hour = start["hour"].asInt(); drequest.start.tm_min = start["min"].asInt(); drequest.start.tm_sec = start["sec"].asInt(); Json::Value end = arguments["end"]; drequest.end.tm_year = end["year"].asInt()-1900; // 这个时间类型从1900开始算作第一年 drequest.end.tm_mon = end["mon"].asInt()-1; // 0是第一个月 drequest.end.tm_mday = end["day"].asInt(); drequest.end.tm_hour = end["hour"].asInt(); drequest.end.tm_min = end["min"].asInt(); drequest.end.tm_sec = end["sec"].asInt(); drequest.channel = arguments["channel"].asInt(); drequest.destpath = arguments["destpath"].asString(); std::map::iterator userDeviceIter = userDeviceMap.find( drequest.loginUUID); if( userDeviceIter != userDeviceMap.end() ) { netdisk = userDeviceIter->second; } Netdisk_LoginInfo loginInfo = loginStore.getLoginInfo(drequest.loginUUID); if (netdisk == NULL) { netdisk = NetdiskFacotory::create(loginInfo.deviceType); if(netdisk != NULL) { userDeviceMap.insert({loginInfo.loginUUID, netdisk}); } else { err_msg(0, "无法识别的设备类型: %s", loginInfo.deviceType.c_str()); } } if ( (code = netdisk->login(loginInfo)) != 0 ) { printf("下载登录失败\n"); } std::vector files; if ( (code = netdisk->downloadByTime(drequest, &files) ) != 0) { printf("下载失败\n"); } Json::Value response; Json::Value payload; response["code"] = code; Json::Value filelist; for(std::string f : files) { filelist.append(f); } payload["filelist"] = filelist; response["payload"] = payload; std::string str = response.toStyledString(); std::cout << str << std::endl; rv = nng_send(sock, strdup(str.c_str()), str.length(), NNG_FLAG_ALLOC); if (rv != 0) { fatal("nng_send", rv); return rv; } return 0; }