| | |
| | | #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 |
| | | } |
| | |
| | | |
| | | } 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; |
| | |
| | | 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 |
| | | |
| | |
| | | |
| | | 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) { |
| | |
| | | } |
| | | |
| | | jsonreader.parse(buf, request); |
| | | nng_free(buf, sz); |
| | | std::string method = request["method"].asString(); |
| | | |
| | | std::map<std::string, RequestHandleFun>::iterator handleFunIter = requestHandleFunMap.find(method); |
| | |
| | | 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; |
| | |
| | | |
| | | 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(); |
| | | |
| | |
| | | } |
| | | |
| | | 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; |
| | | |
| | |
| | | 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内部会释放buf |
| | |
| | | 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内部会释放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<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内部会释放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 << "accepted handleDownloadByTime request" << std::endl; |
| | | int rv; |
| | | char rmsg[MAXLINE]; |
| | | strcpy(rmsg, "received"); |
| | | // char *buf; |
| | | |
| | | Json::Value arguments = request["arguments"]; |
| | |
| | | |
| | | 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) { |
| | |
| | | return rv; |
| | | } |
| | | return 0; |
| | | |
| | | |
| | | } |
| | | |
| | | int handleDownloadByTime(nng_socket sock, Json::Value request) { |
| | |
| | | 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); |
| | | int handleDownloadByTimeAsync(nng_socket sock, Json::Value request); |
| | | int handleGetDeviceInfo(nng_socket sock, Json::Value request); |
File was renamed from service/client.c |
| | |
| | | |
| | | } |
| | | |
| | | 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; |
| | |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | int |
| | | client(const char *method) |
| | | runclient(const char *method) |
| | | { |
| | | nng_socket sock; |
| | | int rv; |
| | |
| | | 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 |
| | |
| | | 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; |
| | |
| | | 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); |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | 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; |
| | | |
| | | } |
| | |
| | | |
| | | |
| | | |
| | | all: nng/reqrep |
| | | all: nng/reqrep test |
| | | |
| | | |
| | | clean: |
New file |
| | |
| | | #include "usg_common.h" |
| | | #include "usg_typedef.h" |
| | | |
| | | int main() { |
| | | char *str = "123"; |
| | | char *str2 = strdup(str); |
| | | free(str2); |
| | | } |