Newer
Older
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
// -*- mode: c++; c-file-style: "k&r"; c-basic-offset: 4 -*-
/***********************************************************************
*
* timeserver/timeserver.cc:
* Single TimeStamp Server.
*
**********************************************************************/
#include "timeserver/timeserver.h"
TimeStampServer::TimeStampServer()
{
ts = 0;
}
TimeStampServer::~TimeStampServer() { }
string
TimeStampServer::newTimeStamp()
{
ts++;
return to_string(ts);
}
void
TimeStampServer::ReplicaUpcall(opnum_t opnum,
const string &str1,
string &str2)
{
Debug("Received Upcall: %lu, %s", opnum, str1.c_str());
// Get a new timestamp from the TimeStampServer
str2 = newTimeStamp();
}
static void Usage(const char *progName)
{
fprintf(stderr, "usage: %s -c conf-file -i replica-index\n",
progName);
exit(1);
}
int
main(int argc, char **argv)
{
int index = -1;
const char *configPath = NULL;
// Parse arguments
int opt;
while ((opt = getopt(argc, argv, "c:i:")) != -1) {
switch (opt) {
case 'c':
configPath = optarg;
break;
case 'i':
{
char *strtolPtr;
index = strtoul(optarg, &strtolPtr, 10);
if ((*optarg == '\0') || (*strtolPtr != '\0') || (index < 0))
{
fprintf(stderr,
"option -i requires a numeric arg\n");
Usage(argv[0]);
}
break;
}
default:
fprintf(stderr, "Unknown argument %s\n", argv[optind]);
break;
}
}
if (!configPath) {
fprintf(stderr, "option -c is required\n");
Usage(argv[0]);
}
if (index == -1) {
fprintf(stderr, "option -i is required\n");
Usage(argv[0]);
}
// Load configuration
std::ifstream configStream(configPath);
if (configStream.fail()) {
fprintf(stderr, "unable to read configuration file: %s\n",
configPath);
Usage(argv[0]);
}
specpaxos::Configuration config(configStream);
if (index >= config.n) {
fprintf(stderr, "replica index %d is out of bounds; "
"only %d replicas defined\n", index, config.n);
Usage(argv[0]);
}
UDPTransport transport(0.0, 0.0, 0);
TimeStampServer server;
specpaxos::vr::VRReplica replica(config, index, &transport, 1, &server);
transport.Run();
return 0;
}