-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.cc
137 lines (124 loc) · 2.98 KB
/
server.cc
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
/*
* ===============================================================
* Description: Implementation of common::server methods.
*
* Created: 2014-02-08 17:30:01
*
* Author: Robert Escriva, [email protected]
* Ayush Dubey, [email protected]
*
* Copyright (C) 2013, Cornell University, see the LICENSE file
* for licensing agreement
* ===============================================================
*/
// Most of the following code has been 'borrowed' from
// Robert Escriva's HyperDex coordinator.
// see https://github.com/rescrv/HyperDex for the original code.
// e
#include <e/serialization.h>
// Weaver
#include "common/server.h"
const char*
server :: to_string(state_t state)
{
switch (state)
{
case ASSIGNED:
return "ASSIGNED";
case NOT_AVAILABLE:
return "NOT_AVAILABLE";
case AVAILABLE:
return "AVAILABLE";
case SHUTDOWN:
return "SHUTDOWN";
case KILLED:
return "KILLED";
default:
return "UNKNOWN";
}
}
const char*
server :: to_string(type_t type)
{
switch (type)
{
case UNDEF:
return "UNDEF";
case SHARD:
return "SHARD";
case VT:
return "VT";
case BACKUP_SHARD:
return "BACKUP_SHARD";
case BACKUP_VT:
return "BACKUP_VT";
default:
return "BADTYPE";
}
}
server :: server()
: state(KILLED)
, id()
, weaver_id(UINT64_MAX)
, virtual_id(UINT64_MAX)
, type(UNDEF)
, bind_to()
{
}
server :: server(const server_id& sid)
: state(ASSIGNED)
, id(sid)
, weaver_id(UINT64_MAX)
, virtual_id(UINT64_MAX)
, type(UNDEF)
, bind_to()
{
}
bool
server :: operator==(const server &other) const
{
return state == other.state
&& id == other.id
&& weaver_id == other.weaver_id
&& virtual_id == other.virtual_id
&& type == other.type
&& bind_to == other.bind_to;
}
bool
server :: operator!=(const server &other) const
{
return !(*this == other);
}
bool
operator < (const server& lhs, const server& rhs)
{
return lhs.id < rhs.id;
}
e::packer
operator << (e::packer lhs, const server& rhs)
{
uint8_t s = static_cast<uint8_t>(rhs.state);
uint8_t t = static_cast<uint8_t>(rhs.type);
return lhs << s << rhs.id << rhs.weaver_id << rhs.virtual_id << t << rhs.bind_to;
}
e::unpacker
operator >> (e::unpacker lhs, server& rhs)
{
uint8_t s, t;
lhs = lhs >> s >> rhs.id >> rhs.weaver_id >> rhs.virtual_id >> t >> rhs.bind_to;
rhs.state = static_cast<server::state_t>(s);
rhs.type = static_cast<server::type_t>(t);
return lhs;
}
size_t
pack_size(const server& p)
{
uint8_t s = 0;
uint8_t t = 0;
return e::pack_size(s)
+ pack_size(p.id)
+ e::pack_size(p.weaver_id)
+ e::pack_size(p.virtual_id)
+ e::pack_size(t)
+ e::pack_size(p.bind_to);
}