-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
141 lines (107 loc) · 4.16 KB
/
main.cpp
File metadata and controls
141 lines (107 loc) · 4.16 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: tim
*
* Created on 20. April 2017, 10:27
*/
#include <cstdlib>
#include <iostream>
#include <thread>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "StoreManager.h"
#include "Server.h"
#include "Sensor.h"
#include "HttpServer.h"
#include "Producer.h"
#define DEFAULT_PORT 27015
#define STORE_COUNT 2
#define STORE_ITEMS "Cheese", "Bread", "Milk", "Juice"
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
string items[] = {STORE_ITEMS};
int prices[STORE_COUNT][4] = {{ 220, 150, 130, 180},
{ 200, 155, 120, 200}};
string resources[] = {STORE_ITEMS, "history"};
RESTManager manager(resources, 5);
manager.initStructure();
int externalStoreN = stoi(manager.getConfig("ExternalStoreN"));
string storeIps[STORE_COUNT+externalStoreN];
int storePorts[STORE_COUNT+externalStoreN];
//Local stores
StoreManager* stores[STORE_COUNT];
int port = stoi(manager.getConfig("Store1Port"));
string brokerIp = manager.getConfig("BrokerIp");
for(int i = 0; i < STORE_COUNT; i++) {
storeIps[i] = manager.getConfig("Store1Ip");
storePorts[i] = port++;
stores[i] = new StoreManager(storePorts[i], items, prices[i], sizeof(prices[i]) / sizeof(int), brokerIp, i);
}
//Exernal stores
string externalStores = manager.getConfig("ExternalStores");
for(int i = STORE_COUNT; i < STORE_COUNT+externalStoreN; i++) {
unsigned int pos = externalStores.find(',');
string store = externalStores.substr(0, pos);
unsigned int portDelim = store.find(':');
storeIps[i] = store.substr(0, portDelim);
storePorts[i] = stoi(store.substr(portDelim+1, store.find(',')-(portDelim+1)));
externalStores.erase(0, pos+1);
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(stoi(manager.getConfig("HttpServerPort")));
HttpServer* httpServer = new HttpServer(addr, manager);
addr.sin_port = htons(stoi(manager.getConfig("ServerPort")));
Server* server = new Server(addr, manager, storeIps, storePorts, STORE_COUNT+externalStoreN);
httpServer->addObserver(&Server::restock);
thread* storeServerThreads[STORE_COUNT];
for(int i = 0; i < STORE_COUNT; i++) {
storeServerThreads[i] = new thread(&StoreManager::startLoop, stores[i]);
}
sleep(1);
thread serverThread(&Server::receive, server);
thread httpServerThread(&HttpServer::start, httpServer);
sleep(1);
addr.sin_addr.s_addr = inet_addr(manager.getConfig("ServerIp").c_str());
Sensor* sensors[] = {new Sensor("Cheese", addr), new Sensor("Bread", addr), new Sensor("Milk", addr), new Sensor("Juice", addr)};
thread* sensorThreads[4];
for(int i = 0; i < 4; i++)
sensorThreads[i] = new thread(&Sensor::send, sensors[i]);
const char* const addr_broker = manager.getConfig("BrokerIp").c_str();
const char* farmProducts[] = {"Milk", "Cheese"};
const char* marketProducts[] = {"Bread", "Juice"};
Producer farm1("farm1", addr_broker, farmProducts, 2);
Producer farm2("farm2", addr_broker, farmProducts, 2);
Producer market1("markt1", addr_broker, marketProducts, 2);
Producer market2("markt2", addr_broker, marketProducts, 2);
thread tFarm1(&Producer::start, &farm1);
thread tFarm2(&Producer::start, &farm2);
thread tMarket1(&Producer::start, &market1);
thread tMarket2(&Producer::start, &market2);
tFarm1.join();
tFarm2.join();
tMarket1.join();
tMarket2.join();
for(int i = 0; i < 4; i++)
sensorThreads[i]->join();
serverThread.join();
httpServerThread.join();
for(thread* thread : storeServerThreads) {
thread->join();
}
delete server;
for(Sensor* sensor : sensors)
delete sensor;
return 0;
}