#include "usg_common.h" #include "netdisk.h" #include "hcnetdisk.h" #include "properties_config.h" #include #include #include #include PropertiesConfig config("../data/config.txt"); void fatal(const char *func, int rv) { fprintf(stderr, "%s: %s\n", func, nng_strerror(rv)); exit(1); } void doLogin(nng_socket sock) { char * buf = NULL; int rv; size_t sz; Json::Value request; Json::Value arguments; arguments["loginUUID"] = "1"; arguments["deviceType"] = "HC"; arguments["username"] = "admin"; arguments["password"] = "a1234567"; arguments["host"] = "192.168.20.11"; arguments["port"] = 8000; request["method"] = "login"; 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 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; size_t sz; Json::Value request; Json::Value arguments; arguments["loginUUID"] = "1"; Json::Value start; start["year"] = 2020; start["mon"] = 4; start["day"] = 10; start["hour"] = 14; start["min"] = 20; start["sec"] = 0; arguments["start"] = start; Json::Value end; end["year"] = 2020; end["mon"] = 4; end["day"] = 10; end["hour"] = 14; end["min"] = 40; end["sec"] = 0; arguments["end"] = end; arguments["channel"] = 1; arguments["destpath"] = "/home/basic/data/Downloads"; arguments["host"] = "192.168.20.11"; arguments["port"] = 8000; request["method"] = "downloadByTime"; 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 << std::endl; // Json::Reader jsonreader; // Json::Value response; // jsonreader.parse(buf, response); // std::cout << response["payload"]["filelist"][3]; nng_free(buf, sz); } int runclient(const char *method) { nng_socket sock; int rv; if ((rv = nng_req0_open(&sock)) != 0) { fatal("nng_socket", rv); } if ((rv = nng_dial(sock, config.get("server_url").c_str(), NULL, 0)) != 0) { fatal("nng_dial", rv); } //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 server(const char *url) { nng_socket sock; int rv; if ((rv = nng_rep0_open(&sock)) != 0) { fatal("nng_rep0_open", rv); } if ((rv = nng_listen(sock, url, NULL, 0)) != 0) { fatal("nng_listen", rv); } for (;;) { char * buf = NULL; size_t sz; 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; std::string str = response.toStyledString(); rv = nng_send(sock, strdup(str.c_str()), str.length(), NNG_FLAG_ALLOC); if (rv != 0) { fatal("nng_send", rv); } } } int main(const int argc, const char **argv) { // std::string str("123"); // char *str2="123"; // printf("str length %d, %d\n", str.length(), strlen(str2)); pthread_t tid; pthread_create(&tid, NULL, client, NULL); server(config.get("client_url").c_str()); return 0; }