-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathService.cpp
725 lines (674 loc) · 17.9 KB
/
Service.cpp
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#include "StdAfx.h"
#include "Service.h"
#include "ace/Message_Block.h"
#include "threadpoolw.h"
#include "SqlHandler.h"
#include "LogFilew.h"
#include "MyDataCommon.h"
#include "PlugIn.h"
#include "net.pb.h"
#include "points.pb.h"
#include "datas.pb.h"
#include "DataMgr.h"
extern threadpool _tp;
extern SqlHandler _sql_handler;
extern DataMgr _dm;
extern PlugIn _plugin;
class PointExistFun : public MyDataFun
{
public:
virtual int MyDataCallBack(void* para)
{
SqlHandler* sql_handler = (SqlHandler*)data_;
int *point_id = (int*)para;
return sql_handler->IsPointExist(*point_id);
}
};
void MyDataServiceHandler::open(ACE_HANDLE h, ACE_Message_Block&)
{
handle(h);
if (reader_.open(*this) != 0 || writer_.open(*this) != 0)
{
delete this;
return;
}
init_alloc_root(&root_, 4096, 4096);
if (read_head())
{
LOG_ERROR_WRITE("读取网络数据失败");
delete this;
return;
}
return;
}
int MyDataServiceHandler::read_head()
{
int heads_size = proto_handler::get_heads_size();
char* headbuf = (char*)alloc_root(&root_, heads_size);
mb_.init(headbuf, heads_size);
ishead_ = true;
if (reader_.read(mb_, heads_size) != 0)
return -1;
return MYDATA_OK;
}
void MyDataServiceHandler::handle_read_stream(const ACE_Asynch_Read_Stream::Result &result)
{
int trans = result.bytes_transferred(); //实际读取的字节数
int want = result.bytes_to_read(); //想要读取的字节数
if (!result.success() || trans==0)
{
delete this;
return;
}
if (trans < want)
{
if (reader_.read(mb_, want-trans))
{
LOG_ERROR_WRITE("读取网络数据失败");
delete this;
}
return;
}
thread_task task;
task.callback_fun = handle_input_task;
task.data = this;
_tp.puttask(task);
}
void MyDataServiceHandler::handle_write_stream(const ACE_Asynch_Write_Stream::Result &result)
{
int length = mb_.length();
if (length > 0)
{
if (writer_.write(mb_, length))
{
LOG_ERROR_WRITE("发送网络数据失败");
delete this;
}
return;
}
free_root(&root_, MY_MARK_BLOCKS_FREE);
if (read_head())
{
LOG_ERROR_WRITE("读取网络数据失败");
delete this;
}
}
void* MyDataServiceHandler::handle_input_task(void* data)
{
MyDataServiceHandler *h = (MyDataServiceHandler*)data;
if (h->ishead_)
{
h->handle_head();
}
else
{
h->handle_body();
}
return 0;
}
int MyDataServiceHandler::handle_head()
{
char *headbuf = mb_.rd_ptr();
int ret = proto_handler::recv_head_proto(head_, headbuf);
if (ret)
{
LOG_ERROR_WRITE("接收头失败, 错误码:%d", ret);
delete this;
return ret;
}
ishead_ = false;
int bodysize = head_.bodysize();
mb_.init((const char*)alloc_root(&root_, bodysize), bodysize);
if (reader_.read(mb_, bodysize) != 0)
{
LOG_ERROR_WRITE("接收头失败, 错误码:%d", ret);
delete this;
return -1;
}
return ret;
}
int MyDataServiceHandler::handle_body()
{
int ret = MYDATA_OK;
switch (head_.bodytype())
{
case MsgWriteNewDatasQuery:
ret = handle_write_new_datas_msg();
break;
case MsgReadNewDatasQuery:
ret = handle_read_new_datas_msg();
break;
case MsgReadDatasNumQuery:
ret = handle_read_datas_num_msg();
break;
case MsgReadDatasQuery:
ret = handle_read_datas_msg();
break;
case MsgWriteOldDatasQuery:
ret = handle_write_old_datas_msg();
break;
case MsgConnectQuery:
ret = handle_connect_msg();
break;
case MsgAddTypeQuery:
ret = handle_add_type_msg();
break;
case MsgDeleteTypeQuery:
ret = handle_delete_type_msg();
break;
case MsgGetTypesNumQuery:
ret = handle_get_typesnum_msg();
break;
case MsgGetTypesQuery:
ret = handle_get_types_msg();
break;
case MsgAddTableQuery:
ret = handle_add_table_msg();
break;
case MsgDeleteTableQuery:
ret = handle_delete_table_msg();
break;
case MsgGetTablesNumQuery:
ret = handle_get_tablesnum_msg();
break;
case MsgGetTablesQuery:
ret = handle_get_tables_msg();
break;
case MsgGetTableByIDQuery:
ret = handle_get_table_byid_msg();
break;
case MsgAddPointQuery:
ret = handle_add_points_msg();
break;
case MsgDeletePointQuery:
ret = handle_delete_point_msg();
break;
case MsgSearchPointsNumQuery:
ret = handle_search_pointsnum_msg();
break;
case MsgSearchPointsQuery:
ret = handle_search_points_msg();
break;
case MsgSearchPointsNumBYIDQuery:
ret = handle_search_pointsnum_byid_msg();
break;
case MsgSearchPointsByIDQuery:
ret = handle_search_points_byid_msg();
break;
case MsgGetTypeByIDQuery:
ret = handle_get_type_byid_msg();
break;
case MsgGetDataFileNumsQuery:
ret = handle_get_datafile_nums_msg();
break;
case MsgDataFileInfosQuery:
ret = handle_get_datafile_infos_msg();
break;
case MsgRemoveDatasQuery:
ret = handle_remove_datas_msg();
break;
case MsgAddFileQuery:
ret = handle_add_file_msg();
break;
case MsgCreateFileQuery:
ret = handle_create_file_msg();
break;
case MsgDeleteFileQuery:
ret = handle_delete_file_msg();
break;
case MsgRebuildIndexQuery:
ret = handle_rebuild_index_msg();
break;
default:
LOG_ERROR_WRITE("没有消息类型%d", head_.bodytype());
ret = MYDATA_ERROR_TYPE;
}
if (ret < 0)
{
//if (ret != MYDATA_CRC_FAIL)
{
LOG_ERROR_WRITE("处理体消息失败, 错误码:%d", ret);
delete this;
}
}
return ret;
}
int MyDataServiceHandler::handle_connect_msg()
{
char *bodybuf = mb_.rd_ptr();
MydataNet::ConnectQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
MydataNet::ConnectReply reply;
reply.set_err(0);
ret = send_all(reply, MsgConnectReply);
return ret;
}
int MyDataServiceHandler::handle_add_type_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::AddTypeQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::AddTypeReply reply;
_sql_handler.AddType(query, reply);
ret = send_all(reply, MsgAddTypeReply);
return ret;
}
int MyDataServiceHandler::handle_delete_type_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::DeleteTypeQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::DeleteTypeReply reply;
_sql_handler.DeleteType(query, reply);
ret = send_all(reply, MsgDeleteTypeReply);
return ret;
}
int MyDataServiceHandler::handle_get_typesnum_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::GetTypesNumQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::GetTypesNumReply reply;
_sql_handler.GetTypesNum(reply);
ret = send_all(reply, MsgGetTypesNumReply);
return ret;
}
int MyDataServiceHandler::handle_get_types_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::GetTypesQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::GetTypesReply reply;
_sql_handler.GetTypes(query, reply);
ret = send_all(reply, MsgGetTypesReply);
return ret;
}
int MyDataServiceHandler::handle_get_type_byid_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::GetTypeByIDQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::GetTypeByIDReply reply;
_sql_handler.GetTypeByID(query, reply);
ret = send_all(reply, MsgGetTypeByIDReply);
return ret;
}
int MyDataServiceHandler::handle_add_table_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::AddTableQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::AddTableReply reply;
_sql_handler.AddTable(query, reply);
ret = send_all(reply, MsgAddTableReply);
return ret;
}
int MyDataServiceHandler::handle_delete_table_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::DeleteTableQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::DeleteTableReply reply;
_sql_handler.DeleteTable(query, reply);
ret = send_all(reply, MsgDeleteTableReply);
return ret;
}
int MyDataServiceHandler::handle_get_tablesnum_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::GetTablesNumQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::GetTablesNumReply reply;
_sql_handler.GetTablesNum(reply);
ret = send_all(reply, MsgGetTablesNumReply);
return ret;
}
int MyDataServiceHandler::handle_get_tables_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::GetTablesQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::GetTablesReply reply;
_sql_handler.GetTables(query, reply);
ret = send_all(reply, MsgGetTablesReply);
return ret;
}
int MyDataServiceHandler::handle_get_table_byid_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::GetTableByIDQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::GetTableByIDReply reply;
_sql_handler.GetTableByID(query, reply);
ret = send_all(reply, MsgGetTableByIDReply);
return ret;
}
int MyDataServiceHandler::handle_add_points_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::AddPointsQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::AddPointsReply reply;
_sql_handler.AddPoints(query, reply);
ret = send_all(reply, MsgAddPointReply);
return ret;
}
int MyDataServiceHandler::handle_delete_point_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::DeletePointQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::DeletePointReply reply;
_sql_handler.DeletePoint(query, reply);
//_dm.RemovePoint(query.id());
//_sql_handler.FinishDeletePoint(query, reply);
ret = send_all(reply, MsgDeletePointReply);
return ret;
}
int MyDataServiceHandler::handle_search_pointsnum_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::SearchPointsNumQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::SearchPointsNumReply reply;
_sql_handler.SearchPointsNum(query, reply);
ret = send_all(reply, MsgSearchPointsNumReply);
return ret;
}
int MyDataServiceHandler::handle_search_points_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::SearchPointsQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::SearchPointsReply reply;
_sql_handler.SearchPoints(query, reply);
ret = send_all(reply, MsgSearchPointsReply);
return ret;
}
int MyDataServiceHandler::handle_search_pointsnum_byid_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::SearchPointsNumByIDQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::SearchPointsNumReply reply;
_sql_handler.SearchPointsNumByID(query, reply);
ret = send_all(reply, MsgSearchPointsNumReply);
return ret;
}
int MyDataServiceHandler::handle_search_points_byid_msg()
{
char *bodybuf = mb_.rd_ptr();
Points::SearchPointsByIDQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Points::SearchPointsReply reply;
_sql_handler.SearchPointsByID(query, reply);
ret = send_all(reply, MsgSearchPointsReply);
return ret;
}
int MyDataServiceHandler::handle_write_new_datas_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::WriteNewDatasQuery &query = write_new_query_;
query.Clear();
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Datas::WriteNewDatasReply reply;
reply.set_err(MYDATA_OK);
auto array = query.data_infos();
int num = 0;
PointExistFun function;
function.data_ = &_sql_handler;
if (_plugin.WriteNewDataFuns_.empty())
{//没有插件
for (auto iter = array.begin(); iter != array.end(); ++iter)
{
ret = _dm.WriteNewData(iter->id(), iter->time_stamp(), iter->data().c_str(), iter->data().length(), &function);
if (ret)
reply.set_err(ret);
else
num++;
}
}
else
{//有插件
bool write_in = true;
for (auto iter = array.begin(); iter != array.end(); ++iter)
{
for (auto fun_iter = _plugin.WriteNewDataFuns_.begin(); fun_iter != _plugin.WriteNewDataFuns_.end(); ++fun_iter)
{
if (!(*fun_iter)(iter->table_id(), iter->id(), iter->time_stamp(), iter->data().c_str()))
{
write_in = false;
reply.set_err(MYDATA_PLUGIN_PREVENT);
break;
}
}
if (write_in)
{
ret = _dm.WriteNewData(iter->id(), iter->time_stamp(), iter->data().c_str(), iter->data().length(), &function);
if (ret)
reply.set_err(ret);
else
num++;
}
else
write_in = true;
}
}
reply.set_num(num);
ret = send_all(reply, MsgWriteNewDatasReply);
return ret;
}
int MyDataServiceHandler::handle_read_new_datas_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::ReadNewDatasQuery &query = read_new_query_;
query.Clear();
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Datas::ReadNewDatasReply &reply = read_new_reply_;
reply.Clear();
auto ids_array = query.ids();
auto datas_infos_array = reply.mutable_data_infos();
reply.set_err(MYDATA_OK);
datas_infos_array->Reserve(ids_array.size());
int num = 0;
time_t t = -1;
char data_buf[MAX_DATA_SIZE] = {0};
int length = 1;
for (auto iter = ids_array.begin(); iter != ids_array.end(); ++iter)
{
ret = _dm.ReadNewData(*iter, t, data_buf, length);
auto data_info = datas_infos_array->Add();
data_info->set_data(data_buf, length);
data_info->set_time_stamp(t);
if (ret == MYDATA_OK)
{
t = -1;
data_buf[0] = '\0';
length = 1;
num++;
}
else
{
reply.set_err(MYDATA_DATA_PAR_SUC);
}
}
reply.set_num(num);
ret = send_all(reply, MsgReadNewDatasReply);
return ret;
}
int MyDataServiceHandler::handle_read_datas_num_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::ReadDatasNumQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Datas::ReadDatasNumReply reply;
int num = 0;
ret = _dm.ReadDatasNum(query.id(), query.time_begin(), query.time_end(), num);
reply.set_err(ret);
reply.set_num(num);
ret = send_all(reply, MsgReadDatasNumReply);
return ret;
}
int MyDataServiceHandler::handle_read_datas_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::ReadDatasQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Datas::ReadDatasReply &reply = read_reply_;
reply.Clear();
ret = _dm.ReadDatas(query.id(), query.time_begin(), query.from_time(), reply);
reply.set_err(ret);
ret = send_all(reply, MsgReadDatasReply);
return ret;
}
int MyDataServiceHandler::handle_get_datafile_nums_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::GetDataFileNumsQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Datas::GetDataFileNumsReply reply;
reply.set_num(_dm.GetFilesNum());
reply.set_err(MYDATA_OK);
ret = send_all(reply, MsgGetDataFileNumsReply);
return ret;
}
int MyDataServiceHandler::handle_get_datafile_infos_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::DataFileInfosQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
Datas::DataFileInfosReply reply;
_dm.GetFileInfos(reply);
reply.set_err(MYDATA_OK);
ret = send_all(reply, MsgDataFileInfosReply);
return ret;
}
int MyDataServiceHandler::handle_write_old_datas_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::WriteOldDatasQuery &query = write_old_query_;
query.Clear();
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
int num = 0;
PointExistFun function;
function.data_ = &_sql_handler;
ret = _dm.WriteOldDatas(query.id(), query, num, &function);
Datas::WriteOldDatasReply reply;
reply.set_err(ret);
reply.set_num(num);
ret = send_all(reply, MsgWriteOldDatasReply);
return ret;
}
int MyDataServiceHandler::handle_remove_datas_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::RemoveDatasQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
ret = _dm.RemoveDatas(query);
Datas::RemoveDatasReply reply;
reply.set_err(ret);
ret = send_all(reply, MsgRemoveDatasReply);
return ret;
}
int MyDataServiceHandler::handle_add_file_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::AddFileQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
ret = _dm.AddFile(query.filename().c_str());
Datas::AddFileReply reply;
reply.set_err(ret);
ret = send_all(reply, MsgAddFileReply);
return ret;
}
int MyDataServiceHandler::handle_create_file_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::CreateFileQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
ret = _dm.CreateFile(query.filename().c_str(), query.size(), query.start_time(), query.end_time());
Datas::CreateFileReply reply;
reply.set_err(ret);
ret = send_all(reply, MsgCreateFileReply);
return ret;
}
int MyDataServiceHandler::handle_delete_file_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::DeleteFileQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
ret = _dm.DeleteFile(query.file_name().c_str(), query.start_time());
Datas::DeleteFileReply reply;
reply.set_err(ret);
ret = send_all(reply, MsgDeleteFileReply);
return ret;
}
int MyDataServiceHandler::handle_rebuild_index_msg()
{
char *bodybuf = mb_.rd_ptr();
Datas::RebuildIndexQuery query;
int ret = proto_handler::recv_body_proto(head_, query, bodybuf);
if (ret)
return ret;
ret = _dm.ReBuildIndex(query.filename().c_str(), query.start_time());
Datas::RebuildIndexReply reply;
reply.set_err(ret);
ret = send_all(reply, MsgRebuildIndexReply);
return ret;
}