-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.h
More file actions
92 lines (86 loc) · 2.19 KB
/
Copy pathmessage.h
File metadata and controls
92 lines (86 loc) · 2.19 KB
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
/*
* message.h
*
* Created on: 2017年6月2日
* Author: zjbpoping
*/
#ifndef MESSAGE_H_
#define MESSAGE_H_
#include "node.h"
#include "sarray.h"
#include <string>
#include <vector>
namespace ps{
enum DataType {
CHAR, INT8, INT16, INT32, INT64,
UINT8, UINT16, UINT32, UINT64,
FLOAT, DOUBLE, OTHER
};
/** \brief data type name */
// static const char* DataTypeName[] = {
// "CHAR", "INT8", "INT16", "INT32", "INT64",
// "UINT8", "UINT16", "UINT32", "UINT64",
// "FLOAT", "DOUBLE", "OTHER"
// };
/**
* \brief compare if V and W are the same type
*/
template<typename V, typename W>
inline bool SameType() {
return std::is_same<typename std::remove_cv<V>::type, W>::value;
}
/**
* \brief return the DataType of V
*/
template<typename V>
DataType GetDataType() {
if (SameType<V, int8_t>()) {
return INT8;
} else if (SameType<V, int16_t>()) {
return INT16;
} else if (SameType<V, int32_t>()) {
return INT32;
} else if (SameType<V, int64_t>()) {
return INT64;
} else if (SameType<V, uint8_t>()) {
return UINT8;
} else if (SameType<V, uint16_t>()) {
return UINT16;
} else if (SameType<V, uint32_t>()) {
return UINT32;
} else if (SameType<V, uint64_t>()) {
return UINT64;
} else if (SameType<V, float>()) {
return FLOAT;
} else if (SameType<V, double>()) {
return DOUBLE;
} else {
return OTHER;
}
}
struct message{
/*消息类型*/
static const int empty = 0;
enum Command {EMPTY, ADD_NODE, TERMINATE, ACK, HEARTBEAT};
message():cmd(EMPTY),sender(empty),receiver(empty),
timestamp(empty),request(false),push(false),customer_id(empty){}
Command cmd;
int sender;
int receiver;
/*Lamport timestamp for message*/
int timestamp;
bool request;
bool push;
int customer_id;
/*向各个节点传输的数据类型及数据*/
std::vector<Node> node;
std::vector<DataType> data_type;
std::vector<SArray<char>> data;
template <typename V>
void AddData(const SArray<V>& val) {
data_type.push_back(GetDataType<V>());
data.push_back(SArray<char>(val));
}
};
}
#endif