wangzhengquan
2020-06-12 602442ecedf17800cddfdae188b05b40345cb011
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;
}