-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerMain.cpp
54 lines (46 loc) · 1.45 KB
/
ServerMain.cpp
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
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include "ServerSocket.h"
#include "ServerThread.h"
int main(int argc, char *argv[]) {
int port;
int engineer_cnt = 0;
int adminId = -1;
int numPeerServers = 0;
ServerSocket socket;
LaptopFactory factory;
std::unique_ptr<ServerSocket> new_socket;
std::vector<std::thread> thread_vector;
if (argc < 4) {
std::cout << "not enough arguments" << std::endl;
std::cout << argv[0] << "[port #] [unique ID] [# peers] (repeat [ID] [IP] [port #])" << std::endl;
return 0;
}
port = atoi(argv[1]);
adminId = atoi(argv[2]);
numPeerServers = atoi(argv[3]);
if(argc < 4 + numPeerServers*3) {
std::cout << "not enough arguments" << std::endl;
std::cout << argv[0] << "[port #] [unique ID] [# peers] (repeat [ID] [IP] [port #])" << std::endl;
return 0;
}
if (!socket.Init(port)) {
std::cout << "Socket initialization failed" << std::endl;
return 0;
}
for (int i = 0; i < numPeerServers; i++) {
int id = atoi(argv[4 + i*3]);
std::string ip = argv[5 + i*3];
int portNum = atoi(argv[6 + i*3]);
factory.adminMap[id] = std::make_pair(ip, portNum);
}
thread_vector.push_back(std::thread(&LaptopFactory::AdminThread, &factory, adminId));
while ((new_socket = socket.Accept())) {
std::thread engineer_thread(&LaptopFactory::EngineerThread, &factory, std::move(new_socket), engineer_cnt++);
thread_vector.push_back(std::move(engineer_thread));
}
return 0;
}