-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerThread.h
More file actions
66 lines (58 loc) · 1.82 KB
/
ServerThread.h
File metadata and controls
66 lines (58 loc) · 1.82 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
#ifndef __SERVERTHREAD_H__
#define __SERVERTHREAD_H__
#include <condition_variable>
#include <future>
#include <mutex>
#include <queue>
#include <thread>
#include <vector>
#include <map>
#include <fstream>
#include <string>
#include <sstream>
#include "Messages.h"
#include "ServerSocket.h"
#include "ServerClientStub.h"
struct ExpertRequest
{
LaptopInfo laptop;
std::promise<LaptopInfo> prom;
};
class LaptopFactory
{
private:
std::queue<std::unique_ptr<ExpertRequest>> erq;
std::mutex erq_lock;
std::condition_variable erq_cv;
std::vector<MapOp> smr_log;
std::map<int, int> customer_record;
std::mutex smr_lock;
std::mutex cr_lock;
std::vector<std::pair<std::string, int>> replicas;
std::map<int, std::pair<std::string, int>> replica_id_to_ip_port;
std::vector<std::unique_ptr<ServerClientStub>> replica_stubs;
bool replicas_connections_made = false;
int last_index; // the last index of the smr_log that has data
int committed_index; // the last index of the smr_log where the
// MapOp of the log entry is committed and
// applied to the customer_record
int primary_id; // the production factory id ( server id ).
// initially set to -1.
int factory_id; // the id of the factory . This is assigned via
// the command line arguments .
LaptopInfo CreateRegularLaptop(CustomerRequest order, int engineer_id);
LaptopInfo CreateCustomLaptop(CustomerRequest order, int engineer_id);
void WriteToLogFile(MapOp op);
public:
void EngineerThread(std::unique_ptr<ServerSocket> socket, int id);
void ExpertThread(int id);
LaptopFactory();
void MakeReplicaConnections();
void SetFactoryId(int id);
void AddReplica(int id, std::string ip, int port);
void SetPrimaryId(int id);
void GetPrimaryId();
void RecoverReplica();
void RecoverFromLogFile();
};
#endif // end of #ifndef __SERVERTHREAD_H__