wangzhengquan
2020-06-12 602442ecedf17800cddfdae188b05b40345cb011
version
4个文件已添加
1 文件已重命名
8个文件已修改
312 ■■■■ 已修改文件
common/include/usg_typedef.h 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
device/include/netdisk.h 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/Makefile 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/core 补丁 | 查看 | 原始文档 | blame | 历史
service/netdisk_service 补丁 | 查看 | 原始文档 | blame | 历史
service/netdisk_service.c 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/request_handler.c 127 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/request_handler.h 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/test_client 补丁 | 查看 | 原始文档 | blame | 历史
service/test_client.c 103 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/Makefile 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/test 补丁 | 查看 | 原始文档 | blame | 历史
test/test.c 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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
}
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;
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
service/core
Binary files differ
service/netdisk_service
Binary files differ
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();
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内部会释放buf
@@ -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内部会释放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"];
@@ -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) {
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);
int handleDownloadByTimeAsync(nng_socket sock, Json::Value request);
int handleGetDeviceInfo(nng_socket sock, Json::Value request);
service/test_client
Binary files differ
service/test_client.c
File was renamed from service/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;
}
test/Makefile
@@ -13,7 +13,7 @@
 
all: nng/reqrep
all: nng/reqrep test
clean:
test/test
Binary files differ
test/test.c
New file
@@ -0,0 +1,8 @@
#include "usg_common.h"
#include "usg_typedef.h"
int main() {
    char *str = "123";
    char *str2 = strdup(str);
    free(str2);
}