-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlock_smain.cc
96 lines (83 loc) · 2.24 KB
/
lock_smain.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
#include "rpc.h"
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#ifdef RSM
#include "lock_server_cache_rsm.h"
#else
#include "lock_server_cache.h"
#endif
#include "paxos.h"
#include "rsm.h"
#include "jsl_log.h"
// Main loop of lock_server
static void
force_exit(int) {
exit(0);
}
int
main(int argc, char *argv[])
{
int count = 0;
// Force the lock_server to exit after 20 minutes
signal(SIGALRM, force_exit);
alarm(20 * 60);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
srandom(getpid());
#ifdef RSM
if (argc != 3) {
fprintf(stderr, "Usage: %s [master:]port [me:]port\n", argv[0]);
if (argc == 2) {
fprintf(stderr, "Only one port is specified, fall back to single node mode.\n");
argv[2] = argv[1];
argc = 3;
} else {
exit(1);
}
}
#else
if (argc != 2) {
fprintf(stderr, "Usage: %s port\n", argv[0]);
exit(1);
}
#endif
char *count_env = getenv("RPC_COUNT");
if (count_env != NULL) {
count = atoi(count_env);
}
// jsl_set_debug(2);
// Comment out the next line to switch between the ordinary lock
// server and the RSM. In Lab 6, we disable the lock server and
// implement Paxos. In Lab 7, we will make the lock server use your
// RSM layer.
#ifdef RSM
// You must comment out the next line once you are done with Step One.
// #define STEP_ONE
#ifdef STEP_ONE
rpcs server(atoi(argv[1]));
lock_server_cache_rsm ls;
server.reg(lock_protocol::acquire, &ls, &lock_server_cache_rsm::acquire);
server.reg(lock_protocol::release, &ls, &lock_server_cache_rsm::release);
#else
rsm rsm(argv[1], argv[2]);
lock_server_cache_rsm ls(&rsm);
rsm.set_state_transfer((rsm_state_transfer *) &ls);
rsm.reg(lock_protocol::acquire, &ls, &lock_server_cache_rsm::acquire);
rsm.reg(lock_protocol::release, &ls, &lock_server_cache_rsm::release);
#endif // STEP_ONE
#endif // RSM
#ifndef RSM
lock_server_cache ls;
rpcs server(atoi(argv[1]), count);
server.reg(lock_protocol::stat, &ls, &lock_server_cache::stat);
server.reg(lock_protocol::acquire, &ls, &lock_server_cache::acquire);
server.reg(lock_protocol::release, &ls, &lock_server_cache::release);
#endif
while (true) {
sleep(1000);
}
return 0;
}