From 602442ecedf17800cddfdae188b05b40345cb011 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期五, 12 六月 2020 12:08:46 +0800 Subject: [PATCH] version --- common/include/usg_typedef.h | 5 service/Makefile | 5 service/request_handler.h | 4 service/netdisk_service | 0 test/test | 0 service/request_handler.c | 127 +++++++++++++++++++- device/include/netdisk.h | 16 +- service/netdisk_service.c | 42 ++++-- service/test_client | 0 test/Makefile | 2 service/core | 0 test/test.c | 8 + service/test_client.c | 103 +++++++++++++---- 13 files changed, 249 insertions(+), 63 deletions(-) diff --git a/common/include/usg_typedef.h b/common/include/usg_typedef.h index a3b9a81..55796e1 100644 --- a/common/include/usg_typedef.h +++ b/common/include/usg_typedef.h @@ -43,7 +43,10 @@ #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) - + +/* Misc constants */ +#define MAXLINE 1024 /* Max text line length */ +#define MAXBUF 8192 /* Max I/O buffer size */ #ifdef __cplusplus } diff --git a/device/include/netdisk.h b/device/include/netdisk.h index 1af6837..b2b3fb6 100644 --- a/device/include/netdisk.h +++ b/device/include/netdisk.h @@ -28,22 +28,20 @@ } Netdisk_DownloadRequest; - +typedef struct DeviceInfo +{ + int startChannel; //璧峰閫氶亾鍙� + int maxChannels; //鏈�澶ч�氶亾鏁� + +} DeviceInfo; // 缃戠粶纭洏 鎶借薄绫� class Netdisk { private: std::string loginDataFile; - struct DeviceInfo - { - int startChannel; //璧峰閫氶亾鍙� - int maxChannels; //鏈�澶ч�氶亾鏁� - - }; + protected: - - DeviceInfo deviceInfo; std::string deviceType; //璁惧绫诲瀷 std::string loginUUID; diff --git a/service/Makefile b/service/Makefile index 2e70b29..9fa2483 100644 --- a/service/Makefile +++ b/service/Makefile @@ -14,14 +14,15 @@ PLATFORM=$(shell $(ROOT)/systype.sh) include $(ROOT)/Make.defines.$(PLATFORM) -all: netdisk_service client test test_queue test_properties +all: netdisk_service test_client test test_queue test_properties -test_properties: properties_config.c netdisk_service: $(ROOT)/device/hcnetdisk.c $(ROOT)/device/netdisk_factory.c login_store.c request_handler.c properties_config.c test: $(ROOT)/device/hcnetdisk.c +test_properties: properties_config.c + clean: rm -f test *.o a.out core temp.* *.a *.so diff --git a/service/core b/service/core new file mode 100644 index 0000000..cdb796e --- /dev/null +++ b/service/core Binary files differ diff --git a/service/netdisk_service b/service/netdisk_service index 975f752..8d70d93 100755 --- a/service/netdisk_service +++ b/service/netdisk_service Binary files differ diff --git a/service/netdisk_service.c b/service/netdisk_service.c index bed648c..3612b21 100644 --- a/service/netdisk_service.c +++ b/service/netdisk_service.c @@ -69,35 +69,37 @@ 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) { 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["rv"] = rv; + response["code"] = rv; + response["msg"] = rmsg; Json::Value filelist; for(std::string f : files) { @@ -150,6 +152,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); @@ -160,15 +163,20 @@ std::cerr << "Don't support " << method << std::endl; } - 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; @@ -178,15 +186,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 = config.get("hclib"); + HCNetdisk::netdisk_init(&hcEnvConfig); + + registRequestHandleFun(); diff --git a/service/request_handler.c b/service/request_handler.c index 592bcd7..8bbd910 100644 --- a/service/request_handler.c +++ b/service/request_handler.c @@ -14,8 +14,10 @@ } int handleLogin(nng_socket sock, Json::Value request) { -std::cout << "accepted login request" << std::endl; - int rv, code; +std::cout << "===accepted login request" << std::endl; + int rv; + char rmsg[MAXLINE]; + strcpy(rmsg, "success"); // char *buf; Netdisk *netdisk = NULL; @@ -41,21 +43,24 @@ if(netdisk != NULL) { userDeviceMap.insert({loginInfo.loginUUID, netdisk}); } else { - err_msg(0, "鏃犳硶璇嗗埆鐨勮澶囩被鍨嬶細 %s", loginInfo.deviceType.c_str()); + + //rvout << "鏃犳硶璇嗗埆鐨勮澶囩被鍨嬶細" << loginInfo.deviceType.c_str(); + snprintf(rmsg, MAXLINE, "鏃犳硶璇嗗埆鐨勮澶囩被鍨嬶細 %s", loginInfo.deviceType.c_str()); } } - code = netdisk->login(loginInfo); - if (code == 0) { + rv = netdisk->login(loginInfo); + if (rv == 0) { loginStore.saveLoginInfo(loginInfo); - std::cout << "璧峰閫氶亾:" << netdisk->getDeviceInfo().startChannel << ", 鏈�澶ч�氶亾鍙凤細" << netdisk->getDeviceInfo().maxChannels << std::endl; + // std::cout << "璧峰閫氶亾:" << netdisk->getDeviceInfo().startChannel << ", 鏈�澶ч�氶亾鍙凤細" << netdisk->getDeviceInfo().maxChannels << std::endl; } Json::Value response; Json::Value payload; payload["loginUUID"] = loginInfo.loginUUID; - response["code"] = code; + response["code"] = rv; + response["msg"] = rmsg; response["payload"] = payload; const std::string str = response.toStyledString(); // nng鍐呴儴浼氶噴鏀綽uf @@ -68,11 +73,116 @@ 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<std::string, Netdisk *>::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鍐呴儴浼氶噴鏀綽uf + 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<std::string, Netdisk *>::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鍐呴儴浼氶噴鏀綽uf + 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 << "accepted handleDownloadByTime request" << std::endl; int rv; + char rmsg[MAXLINE]; + strcpy(rmsg, "received"); // char *buf; Json::Value arguments = request["arguments"]; @@ -103,6 +213,7 @@ 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) { @@ -110,8 +221,6 @@ return rv; } return 0; - - } int handleDownloadByTime(nng_socket sock, Json::Value request) { diff --git a/service/request_handler.h b/service/request_handler.h index da51eaf..704412c 100644 --- a/service/request_handler.h +++ b/service/request_handler.h @@ -11,5 +11,7 @@ typedef int (*RequestHandleFun)(nng_socket sock, Json::Value request); int handleLogin(nng_socket sock, Json::Value request); +int handleLogout(nng_socket sock, Json::Value request); int handleDownloadByTime(nng_socket sock, Json::Value request); -int handleDownloadByTimeAsync(nng_socket sock, Json::Value request); \ No newline at end of file +int handleDownloadByTimeAsync(nng_socket sock, Json::Value request); +int handleGetDeviceInfo(nng_socket sock, Json::Value request); \ No newline at end of file diff --git a/service/test_client b/service/test_client new file mode 100755 index 0000000..87a2890 --- /dev/null +++ b/service/test_client Binary files differ diff --git a/service/client.c b/service/test_client.c similarity index 68% rename from service/client.c rename to service/test_client.c index 170e788..368f641 100644 --- a/service/client.c +++ b/service/test_client.c @@ -43,6 +43,54 @@ } +void doGetDeviceInfo(nng_socket sock) { + char * buf = NULL; + int rv; + size_t sz; + Json::Value request; + request["method"] = "getDeviceInfo"; + Json::Value arguments; + arguments["loginUUID"] = "1"; + request["arguments"]= arguments; + + std::string str = request.toStyledString(); + + if ((rv = nng_send(sock, strdup(str.c_str()), str.length(), 0)) != 0) { + fatal("nng_send", rv); + } + if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) { + fatal("nng_recv", rv); + } + std::cout << buf; + nng_free(buf, sz); + + +} + +void doLogout(nng_socket sock) { + char * buf = NULL; + int rv; + size_t sz; + Json::Value request; + request["method"] = "logout"; + Json::Value arguments; + arguments["loginUUID"] = "1"; + request["arguments"]= arguments; + + std::string str = request.toStyledString(); + + if ((rv = nng_send(sock, strdup(str.c_str()), str.length(), 0)) != 0) { + fatal("nng_send", rv); + } + if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) { + fatal("nng_recv", rv); + } + std::cout << buf; + nng_free(buf, sz); + + +} + void doDownloadByTime(nng_socket sock) { char * buf = NULL; int rv; @@ -93,8 +141,10 @@ } + + int -client(const char *method) +runclient(const char *method) { nng_socket sock; int rv; @@ -105,18 +155,35 @@ if ((rv = nng_dial(sock, url, NULL, 0)) != 0) { fatal("nng_dial", rv); } - printf("CLIENT: SENDING DATE REQUEST\n"); + //printf("CLIENT: SENDING DATE REQUEST\n"); if (strcmp(method, "login") == 0) { doLogin(sock); } else if (strcmp(method, "download") == 0) { doDownloadByTime(sock); + } else if (strcmp(method, "getDeviceInfo") == 0) { + doGetDeviceInfo(sock); + } else if (strcmp(method, "logout") == 0) { + doLogout(sock); } else { printf("Don't surpport %s \n", method); } nng_close(sock); return (0); +} + + +void *client(void *vargp) { + char method[1024]={0}; + for(;;) { + printf("===璇疯緭鍏ヨ姹傛柟娉曪細\n"); + scanf("%s", method); + if (strcmp(method, "q") == 0) { + exit(0); + } + runclient(method); + } } int @@ -137,7 +204,9 @@ if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) { fatal("nng_recv", rv); } + printf("===Received Call Back===\n"); std::cout << buf << std::endl; + nng_free(buf, sz); Json::Value response; response["code"] = 0; @@ -146,21 +215,7 @@ if (rv != 0) { fatal("nng_send", rv); } - // if ((sz == sizeof(uint64_t)) && - // ((GET64(buf, val)) == DATECMD)) { - // time_t now; - // printf("SERVER: RECEIVED DATE REQUEST\n"); - // now = time(&now); - // printf("SERVER: SENDING DATE: "); - // showdate(now); - - // // Reuse the buffer. We know it is big enough. - // PUT64(buf, (uint64_t) now); - - // continue; - // } - // Unrecognized command, so toss the buffer. - //nng_free(buf, sz); + } } @@ -168,14 +223,14 @@ int main(const int argc, const char **argv) { - if ((argc > 1)) { - client(argv[1]); - server(); - } + // std::string str("123"); // char *str2="123"; // printf("str length %d, %d\n", str.length(), strlen(str2)); - - fprintf(stderr, "Usage: client <method> ...\n"); - return (1); + pthread_t tid; + pthread_create(&tid, NULL, client, NULL); + + server(); + return 0; + } \ No newline at end of file diff --git a/test/Makefile b/test/Makefile index 8080681..c8fa824 100755 --- a/test/Makefile +++ b/test/Makefile @@ -13,7 +13,7 @@ -all: nng/reqrep +all: nng/reqrep test clean: diff --git a/test/test b/test/test new file mode 100755 index 0000000..cc3b0c5 --- /dev/null +++ b/test/test Binary files differ diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..2ea0191 --- /dev/null +++ b/test/test.c @@ -0,0 +1,8 @@ +#include "usg_common.h" +#include "usg_typedef.h" + +int main() { + char *str = "123"; + char *str2 = strdup(str); + free(str2); +} \ No newline at end of file -- Gitblit v1.8.0