-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplain-server.cpp
59 lines (52 loc) · 1.43 KB
/
plain-server.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
55
56
57
58
59
#include <random>
#include <sstream>
#include "server.hpp"
#define BID_RANGE (1 << 15)
#define BIDDER_NUM 96
class PlainServer : public Server {
void handle(string msg, SSL* ssl) {
// deserialize
for (int i = 0; i < BIDDER_NUM; i++) {
int bid = atoi(msg.c_str());
}
srand(time(0));
// run a second price auction
vector<int> bids(BIDDER_NUM);
for (int i = 0; i < bids.size(); i++) {
bids[i] = rand() % BID_RANGE;
}
int max = bids[0];
int secondMax = bids[1];
int maxId = 0;
int secondId = 0;
for (int i = 0; i < bids.size(); i++) {
if (max < bids[i]) {
secondMax = max;
secondId = maxId;
max = bids[i];
maxId = i;
}
}
stringstream ss;
ss << maxId << " " << secondMax;
// return the winner and winning bid as response
string response = ss.str();
assert(sendMsg(ssl, response));
}
};
int main(int argc, char* argv[]) {
string ip = "0.0.0.0:6667";
// fixed 1 sec
double time_period = 1.0;
int opt;
while ((opt = getopt(argc, argv, "i:t:")) != -1) {
if (opt == 'i') {
ip = string(optarg);
} else if (opt == 't') {
time_period = atoi(optarg);
}
}
PlainServer server;
server.run(ip, time_period);
return 0;
}