From 591aacee97f4a6486631c38a6b418e20b2c4109c Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 10 九月 2020 14:56:47 +0800
Subject: [PATCH] update

---
 service/netdisk_service.c |   63 +++++++++++++++++++++++--------
 1 files changed, 46 insertions(+), 17 deletions(-)

diff --git a/service/netdisk_service.c b/service/netdisk_service.c
index 1f55786..d6a0b19 100644
--- a/service/netdisk_service.c
+++ b/service/netdisk_service.c
@@ -9,6 +9,8 @@
 #include <nng/nng.h>
 #include <nng/protocol/reqrep0/rep.h>
 #include <nng/protocol/reqrep0/req.h>
+#include <nng/protocol/survey0/survey.h>
+#include <nng/protocol/survey0/respond.h>
 
 using namespace std;
 
@@ -95,18 +97,6 @@
   }
  
 
-  // Json::Value response;
-  // Json::Value payload;
-  // response["code"] = rv;
-  // response["msg"] = rmsg;
-
-  // Json::Value filelist;
-  // for(std::string f : files) {
-  //   filelist.append(f);
-  // }
-  // payload["filelist"] = filelist;
-  // response["payload"] = payload;
-
   Json::Value request;
   request["method"] = "downloadByTimeCallBack";
   Json::Value arguments;
@@ -120,8 +110,7 @@
   request["arguments"] = arguments;
   std::string str = request.toStyledString();
 
-  std::cout << "download finished, call back" << std::endl;
-  std::cout << str << std::endl;
+  std::cout << "SENDING download finished\n" << str << std::endl;
   connectAndSend(remoteUrl.c_str(), strdup(str.c_str()) );
   
   return 0;
@@ -162,7 +151,7 @@
     if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
       fatal("nng_recv", rv);
     }
-
+printf("RECEIVED RPC REQUEST:\n %s", buf);
     jsonreader.parse(buf, request);
     nng_free(buf, sz);
     std::string method = request["method"].asString();
@@ -196,6 +185,47 @@
     pthread_create(&tid, NULL, worker, NULL);
 }
 
+
+void heartBeat(const char *url, const char *name)
+{
+  nng_socket sock;
+  int rv;
+
+  if ((rv = nng_respondent0_open(&sock)) != 0) {
+    fatal("nng_respondent0_open", rv);
+  }
+  if ((rv = nng_dial(sock, url, NULL, NNG_FLAG_NONBLOCK)) != 0) {
+    fatal("nng_dial", rv);
+  }
+  for (;;) {
+    char *buf = NULL;
+    size_t sz;
+    if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) == 0) {
+//printf("CLIENT (%s): RECEIVED \"%s\" SURVEY REQUEST\n", name, buf);
+      nng_free(buf, sz);
+      char response[1024];
+      sprintf(response, "%s-%d", name, getpid());
+//printf("CLIENT (%s): SENDING SURVEY RESPONSE:%s\n", name, response);
+      if ((rv = nng_send(sock, response, strlen(response) + 1, 0)) != 0) {
+        fatal("nng_send", rv);
+      }
+    }
+  }
+}
+
+void *heart(void *vargp)
+{
+  pthread_detach(pthread_self());
+  heartBeat(config.get("heart_server").c_str(), "netdisk");
+  return NULL;
+}
+
+// 蹇冭烦鍙戦�佽繘绋�
+void initHeart() {
+  pthread_t tid;
+  pthread_create(&tid, NULL, heart, NULL);
+}
+
 int main()
 {
   //鐜鍙橀噺鍒濆鍖�
@@ -207,11 +237,10 @@
   hcEnvConfig.libpath = "../lib/hc";
   HCNetdisk::netdisk_init(&hcEnvConfig);
 
- 
-
   registRequestHandleFun();
 
   initThreadPool();
+  initHeart();
 
   startServer(config.get("server_url").c_str());
 

--
Gitblit v1.8.0