-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
174 lines (147 loc) · 5.13 KB
/
main.cpp
File metadata and controls
174 lines (147 loc) · 5.13 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
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
/*
+----------------------------------------------------------------------+
| The Isis Total Order Algorithm. |
+----------------------------------------------------------------------+
| http://dl.acm.org/citation.cfm?id=7478 |
+----------------------------------------------------------------------+
| This is an implementation of the ABCAST protocol, |
| as described in the paper (link given above). |
+----------------------------------------------------------------------+
| This source file is the entry point of the implementation. |
+----------------------------------------------------------------------+
*/
#include <cstring>
#include <fstream>
#include "Isis.h"
#define NOP 0
#define PORT 1
#define HOSTFILE 2
#define COUNT 3
#define MIN_PORT_NUM 1024
#define MAX_PORT_NUM 65535
#define HOST_NAME_LEN 256
using namespace std;
Isis *bootstrap(string, char *, int, int *); // Bootstraps the application.
void printUsage(); // Prints the usage.
// The show starts here!
int main(int argc, char **argv) {
int nextArg = NOP, count, portNum;
char *hostFilePath;
string port;
bool proceed = true;
// Parses the command line arguments and reads the values passed.
for(int i = 1; i < argc && proceed; i++) {
if(argv[i][0] == '-') {
if(strlen(argv[i]) != 2) {
proceed = false;
continue;
}
switch(argv[i][1]) {
case 'p':
nextArg = PORT;
break;
case 'h':
nextArg = HOSTFILE;
break;
case 'c':
nextArg = COUNT;
break;
default:
printUsage();
proceed = false;
}
} else {
switch(nextArg) {
case PORT:
port = string(argv[i]);
portNum = atoi(argv[i]);
if(portNum < MIN_PORT_NUM || portNum > MAX_PORT_NUM) {
cerr<<"The port number should lie between 1024 and 65535 including both.";
proceed = false;
continue;
}
break;
case HOSTFILE:
hostFilePath = argv[i];
break;
case COUNT:
count = atoi(argv[i]);
if(count < 0) {
proceed = false;
}
break;
case NOP:
printUsage();
proceed = false;
break;
}
}
}
// All OK. The command line arguments were fine.
if(proceed) {
int myId = -1;
Isis *isisNode = bootstrap(port, hostFilePath, count, &myId);
if(isisNode && myId >= 0) {
isisNode->start();
}
}
}
// Prints the usage.
void printUsage() {
cout<<"Incorrect usage.";
cout<<"\nUsage: proj2 -p port -h hostfile -c count";
cout.flush();
}
// Reads the host file and builds the required data structures and, instantiates the object.
Isis *bootstrap(string port, char *hostFilePath, int count, int *myId) {
int status, numProcs = 0;
uint32_t commanderId;
char myHostName[HOST_NAME_LEN];
vector<string> hostNames;
ifstream hostfile(hostFilePath);
Isis *isisNode = NULL;
*myId = -1;
// Read the hostfile and prepare the required data structures related to the hosts (generals).
if(hostfile.is_open()) {
if((status = gethostname(myHostName, HOST_NAME_LEN)) != 0) {
perror("Error encountered in fetching my host name.");
}
while(hostfile.good()) {
string hostName;
getline(hostfile, hostName);
if(!hostName.empty()) {
hostNames.push_back(hostName);
struct hostent *host = gethostbyname(hostName.c_str()); // Get the host address from host name.
if(host != NULL) {
struct in_addr hostAddress;
memcpy(&hostAddress, host->h_addr_list[0], sizeof(struct in_addr)); // Extract the IP adress from hostent structure.
}
if(strcmp(myHostName, hostName.c_str()) == 0) {
*myId = numProcs;
}
numProcs++;
}
}
hostfile.close();
}
if(*myId >= 0) {
ProcInfo *procInfo = new ProcInfo;
// Prepare the object with the required information to be passed to the constructors.
if(procInfo) {
procInfo->myId = (uint32_t) *myId;
procInfo->port = port;
procInfo->numProcs = numProcs;
procInfo->numMsgs = count;
procInfo->hostNames = hostNames;
try {
isisNode = new Isis(procInfo);
delete procInfo;
} catch(string msg) {
cerr<<msg;
}
}
} else {
cerr<<"\nMy hostname was not found in the file: "<<hostFilePath;
}
return isisNode;
}