wangzhengquan
2020-06-12 407bf8847c2ed391833909233c95faf66cda195b
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
182
183
184
185
186
187
188
189
190
191
192
#include "request_handler.h"
#include "netdisk_factory.h"
 
extern SafeQueue<Netdisk_DownloadRequest> task_queue;
 
extern std::map<std::string, Netdisk *> userDeviceMap;
 
extern LoginStore loginStore;
 
static inline 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) {
std::cout << "accepted login request" << std::endl;
  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) {
    netdisk = NetdiskFacotory::create(loginInfo.deviceType);
    if(netdisk != NULL) {
      userDeviceMap.insert({loginInfo.loginUUID, netdisk});
    } else {
      err_msg(0, "无法识别的设备类型: %s", loginInfo.deviceType.c_str());
    }
  }
 
  code = netdisk->login(loginInfo);
  if (code == 0) {
    loginStore.saveLoginInfo(loginInfo);
    std::cout << "起始通道:" << netdisk->getDeviceInfo().startChannel << ", 最大通道号:" << netdisk->getDeviceInfo().maxChannels << 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内部会释放buf
  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 handleDownloadByTimeAsync(nng_socket sock, Json::Value request) {
std::cout << "accepted handleDownloadByTime request" << std::endl;
  int rv;
  // char *buf;
  
  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();
 
  task_queue.push(drequest);
 
  
  Json::Value response;
  response["code"] = 0;
  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) {
    fatal("nng_send", rv);
    return rv;
  }
  return 0;
 
 
}
    
int handleDownloadByTime(nng_socket sock, Json::Value request) {
std::cout << "accepted handleDownloadByTime request" << std::endl;
  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<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());
    }
  }
 
  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;
 
 
}