-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreManager.cpp
More file actions
113 lines (95 loc) · 4.41 KB
/
StoreManager.cpp
File metadata and controls
113 lines (95 loc) · 4.41 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
/*
* File: Store.cpp
* Author: debian
*
* Created on 21. Juni 2017, 08:56
*/
#include "StoreManager.h"
StoreManager::StoreManager(int port, std::string* items, int* prices, unsigned long n, std::string brokerIp, int clientID, int startStock)
: client(std::string("store" + std::to_string(clientID)).c_str(), brokerIp.c_str(), this), stop(false)
{
for (int i = 0; i < n; i++) {
itemPrices.insert(std::pair<std::string, int>(items[i], prices[i]));
itemStock.insert(std::pair<std::string, int>(items[i], startStock));
demandCount.insert(std::pair<std::string, int>(items[i], 0));
client.subscribe(std::string(OFFER_PREFIX + items[i]).c_str());
}
client.subscribe(CONFIRM_CHANNEL);
serverThread = new std::thread(StoreHandler::startStoreServer, port, &itemPrices, &itemStock, &ordersReceived);
}
StoreManager::~StoreManager() {
delete serverThread;
}
void StoreManager::startLoop() {
while(true) {
if(stop)
exit(EXIT_SUCCESS);
client.loop();
for(auto i = itemStock.cbegin(); i != itemStock.cend(); i++) {
std::string item = i->first;
unsigned int stock = i->second;
if(stock < INITIAL_STOCK*0.8) {
if(demandCount.at(item) % OFFER_DELAY == 0) {
MQTTClient::Offer offer;
strncpy(offer.item, item.c_str(), MAX_ITEM_LEN);
offer.price = itemPrices.at(item)/2 + (demandCount.at(item)/OFFER_DELAY)*PRICE_INCREASE;
offer.amount = INITIAL_STOCK - stock;
client.publish(std::string(DEMAND_PREFIX + item).c_str(), &offer, sizeof(offer));
printf("%s: Sent demand for %d %s at %d each\n", client.getID(), offer.amount, offer.item, offer.price);
}
demandCount.at(item)++;
}
}
//printf("Store alive\n");
}
serverThread->join();
}
void StoreManager::on_mosqEvent(const char* channel, const void* msg) {
MQTTClient::Offer* offer = (MQTTClient::Offer*)msg;
//printf("Offer: %d times %s for %d each\n", offer->amount, offer->item, offer->price);
if(strstr(channel, OFFER_PREFIX)) {
const char* item = channel + strlen(OFFER_PREFIX);
//printf("Received offer for %s\n", item);
if(!strcmp(item, offer->item) && itemStock.at(item) < INITIAL_STOCK && !isActiveOrder(offer) && offer->price <= (itemPrices.at(item)/2 + (demandCount.at(item)/OFFER_DELAY)*PRICE_INCREASE)) {
MQTTClient::Offer *order;
if(offer->amount <= INITIAL_STOCK-itemStock.at(item))
order = offer;
else {
order = new MQTTClient::Offer;
strncpy(order->item, item, MAX_ITEM_LEN);
order->price = offer->price;
order->amount = INITIAL_STOCK-itemStock.at(item);
}
if(client.publish(ORDER_CHANNEL, order, sizeof(*order))) {
strncpy(activeOrder.item, order->item, MAX_ITEM_LEN);
activeOrder.price = order->price;
activeOrder.amount = order->amount;
if(order->amount != offer->amount)
delete order;
printf("%s: Received offer for and ordered %d %s at %d each\n", client.getID(), order->amount, offer->item, offer->price);
}
}
} else if(strstr(channel, CONFIRM_CHANNEL)) {
if(!strncmp(offer->item, activeOrder.item, MAX_ITEM_LEN) && offer->price == activeOrder.price && offer->amount == activeOrder.amount) {
itemStock.at(offer->item) += offer->amount;
demandCount.at(offer->item) = 0;
*activeOrder.item = 0;
printf("%s: Restocked %s by %d to %d for %d each\n", client.getID(), offer->item, offer->amount, itemStock.at(offer->item), offer->price);
}
} else {
printf("%s: Ignored offer for %d %s at %d each\n", client.getID(), offer->amount, offer->item, offer->price);
}
}
bool StoreManager::isActiveOrder(MQTTClient::Offer* offer) const {
bool equal = true;
if(strncmp(activeOrder.item, offer->item, MAX_ITEM_LEN))
equal = false;
else if(activeOrder.price != offer->price)
equal = false;
else if(activeOrder.amount != offer->amount)
equal = false;
return equal;
}
void StoreManager::stopLoop() {
stop = true;
}