派生自 development/c++

pansen
2018-12-20 e72e8d1b83822a65abff787b41cb1fbc46343adb
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// Created by ps on 18-11-29.
//
 
#ifndef SYNCDBTOOL_ERLANGDBTOOL_H
#define SYNCDBTOOL_ERLANGDBTOOL_H
 
#include <iostream>
#include <cstring>
#include <unistd.h>
 
#include <sys/types.h>
#include <zconf.h>
#include <thread>
 
#include <map>
#include <vector>
#include <mutex>
 
#include <stdio.h>
#include <stdarg.h>
#include <jsoncpp/json/json.h>
#include <arpa/inet.h>
 
#include "erl_interface.h"
#include "ei.h"
 
#include "SyncDB.hpp"
 
#define TIMEOUT (5000)
#define BUFSIZE 1000
#define PORT 13001
 
 
//typedef void (*CBF)(std::string);
 
extern void erlangCallBackFunc(std::string);
 
//#TODO 多线程优化
namespace ErlangTool {
 
    static int m_loop = 0;
    //map<uuid,人脸特征信息>
    typedef std::map<std::string, SyncDB::Feature_Info> map_FaceDataCache;
    //map<uuid,设备信息>
    typedef std::map<std::string, SyncDB::Device_Info> map_DevDataCache;
    //map<表名,底库表信息>
    typedef std::map<std::string, SyncDB::Table_Info> map_TabDataCache;
    //map<表名,黑白名单类型>
    typedef std::map<std::string, SyncDB::Bw_Info> map_BwDataCache;
 
    typedef std::vector<SyncDB::AddPersRet> vec_AddDataCache;
    typedef std::vector<SyncDB::PerExistRet> vec_PerExistRet;
    typedef std::vector<SyncDB::UpdPersRet> vec_UpdPersRet;
    //c++11 互斥锁
    // LockG(Parma);
    typedef std::lock_guard<std::mutex> LockG;
 
    /***
     * 判断文件夹路径是否存在,不存在则创建
     * @param dir 文件路径
     * @param mkdir_flag 是否创建创建文件夹
     * @return 是否成功
     */
    static bool dir_file_exists(std::string dir, bool mkdir_flag = true);
 
    /***
     * 测试节点是否启动
     * @param nodeName 节点名称
     * @param cookie   节点cookie
     * @return 启动状态
     */
    static bool pingNode(const std::string &nodeName, const std::string &cookie);
 
    /***
     * 检查ETERM的数据类型
     * @param elemen
     * @return 返回数据类型
     */
    static int checkETERMType(ETERM *elemen);
 
    static void erlangFreeEterm(int count, ...);
 
    /***
     * Erlang交互工具
     * #todo map保存到内存?
     */
    class ErlangDbTool {
    public:
        explicit ErlangDbTool();
 
        /***
         * 初始化节点(zhizuo chaxun shiyong)
         * @param nodeName 节点名称
         * @param cookie 集群cookie
         */
        ErlangDbTool(const std::string &nodeName, const std::string &cookie, const std::string &pName);
 
        /***
         * 初始化节点
         * @param path 节点数据库保存路径
         * @param nodeName 节点名称
         * @param cookie 集群cookie
         */
        ErlangDbTool(const std::string &path, const std::string &nodeName, const std::string &cookie,
                     const std::string &pName);
 
        //clusterID clusterName
        /***
         * 初始化节点
         * @param path 节点数据库保存路径
         * @param nodeName 节点名称
         * @param cookie 集群cookie
         * @param clusterID 集群id
         * @param clusterName 集群名称
         */
        ErlangDbTool(const std::string &path, const std::string &nodeName, const std::string &cookie,
                     const std::string &clusterID, const std::string &clusterName);
 
        //clusterID clusterName
        /***
         * 初始化节点
         * @param path 节点数据库保存路径
         * @param nodeName 节点名称
         * @param cookie 集群cookie
         * @param clusterID 集群id
         * @param clusterName 集群名称
         * @param pName c节点标识名
         */
        ErlangDbTool(const std::string &path, const std::string &nodeName, const std::string &cookie,
                     const std::string &clusterID, const std::string &clusterName, const std::string &pName);
 
        virtual ~ErlangDbTool();
 
    private:
        void initCNode();
 
    public:
        //#todo setCNodeName
 
        bool setCNodeName();
 
