From bf17db04a093ca277c421ea7d24fe2a050963dca Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期三, 10 六月 2020 18:25:38 +0800
Subject: [PATCH] version 1
---
service/Makefile | 27 +
test/nanomsg/pubsub | 0
test/nanomsg/pair.c | 0
Makefile | 2
test/nng/reqrep.c | 211 +++++++----
service/netdisk_service | 0
device/hcnetdisk.c | 24
test/nanomsg/bus.c | 0
test/nanomsg/pipeline.c | 0
test/nanomsg/pair | 0
device/include/netdisk.h | 18
service/netdisk_service.c | 245 +++++++++++++
test/nanomsg/reqrep | 0
device/test | 0
test/nanomsg/survey.c | 0
message_define.md | 159 ++++++++
test/nanomsg/reqrep.c | 106 +++++
device/Makefile | 4
test/nanomsg/pubsub.c | 0
test/nanomsg/survey | 0
test/nng/reqrep | 0
service/test.c | 70 +++
service/client.c | 132 +++++++
device/include/hcnetdisk.h | 2
test/nanomsg/bus | 0
/dev/null | 43 --
data/login.dat | 2
test/Makefile | 5
service/core | 0
device/test.c | 22 -
test/nanomsg/pipeline | 0
service/client | 0
service/test | 0
33 files changed, 903 insertions(+), 169 deletions(-)
diff --git a/Makefile b/Makefile
index a5b0d8f..dc294a8 100755
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-DIRS = common netdisk
+DIRS = common device service
all:
for i in $(DIRS); do \
diff --git a/device/data/login.dat b/data/login.dat
similarity index 84%
rename from device/data/login.dat
rename to data/login.dat
index 33dfe6d..cfdd39a 100644
--- a/device/data/login.dat
+++ b/data/login.dat
@@ -1,5 +1,6 @@
{
"1" : {
+ "deviceType" : "HC",
"host" : "192.168.20.11",
"loginUUID" : "1",
"password" : "a1234567",
@@ -7,6 +8,7 @@
"username" : "admin"
},
"2" : {
+ "deviceType" : "HC",
"host" : "192.168.20.11",
"loginUUID" : "2",
"password" : "a1234567",
diff --git a/device/Makefile b/device/Makefile
index c6372a7..f638738 100644
--- a/device/Makefile
+++ b/device/Makefile
@@ -3,7 +3,7 @@
#
ROOT=..
LDLIBS+=-Wl,-rpath=../hclib:../hclib/HCNetSDKCom:../common
-LDDIR += -L$(ROOT)/hcnetdisk_wrapper -L../hclib -L../hclib/HCNetSDKCom
+LDDIR += -L../hclib -L../hclib/HCNetSDKCom
LDLIBS += -lhcnetsdk -lhpr -lHCCore
LDLIBS += -lpthread -ljsoncpp
LIB_NETDISK = libnetdisk.a
@@ -17,7 +17,7 @@
all: $(LIB_NETDISK) $(DLIB_NETDISK)
-test: hcnetdisk.c netdisk.c
+test: hcnetdisk.c
#static lib
$(LIB_NETDISK): $(OBJS)
diff --git a/device/hcnetdisk.c b/device/hcnetdisk.c
index 7938a60..b62aecc 100644
--- a/device/hcnetdisk.c
+++ b/device/hcnetdisk.c
@@ -36,8 +36,8 @@
-HCNetdisk::HCNetdisk(const std::string &_devicename):Netdisk(_devicename){
-
+HCNetdisk::HCNetdisk(): Netdisk(){
+ deviceType="HC";
}
int HCNetdisk::_login(Netdisk_LoginInfo &loginInfo) {
@@ -55,6 +55,7 @@
NET_DVR_DEVICEINFO_V40 struDeviceInfo = {0};
//lUserID = 0;
lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfo);
+ printf("lUserID = %d\n", lUserID);
if (lUserID < 0)
{
printf("Login error, %d\n", NET_DVR_GetLastError());
@@ -62,7 +63,8 @@
return -1;
}
- printf("lUserID = %ld\n", lUserID);
+
+ loginInfo.userid = std::to_string(lUserID);
userid = lUserID;
username = loginInfo.username;
password = loginInfo.password;
@@ -72,8 +74,6 @@
startChannel = struDeviceInfo.struDeviceV30.byStartDChan;
maxChannels = struDeviceInfo.struDeviceV30.byIPChanNum + struDeviceInfo.struDeviceV30.byHighDChanNum * 256;
-
-
return 0;
}
@@ -81,16 +81,14 @@
int HCNetdisk::login(Netdisk_LoginInfo &loginInfo) {
- //_login(loginInfo);
- Netdisk::addLoginInfo(loginInfo);
+ return _login(loginInfo);
+ //Netdisk::addLoginInfo(loginInfo);
- return 0;
}
int HCNetdisk::logout() {
//娉ㄩ攢鐢ㄦ埛
NET_DVR_Logout(userid);
- userid = -1;
return 0;
}
@@ -99,8 +97,8 @@
char destfile[1024];
int i = 0;
- Netdisk_LoginInfo loginInfo = Netdisk::getLoginInfo(request.loginUUID);
- _login(loginInfo);
+ // Netdisk_LoginInfo loginInfo = Netdisk::getLoginInfo(request.loginUUID);
+ // _login(loginInfo);
start = request.start;
@@ -117,12 +115,12 @@
// printf("end = %s", asctime(&end));
sprintf(destfile, "%s/%s-%d-%ld-%d",
request.destpath.c_str(),
- devicename.c_str(),
+ deviceType.c_str(),
request.channel,
mktime(&start),
i);
// printf("%s\n", destfile);
- downloadByTime_wrapper(userid, request.channel, &start, &end, destfile);
+ // downloadByTime_wrapper(userid, request.channel, &start, &end, destfile);
if(files != NULL) {
files->push_back(destfile);
}
diff --git a/device/include/hcnetdisk.h b/device/include/hcnetdisk.h
index 2431290..85058c6 100644
--- a/device/include/hcnetdisk.h
+++ b/device/include/hcnetdisk.h
@@ -18,7 +18,7 @@
int _login(Netdisk_LoginInfo &loginInfo);
public:
static bool envInited ;
- HCNetdisk(const std::string &_devicename = "HC");
+ HCNetdisk();
virtual ~HCNetdisk();
diff --git a/device/include/logintable.h b/device/include/logintable.h
deleted file mode 100644
index c3d477c..0000000
--- a/device/include/logintable.h
+++ /dev/null
@@ -1,28 +0,0 @@
-#ifndef __NETDISK_LOGINTABLE_H__
-#define __NETDISK_LOGINTABLE_H__
-
-#include "usg_common.h"
-#include "usg_typedef.h"
-
-typedef struct LoginTableRecord
-{
- char loginUUID[512];
- char username[512];
- char password[512];
- char host[512];
- int port;
- int invalid;
-} LoginTable;
-
-
-
-
-class LoginTable {
-private:
- static char * logindatFile = "./data/login.dat"
-public:
-
-}
-
-
-#endif
\ No newline at end of file
diff --git a/device/include/netdisk.h b/device/include/netdisk.h
index 1964cc7..3695ac6 100644
--- a/device/include/netdisk.h
+++ b/device/include/netdisk.h
@@ -10,6 +10,8 @@
typedef struct Netdisk_LoginInfo {
std::string loginUUID;
+ std::string deviceType;
+ std::string userid;
std::string username;
std::string password;
std::string host;
@@ -34,27 +36,26 @@
std::string loginDataFile;
protected:
- std::string devicename; //璁惧鍚嶇О
+
int startChannel; //璧峰閫氶亾鍙�
int maxChannels; //鏈�澶ч�氶亾鏁�
- long userid; //鐢ㄦ埛id
+ std::string deviceType; //璁惧绫诲瀷
+ std::string loginUUID;
std::string username; //鐢ㄦ埛鍚�
std::string password; //瀵嗙爜
std::string host; //ip
int port; //绔彛
+ long userid; //鐢ㄦ埛id
- //Json::FastWriter jsonwriter;
-
- Json::Value loginData;
- Netdisk_LoginInfo getLoginInfo(std::string uuid);
+ // Netdisk_LoginInfo getLoginInfo(std::string uuid);
- int addLoginInfo(Netdisk_LoginInfo &loginInfo);
+ // int addLoginInfo(Netdisk_LoginInfo &loginInfo);
public:
- Netdisk(const std::string &_devicename="");
+ Netdisk(){};
virtual ~Netdisk() {}
@@ -65,7 +66,6 @@
virtual int downloadByTime(Netdisk_DownloadRequest &request, std::vector<std::string> *files) = 0;
- void saveLoginInfo();
int getStartChannel() {return startChannel;}
diff --git a/device/netdisk.c b/device/netdisk.c
deleted file mode 100644
index 74b7e93..0000000
--- a/device/netdisk.c
+++ /dev/null
@@ -1,43 +0,0 @@
-#include "netdisk.h"
-using namespace std;
-
-Netdisk::Netdisk(const std::string &_devicename): loginDataFile("data/login.dat"), devicename(_devicename) {
- Json::Reader jsonreader;
- ifstream fin(loginDataFile);
- jsonreader.parse(fin, loginData);
- fin.close();
-}
-
-int Netdisk::addLoginInfo(Netdisk_LoginInfo &loginInfo) {
- // Json::FastWriter write;
- // Json::Value root;
-
- Json::Value item;
- item["loginUUID"] = loginInfo.loginUUID;
- item["username"] = loginInfo.username;
- item["password"] = loginInfo.password;
- item["host"] = loginInfo.host;
- item["port"] = loginInfo.port;
-
- loginData[loginInfo.loginUUID] = item;
-
-
- auto str = loginData.toStyledString();
- // std::cout << str << std::endl;
-
- std::ofstream fout(loginDataFile);
- fout << str;
- fout.close();
- return 0;
-}
-
-Netdisk_LoginInfo Netdisk::getLoginInfo(std::string uuid) {
- Json::Value item = loginData[uuid];
- Netdisk_LoginInfo loginInfo;
- loginInfo.loginUUID = item["loginUUID"].asString();
- loginInfo.username = item["username"].asString();
- loginInfo.password = item["password"].asString();
- loginInfo.host = item["host"].asString();
- loginInfo.port = item["port"].asInt();
- return loginInfo;
-}
diff --git a/device/test b/device/test
index d97ccbd..2835ae5 100755
--- a/device/test
+++ b/device/test
Binary files differ
diff --git a/device/test.c b/device/test.c
index 6a75e45..d8a777c 100644
--- a/device/test.c
+++ b/device/test.c
@@ -7,11 +7,12 @@
void test1() {
- Netdisk *netdisk = new HCNetdisk("娴峰悍");
+ Netdisk *netdisk = new HCNetdisk();
//鐧诲綍
Netdisk_LoginInfo loginInfo;
loginInfo.loginUUID="1";
+ loginInfo.deviceType="HC";
loginInfo.host = "192.168.20.11";
loginInfo.port = 8000;
loginInfo.username = "admin";
@@ -53,24 +54,7 @@
netdisk->logout();
}
-void test2() {
-
-
- HCNetdisk netdisk("娴峰悍");
- //Netdisk *netdisk = dhnetdisk_new();
-
-
- //鐧诲綍
- Netdisk_LoginInfo loginInfo;
- loginInfo.host = "192.168.20.11";
- loginInfo.port = 8000;
- loginInfo.username = "admin";
- loginInfo.password = "a1234567";
- netdisk.login(loginInfo);
-
- std::cout << "璧峰閫氶亾:" << netdisk.getStartChannel() << ", 鏈�澶ч�氶亾鍙凤細" << netdisk.getMaxChannels() << std::endl;
-
-}
+
int main() {
diff --git a/message_define.md b/message_define.md
new file mode 100644
index 0000000..00b26af
--- /dev/null
+++ b/message_define.md
@@ -0,0 +1,159 @@
+# 娑堟伅浣撳畾涔�
+
+閫氫俊娑堟伅浣撻噰鐢╦son鐨勬暟鎹牸寮忋��
+
+## 1. 鐧诲綍
+
+璇锋眰浣�
+
+```
+{
+ method: "login",
+ arguments: {
+ //璁惧绫诲瀷锛圚C锛氭捣搴凤級
+ deviceType: "HC",
+ loginUUID: "11111",
+ //鐢ㄦ埛鍚�
+ username: "admin",
+ //瀵嗙爜
+ password: "a1234567",
+ // ip鍦板潃
+ host: "192.168.20.11",
+ // 绔彛鍙�
+ port: 8000
+ }
+
+}
+
+```
+鍝嶅簲浣�
+```
+{
+ code: 0, // 0琛ㄧず鎴愬姛锛屽叾浠栧�间唬琛ㄧ浉搴旂殑閿欒鐮�
+ payload: {
+ loginUUID: "11111"
+ }
+
+}
+
+```
+
+
+## 2銆佷笅杞�
+璇锋眰浣�
+```
+{
+ method: "downloadByTime",
+ arguments: {
+ loginUUID: "11111",
+ //閫氶亾鍙�
+ channel: 1,
+ //寮�濮嬫椂闂�
+ start: {
+ year: 2020,
+ mon: 4,
+ day: 10,
+ hour: 14,
+ min: 20,
+ sec: 0
+ },
+ //缁撴潫鏃堕棿
+ end: {
+ year: 2020,
+ mon: 4,
+ day: 10,
+ hour: 15,
+ min: 20,
+ sec: 0
+ },
+ //涓嬭浇鏂囦欢瀛樺偍璺緞
+ destpath: "/Downloads"
+ }
+
+}
+```
+鍝嶅簲浣�
+
+```
+{
+ code: 0, // 0琛ㄧず鎴愬姛锛屽叾浠栧�间唬琛ㄧ浉搴旂殑閿欒鐮�
+ payload: {
+ //瀹屾垚涓嬭浇鐨勬枃浠剁殑鍒楄〃
+ filelist: [
+ "/Downloads/1.mp3",
+ "/Downloads/2.mp3",
+ "/Downloads/3.mp3"
+ ]
+ }
+
+}
+
+```
+
+## 3. 鐧诲嚭
+璇锋眰浣�
+```
+{
+ method: "logout",
+ arguments: {
+ loginUUID: "1"
+ }
+
+}
+```
+鍝嶅簲浣�
+
+```
+{
+ code: 0 // 0琛ㄧず鎴愬姛锛屽叾浠栧�间唬琛ㄧ浉搴旂殑閿欒鐮�
+
+}
+```
+
+## 4. 鑾峰彇璧峰閫氶亾鍙�
+璇锋眰浣�
+```
+{
+ method: "getStartChannel",
+ arguments: {
+ loginUUID: "1"
+ }
+}
+
+```
+
+鍝嶅簲浣�
+
+```
+{
+ code: 0, // 0琛ㄧず鎴愬姛锛屽叾浠栧�间唬琛ㄧ浉搴旂殑閿欒鐮�
+ payload: {
+ startChanel: 1
+ }
+
+}
+```
+
+## 5. 鑾峰彇鏈�澶ч�氶亾鍙�
+璇锋眰浣�
+```
+{
+ method: "getMaxChannels",
+ arguments: {
+ loginUUID: "1"
+ }
+}
+
+```
+
+鍝嶅簲浣�
+
+```
+{
+ code: 0, // 0琛ㄧず鎴愬姛锛屽叾浠栧�间唬琛ㄧ浉搴旂殑閿欒鐮�
+ payload: {
+ maxChanel: 64
+ }
+
+}
+```
\ No newline at end of file
diff --git a/service/Makefile b/service/Makefile
new file mode 100644
index 0000000..01f98fd
--- /dev/null
+++ b/service/Makefile
@@ -0,0 +1,27 @@
+#
+# Makefile for common library.
+#
+ROOT=..
+LDLIBS+=-Wl,-rpath=$(ROOT)/hclib:$(ROOT)/hclib/HCNetSDKCom:$(ROOT)/common:$(ROOT)/device
+LDDIR += -L$(ROOT)/device -L$(ROOT)/hclib -L$(ROOT)/hclib/HCNetSDKCom
+LDLIBS += -lnetdisk
+LDLIBS += -lhcnetsdk -lhpr -lHCCore
+LDLIBS += -ljsoncpp -lnng -lpthread
+
+INCLUDE += -I$(ROOT)/device/include
+LIB_NETDISK = $(ROOT)/libnetdisk.a
+DLIB_NETDISK = $(ROOT)/libnetdisk.so
+PLATFORM=$(shell $(ROOT)/systype.sh)
+include $(ROOT)/Make.defines.$(PLATFORM)
+
+all: netdisk_service client test
+
+netdisk_service: $(ROOT)/device/hcnetdisk.c
+
+test: $(ROOT)/device/hcnetdisk.c
+
+clean:
+ rm -f test *.o a.out core temp.* *.a *.so
+
+
+include $(ROOT)/Make.common.inc
diff --git a/service/client b/service/client
new file mode 100755
index 0000000..f0b2e36
--- /dev/null
+++ b/service/client
Binary files differ
diff --git a/service/client.c b/service/client.c
new file mode 100644
index 0000000..49d379d
--- /dev/null
+++ b/service/client.c
@@ -0,0 +1,132 @@
+#include "usg_common.h"
+#include "netdisk.h"
+#include "hcnetdisk.h"
+#include <jsoncpp/json/json.h>
+#include <nng/nng.h>
+#include <nng/protocol/reqrep0/rep.h>
+#include <nng/protocol/reqrep0/req.h>
+const char *url = "tcp://127.0.0.1:8899";
+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, strdupa(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"] = 15;
+ end["min"] = 20;
+ end["sec"] = 0;
+ arguments["end"] = end;
+
+ arguments["channel"] = 1;
+ arguments["destpath"] = "/home/basic/data/Downloads/test";
+ 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
+client(const char *method)
+{
+ nng_socket sock;
+ int rv;
+
+ if ((rv = nng_req0_open(&sock)) != 0) {
+ fatal("nng_socket", rv);
+ }
+ if ((rv = nng_dial(sock, url, 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 {
+ printf("Don't surpport %s \n", method);
+ }
+
+ nng_close(sock);
+ return (0);
+}
+
+int
+main(const int argc, const char **argv)
+{
+ if ((argc > 1))
+ return (client(argv[1]));
+ // 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);
+}
\ No newline at end of file
diff --git a/service/core b/service/core
new file mode 100644
index 0000000..118c81c
--- /dev/null
+++ b/service/core
Binary files differ
diff --git a/service/netdisk_service b/service/netdisk_service
new file mode 100755
index 0000000..196531c
--- /dev/null
+++ b/service/netdisk_service
Binary files differ
diff --git a/service/netdisk_service.c b/service/netdisk_service.c
new file mode 100644
index 0000000..57fd496
--- /dev/null
+++ b/service/netdisk_service.c
@@ -0,0 +1,245 @@
+#include "usg_common.h"
+#include "netdisk.h"
+#include "hcnetdisk.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;
+
+const char *url = "tcp://127.0.0.1:8899";
+Json::Value loginData;
+
+std::map<std::string, Netdisk *> *userDeviceMap;
+
+std::string login_data_file = "../data/login.dat";
+int saveLoginInfo(Netdisk_LoginInfo &loginInfo) {
+
+ Json::Value item;
+ item["loginUUID"] = loginInfo.loginUUID;
+ item["deviceType"] = loginInfo.deviceType;
+ item["username"] = loginInfo.username;
+ item["password"] = loginInfo.password;
+ item["host"] = loginInfo.host;
+ item["port"] = loginInfo.port;
+
+ loginData[loginInfo.loginUUID] = item;
+
+
+ auto str = loginData.toStyledString();
+ // std::cout << str << std::endl;
+
+ std::ofstream fout(login_data_file);
+ fout << str;
+ fout.close();
+ return 0;
+}
+
+Netdisk_LoginInfo getLoginInfo(std::string uuid) {
+ Json::Value item = loginData[uuid];
+ Netdisk_LoginInfo loginInfo;
+ loginInfo.loginUUID = item["loginUUID"].asString();
+ loginInfo.deviceType = item["deviceType"].asString();
+ loginInfo.username = item["username"].asString();
+ loginInfo.password = item["password"].asString();
+ loginInfo.host = item["host"].asString();
+ loginInfo.port = item["port"].asInt();
+ return loginInfo;
+}
+
+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) {
+ int rv, code;
+ // 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<std::string, Netdisk *>::iterator userDeviceIter = userDeviceMap->find(loginInfo.loginUUID);
+ if( userDeviceIter != userDeviceMap->end() ) {
+ netdisk = userDeviceIter->second;
+ }
+ if (netdisk == NULL) {
+ printf("=========null\n");
+ if(loginInfo.deviceType == "HC") {
+ // std::cout << "new HCNetdisk" << std::endl;
+ netdisk = new HCNetdisk();
+ userDeviceMap->insert({loginInfo.loginUUID, netdisk});
+ } else {
+ err_msg(0, "鏃犳硶璇嗗埆鐨勮澶囩被鍨嬶細 %s", loginInfo.deviceType.c_str());
+ }
+ }
+
+ code = netdisk->login(loginInfo);
+ if (code == 0) {
+ saveLoginInfo(loginInfo);
+ std::cout << "璧峰閫氶亾:" << netdisk->getStartChannel() << ", 鏈�澶ч�氶亾鍙凤細" << netdisk->getMaxChannels() << std::endl;
+ }
+
+
+ Json::Value response;
+ Json::Value payload;
+ payload["loginUUID"] = loginInfo.loginUUID;
+ response["code"] = code;
+ response["payload"] = payload;
+ const std::string str = response.toStyledString();
+ // nng鍐呴儴浼氶噴鏀綽uf
+ 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 handleDownloadByTime(nng_socket sock, Json::Value request) {
+ 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<string, Netdisk *>::iterator userDeviceIter = userDeviceMap->find( drequest.loginUUID);
+ if( userDeviceIter != userDeviceMap->end() ) {
+ netdisk = userDeviceIter->second;
+ }
+
+ Netdisk_LoginInfo loginInfo = getLoginInfo(drequest.loginUUID);
+ if (netdisk == NULL) {
+
+ if(loginInfo.deviceType == "HC") {
+ // std::cout << "new HCNetdisk" << std::endl;
+ netdisk = new HCNetdisk();
+ userDeviceMap->insert({loginInfo.loginUUID, netdisk});
+ } else {
+ err_msg(0, "鏃犳硶璇嗗埆鐨勮澶囩被鍨嬶細 %s", loginInfo.deviceType.c_str());
+
+ }
+ }
+
+ if ( (code = netdisk->login(loginInfo)) != 0 ) {
+ printf("涓嬭浇鐧诲綍澶辫触\n");
+ }
+
+ std::vector<std::string> 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;
+
+
+}
+
+int
+server()
+{
+ Json::Reader jsonreader;
+ Json::Value request;
+ 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);
+ }
+
+ jsonreader.parse(buf, request);
+ std::string method = request["method"].asString();
+ if (method == "login") {
+ handleLogin(sock, request);
+ } else if (method == "downloadByTime") {
+ handleDownloadByTime(sock, request);
+ } else {
+ std::cerr << "Don't support " << method << std::endl;
+ }
+ // Unrecognized command, so toss the buffer.
+ nng_free(buf, sz);
+ }
+}
+
+int main()
+{
+ Netdisk_EnvConfig config;
+ config.libpath = "../hclib/";
+ HCNetdisk::netdisk_init(&config);
+
+ Json::Reader jsonreader;
+ ifstream fin(login_data_file);
+ jsonreader.parse(fin, loginData);
+ fin.close();
+
+
+ userDeviceMap = new std::map<std::string, Netdisk *>();
+ server();
+
+
+ HCNetdisk::netdisk_deinit();
+
+
+}
diff --git a/service/test b/service/test
new file mode 100755
index 0000000..498a736
--- /dev/null
+++ b/service/test
Binary files differ
diff --git a/service/test.c b/service/test.c
new file mode 100644
index 0000000..d8a777c
--- /dev/null
+++ b/service/test.c
@@ -0,0 +1,70 @@
+#include "usg_common.h"
+#include "netdisk.h"
+#include "hcnetdisk.h"
+
+
+
+void test1() {
+
+
+ Netdisk *netdisk = new HCNetdisk();
+
+ //鐧诲綍
+ Netdisk_LoginInfo loginInfo;
+ loginInfo.loginUUID="1";
+ loginInfo.deviceType="HC";
+ loginInfo.host = "192.168.20.11";
+ loginInfo.port = 8000;
+ loginInfo.username = "admin";
+ loginInfo.password = "a1234567";
+ netdisk->login(loginInfo);
+
+ std::cout << "璧峰閫氶亾:" << netdisk->getStartChannel() << ", 鏈�澶ч�氶亾鍙凤細" << netdisk->getMaxChannels() << std::endl;
+
+
+ //涓嬭浇
+ Netdisk_DownloadRequest request;
+ request.loginUUID = "1";
+ request.start.tm_year = 2020-1900; // 杩欎釜鏃堕棿绫诲瀷浠�1900寮�濮嬬畻浣滅涓�骞�
+ request.start.tm_mon = 4-1; // 0鏄涓�涓湀
+ request.start.tm_mday = 10;
+ request.start.tm_hour = 14;
+ request.start.tm_min = 20;
+ request.start.tm_sec = 0;
+
+
+ request.end.tm_year = 2020-1900; // 杩欎釜鏃堕棿绫诲瀷浠�1900寮�濮嬬畻浣滅涓�骞�
+ request.end.tm_mon = 4-1; // 0鏄涓�涓湀
+ request.end.tm_mday = 10;
+ request.end.tm_hour = 15;
+ request.end.tm_min = 20;
+ request.end.tm_sec = 0;
+
+ request.channel = 1;
+ request.destpath = "/home/basic/data/Downloads/test";
+ std::vector<std::string> files;
+ if (netdisk->downloadByTime(request, &files) != 0) {
+ printf("涓嬭浇澶辫触\n");
+ } else {
+ for(auto f : files) {
+ std::cout << f << '\n';
+ }
+ }
+
+ netdisk->logout();
+}
+
+
+
+int main() {
+
+ Netdisk_EnvConfig config;
+ config.libpath = "../hclib/";
+ HCNetdisk::netdisk_init(&config);
+
+ test1();
+ // test2();
+
+ HCNetdisk::netdisk_deinit();
+
+}
\ No newline at end of file
diff --git a/test/Makefile b/test/Makefile
index be2e246..8080681 100755
--- a/test/Makefile
+++ b/test/Makefile
@@ -5,7 +5,7 @@
LDLIBS+=-Wl,-rpath=../hclib:../hclib/HCNetSDKCom:../common
LDDIR += -L$(ROOT)/hcnetdisk_wrapper -L../hclib -L../hclib/HCNetSDKCom
LDLIBS += -lhcnetsdk -lhpr -lHCCore
-LDLIBS += -lpthread -ljsoncpp -lnanomsg
+LDLIBS += -ljsoncpp -lnng -lpthread
LIB_NETDISK = libnetdisk.a
DLIB_NETDISK = libnetdisk.so
PLATFORM=$(shell $(ROOT)/systype.sh)
@@ -13,9 +13,8 @@
-all: download1 test_file test_io profile test_json
+all: nng/reqrep
-test_file: test_file.c
clean:
rm -f test *.o a.out core temp.* *.a *.so
diff --git a/test/nng/bus b/test/nanomsg/bus
similarity index 100%
rename from test/nng/bus
rename to test/nanomsg/bus
Binary files differ
diff --git a/test/nng/bus.c b/test/nanomsg/bus.c
similarity index 100%
rename from test/nng/bus.c
rename to test/nanomsg/bus.c
diff --git a/test/nng/pair b/test/nanomsg/pair
similarity index 100%
rename from test/nng/pair
rename to test/nanomsg/pair
Binary files differ
diff --git a/test/nng/pair.c b/test/nanomsg/pair.c
similarity index 100%
rename from test/nng/pair.c
rename to test/nanomsg/pair.c
diff --git a/test/nng/pipeline b/test/nanomsg/pipeline
similarity index 100%
rename from test/nng/pipeline
rename to test/nanomsg/pipeline
Binary files differ
diff --git a/test/nng/pipeline.c b/test/nanomsg/pipeline.c
similarity index 100%
rename from test/nng/pipeline.c
rename to test/nanomsg/pipeline.c
diff --git a/test/nng/pubsub b/test/nanomsg/pubsub
similarity index 100%
rename from test/nng/pubsub
rename to test/nanomsg/pubsub
Binary files differ
diff --git a/test/nng/pubsub.c b/test/nanomsg/pubsub.c
similarity index 100%
rename from test/nng/pubsub.c
rename to test/nanomsg/pubsub.c
diff --git a/test/nanomsg/reqrep b/test/nanomsg/reqrep
new file mode 100755
index 0000000..ff3e569
--- /dev/null
+++ b/test/nanomsg/reqrep
Binary files differ
diff --git a/test/nanomsg/reqrep.c b/test/nanomsg/reqrep.c
new file mode 100644
index 0000000..e6f17c7
--- /dev/null
+++ b/test/nanomsg/reqrep.c
@@ -0,0 +1,106 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <nanomsg/nn.h>
+#include <nanomsg/reqrep.h>
+
+#define NODE0 "node0"
+#define NODE1 "node1"
+#define DATE "DATE"
+
+void
+fatal(const char *func)
+{
+ fprintf(stderr, "%s: %s\n", func, nn_strerror(nn_errno()));
+ exit(1);
+}
+
+char *
+date(void)
+{
+ time_t now = time(&now);
+ struct tm *info = localtime(&now);
+ char *text = asctime(info);
+ text[strlen(text)-1] = '\0'; // remove '\n'
+ return (text);
+}
+
+int
+node0(const char *url)
+{
+ int sz_date = strlen(DATE) + 1; // '\0' too
+ int sock;
+ int rv;
+
+ if ((sock = nn_socket(AF_SP, NN_REP)) < 0) {
+ fatal("nn_socket");
+ }
+ if ((rv = nn_bind(sock, url)) < 0) {
+ fatal("nn_bind");
+ }
+ for (;;) {
+ char *buf = NULL;
+ int bytes;
+ if ((bytes = nn_recv(sock, &buf, NN_MSG, 0)) < 0) {
+ fatal("nn_recv");
+ }
+ if ((bytes == (strlen(DATE) + 1)) && (strcmp(DATE, buf) == 0)) {
+ printf("NODE0: RECEIVED DATE REQUEST\n");
+ char *d = date();
+ int sz_d = strlen(d) + 1; // '\0' too
+ printf("NODE0: SENDING DATE %s\n", d);
+ if ((bytes = nn_send(sock, d, sz_d, 0)) < 0) {
+ fatal("nn_send");
+ }
+ }
+ nn_freemsg(buf);
+ }
+}
+
+int
+node1(const char *url)
+{
+ int sz_date = strlen(DATE) + 1; // '\0' too
+ char *buf = NULL;
+ int bytes = -1;
+ int sock;
+ int rv;
+
+ if ((sock = nn_socket(AF_SP, NN_REQ)) < 0) {
+ fatal("nn_socket");
+ }
+ if ((rv = nn_connect (sock, url)) < 0) {
+ fatal("nn_connect");
+ }
+ printf("NODE1: SENDING DATE REQUEST %s\n", DATE);
+ if ((bytes = nn_send(sock, DATE, sz_date, 0)) < 0) {
+ fatal("nn_send");
+ }
+ if ((bytes = nn_recv(sock, &buf, NN_MSG, 0)) < 0) {
+ fatal("nn_recv");
+ }
+ printf("NODE1: RECEIVED DATE %s\n", buf);
+ nn_freemsg(buf);
+ return (nn_shutdown(sock, 0));
+}
+
+int
+main(const int argc, const char **argv)
+{
+ if ((argc > 1) && (strcmp(NODE0, argv[1]) == 0))
+ return (node0(argv[2]));
+
+ if ((argc > 1) && (strcmp(NODE1, argv[1]) == 0))
+ return (node1(argv[2]));
+
+ fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", NODE0, NODE1);
+ return (1);
+}
+
+/*
+
+./reqrep node0 ipc:///tmp/reqrep.ipc & node0=$! && sleep 1
+./reqrep node1 ipc:///tmp/reqrep.ipc
+kill $node0
+*/
\ No newline at end of file
diff --git a/test/nng/survey b/test/nanomsg/survey
similarity index 100%
rename from test/nng/survey
rename to test/nanomsg/survey
Binary files differ
diff --git a/test/nng/survey.c b/test/nanomsg/survey.c
similarity index 100%
rename from test/nng/survey.c
rename to test/nanomsg/survey.c
diff --git a/test/nng/reqrep b/test/nng/reqrep
index ff3e569..a9ce2e7 100755
--- a/test/nng/reqrep
+++ b/test/nng/reqrep
Binary files differ
diff --git a/test/nng/reqrep.c b/test/nng/reqrep.c
index e6f17c7..805afe6 100644
--- a/test/nng/reqrep.c
+++ b/test/nng/reqrep.c
@@ -1,106 +1,161 @@
-#include <stdlib.h>
+//
+// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2018 Capitar IT Group BV <info@capitar.com>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
+
+//
+// This is just a simple REQ/REP demonstration application. It is derived
+// from the legacy nanomsg demonstration program of the same name, written
+// by Tim Dysinger, but updated for nng. I've also updated it to pass simpler
+// binary data rather than strings over the network.
+//
+// The program implements a simple RPC style service, which just returns
+// the date in UNIX time (seconds since 1970).
+//
+#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <time.h>
-#include <nanomsg/nn.h>
-#include <nanomsg/reqrep.h>
-#define NODE0 "node0"
-#define NODE1 "node1"
-#define DATE "DATE"
+#include <nng/nng.h>
+#include <nng/protocol/reqrep0/rep.h>
+#include <nng/protocol/reqrep0/req.h>
+
+#define CLIENT "client"
+#define SERVER "server"
+#define DATECMD 1
+
+#define PUT64(ptr, u) \
+ do { \
+ (ptr)[0] = (uint8_t)(((uint64_t)(u)) >> 56); \
+ (ptr)[1] = (uint8_t)(((uint64_t)(u)) >> 48); \
+ (ptr)[2] = (uint8_t)(((uint64_t)(u)) >> 40); \
+ (ptr)[3] = (uint8_t)(((uint64_t)(u)) >> 32); \
+ (ptr)[4] = (uint8_t)(((uint64_t)(u)) >> 24); \
+ (ptr)[5] = (uint8_t)(((uint64_t)(u)) >> 16); \
+ (ptr)[6] = (uint8_t)(((uint64_t)(u)) >> 8); \
+ (ptr)[7] = (uint8_t)((uint64_t)(u)); \
+ } while (0)
+
+#define GET64(ptr, v) \
+ v = (((uint64_t)((uint8_t)(ptr)[0])) << 56) + \
+ (((uint64_t)((uint8_t)(ptr)[1])) << 48) + \
+ (((uint64_t)((uint8_t)(ptr)[2])) << 40) + \
+ (((uint64_t)((uint8_t)(ptr)[3])) << 32) + \
+ (((uint64_t)((uint8_t)(ptr)[4])) << 24) + \
+ (((uint64_t)((uint8_t)(ptr)[5])) << 16) + \
+ (((uint64_t)((uint8_t)(ptr)[6])) << 8) + \
+ (((uint64_t)(uint8_t)(ptr)[7]))
void
-fatal(const char *func)
+fatal(const char *func, int rv)
{
- fprintf(stderr, "%s: %s\n", func, nn_strerror(nn_errno()));
- exit(1);
+ fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
+ exit(1);
}
-char *
-date(void)
+void
+showdate(time_t now)
{
- time_t now = time(&now);
- struct tm *info = localtime(&now);
- char *text = asctime(info);
- text[strlen(text)-1] = '\0'; // remove '\n'
- return (text);
+ struct tm *info = localtime(&now);
+ printf("%s", asctime(info));
}
int
-node0(const char *url)
+server(const char *url)
{
- int sz_date = strlen(DATE) + 1; // '\0' too
- int sock;
- int rv;
+ nng_socket sock;
+ int rv;
- if ((sock = nn_socket(AF_SP, NN_REP)) < 0) {
- fatal("nn_socket");
- }
- if ((rv = nn_bind(sock, url)) < 0) {
- fatal("nn_bind");
- }
- for (;;) {
- char *buf = NULL;
- int bytes;
- if ((bytes = nn_recv(sock, &buf, NN_MSG, 0)) < 0) {
- fatal("nn_recv");
- }
- if ((bytes == (strlen(DATE) + 1)) && (strcmp(DATE, buf) == 0)) {
- printf("NODE0: RECEIVED DATE REQUEST\n");
- char *d = date();
- int sz_d = strlen(d) + 1; // '\0' too
- printf("NODE0: SENDING DATE %s\n", d);
- if ((bytes = nn_send(sock, d, sz_d, 0)) < 0) {
- fatal("nn_send");
- }
- }
- nn_freemsg(buf);
- }
+ 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;
+ uint64_t val;
+ if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
+ fatal("nng_recv", 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);
+ rv = nng_send(sock, buf, sz, NNG_FLAG_ALLOC);
+ if (rv != 0) {
+ fatal("nng_send", rv);
+ }
+ continue;
+ }
+ // Unrecognized command, so toss the buffer.
+ nng_free(buf, sz);
+ }
}
int
-node1(const char *url)
+client(const char *url)
{
- int sz_date = strlen(DATE) + 1; // '\0' too
- char *buf = NULL;
- int bytes = -1;
- int sock;
- int rv;
+ nng_socket sock;
+ int rv;
+ size_t sz;
+ char * buf = NULL;
+ uint8_t cmd[sizeof(uint64_t)];
- if ((sock = nn_socket(AF_SP, NN_REQ)) < 0) {
- fatal("nn_socket");
- }
- if ((rv = nn_connect (sock, url)) < 0) {
- fatal("nn_connect");
- }
- printf("NODE1: SENDING DATE REQUEST %s\n", DATE);
- if ((bytes = nn_send(sock, DATE, sz_date, 0)) < 0) {
- fatal("nn_send");
- }
- if ((bytes = nn_recv(sock, &buf, NN_MSG, 0)) < 0) {
- fatal("nn_recv");
- }
- printf("NODE1: RECEIVED DATE %s\n", buf);
- nn_freemsg(buf);
- return (nn_shutdown(sock, 0));
+ PUT64(cmd, DATECMD);
+
+ if ((rv = nng_req0_open(&sock)) != 0) {
+ fatal("nng_socket", rv);
+ }
+ if ((rv = nng_dial(sock, url, NULL, 0)) != 0) {
+ fatal("nng_dial", rv);
+ }
+ printf("CLIENT: SENDING DATE REQUEST\n");
+ if ((rv = nng_send(sock, cmd, sizeof(cmd), 0)) != 0) {
+ fatal("nng_send", rv);
+ }
+ if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
+ fatal("nng_recv", rv);
+ }
+
+ if (sz == sizeof(uint64_t)) {
+ uint64_t now;
+ GET64(buf, now);
+ printf("CLIENT: RECEIVED DATE: ");
+ showdate((time_t) now);
+ } else {
+ printf("CLIENT: GOT WRONG SIZE!\n");
+ }
+
+ // This assumes that buf is ASCIIZ (zero terminated).
+ nng_free(buf, sz);
+ nng_close(sock);
+ return (0);
}
int
main(const int argc, const char **argv)
{
- if ((argc > 1) && (strcmp(NODE0, argv[1]) == 0))
- return (node0(argv[2]));
+ if ((argc > 1) && (strcmp(CLIENT, argv[1]) == 0))
+ return (client(argv[2]));
- if ((argc > 1) && (strcmp(NODE1, argv[1]) == 0))
- return (node1(argv[2]));
+ if ((argc > 1) && (strcmp(SERVER, argv[1]) == 0))
+ return (server(argv[2]));
- fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", NODE0, NODE1);
- return (1);
+ fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", CLIENT, SERVER);
+ return (1);
}
-
-/*
-
-./reqrep node0 ipc:///tmp/reqrep.ipc & node0=$! && sleep 1
-./reqrep node1 ipc:///tmp/reqrep.ipc
-kill $node0
-*/
\ No newline at end of file
--
Gitblit v1.8.0