wangzhengquan
2020-06-11 5c9cbf5ea152f501ae976e0e0b4ef5ee98afdfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#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";
const char *localUrl = "tcp://127.0.0.1:9988";
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 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
server()
{
    nng_socket sock;
    int        rv;
 
    if ((rv = nng_rep0_open(&sock)) != 0) {
        fatal("nng_rep0_open", rv);
    }
    if ((rv = nng_listen(sock, localUrl, 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);
        }
        std::cout << buf << std::endl;
 
        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);
        }
        // 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);
    }
}
 
 
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);
}