        bool sendMessage();
 
        //#todo sendMessage
 
        void test(pthread_cond_t *, pthread_mutex_t *);
 
        /***
         * 启动erlang节点
         * @return 启动状态
         */
        int initErlang();
 
        std::string getConfigJsonString();
 
        /***
         * 启动节点数据库
         * 新节点自动创建数据库
         * @param FatherNodeName 第一个节点无需传入此参数,其他节点需要传入引导节点的名称
         * @param DeviceName 设备名称
         * @param DevAdd 设备地址
         * @return
         */
        bool startNodeDb(std::string FatherNodeName, std::string DeviceId = "", std::string DevAdd = "DevAddress");
 
 
        /***
         * 删除节点数据库,同时删除本地文件
         * @return 返回状态
         */
        bool removeNode();
 
 
        bool modifyCluName(std::string CluId, std::string CluName);
 
 
        /***
         * 查找在线节点信息
         * #TODO 有一个小bug
         * device_info.create_by会被重置
         * @return 节点集合信息
         */
        map_DevDataCache findAllNode();
 
        /***
         * 创建底库表
         * @param TableType car||person
         * @param TableName
         * @param SyncType true 1||false 0
         * @param BwType   "Black"1||"White"0
         * @return 创建状态
         */
        bool createDatabase(std::string TableType, std::string TableName, bool SyncType, std::string BwType,
                            std::string StartTime = "", std::string EndTime = "");
 
        /***
         * 更新底库表
         * @param TableType car||person
         * @param TableName
         * @param SyncType true 1||false 0
         * @param BwType   "white"0||"black"1
         * @return 创建状态
         */
        bool updateDatabase(std::string UUID, std::string TableType, std::string TableName, bool SyncType,
                            std::string BwType, std::string StartTime = "", std::string EndTime = "");
 
        /***
         * 删除数据表
         * @param TableType car||person
         * @param TableName biaoming
         * @param SyncType true 1||false 0
         * @return
         */
        bool deleteDatabase(std::string TableType, std::string TableName, bool SyncType);
 
        /***
         * 查询所有数据表
         * @return
         */
        map_TabDataCache findAllDatabase();
 
        /***
         * 查询所有表的黑白名单类型
         * @return
         */
        map_BwDataCache findAllTypeInfo();
 
        /***
         * 增加人员
         * @param UUID  out
         * @param TableName
         * @param Feature
         * @param PersonName
         * @param Age
         * @param Sex
         * @param IdCard
         * @param PhoneNum
         * @return
         */
        bool addPerson(std::string &UUID, std::string TableName, std::string Feature, std::string ImgUrl = " ",
                       std::string IdCard = " ", std::string PersonName = " ", std::string Age = " ",
                       std::string Sex = " ", std::string PhoneNum = " ");
 
        vec_AddDataCache addPersons(std::string TableUuid, std::vector<SyncDB::AddPersonInfo> &tmpPer);
 
        /***
         * 删除人员
         * @param UUID
         * @param TableName
         * @return
         */
        bool delPerson(std::string UUID, std::string TableName);
 
 
        /***
         * 根据表名加载人脸特征
         * @param TableName
         * @return
         */
        map_FaceDataCache loadFaceFeaData(std::string TableName);
 
 
        /***
         * 根据表名加载人脸特征2
         * @param TableName
         * @return
         */
        map_FaceDataCache loadFaceFeaData2(std::string TableName);
 
        vec_PerExistRet personIsExistOnClu(std::string personUid);
 
        vec_UpdPersRet singlePersonUpdate(std::string NewPerId, std::string OldId, std::string Idcard,
                                          std::string PerPicUrl, std::string PerFea);
//    private:
        /***
         * 重新连接节点
         * @return 连接状态
         */
        bool resetConn();
 
        /***
         * 等待节点启动,阻塞
         * @return
         */
        bool waitNode();
 
    public:
 
    private:
        //存储路径
        std::string m_path;
        std::string m_nodeName;
        std::string m_cNodeName;
        std::string m_pName;
        std::string m_cookie;
 
        std::string m_clusterID;
        std::string m_clusterName;
 
        bool m_ret;
        int m_fd;
 
        std::mutex m_mutex;
    };
 
    static int my_listen(int port);
 
}
 
 
#endif //SYNCDBTOOL_ERLANGDBTOOL_H