-
Notifications
You must be signed in to change notification settings - Fork 0
/
plazza.cpp
103 lines (96 loc) · 2.15 KB
/
plazza.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
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
#include "plazza.h"
#include <stdio.h>
#include <errno.h>
#include <cstring> // strerror
Plazza::Plazza(int _multiplier, int _cooks, int _time)
{
this->multiplier = _multiplier;
this->cooks = _cooks;
this->time = _time;
this->num = 0;
this->pid = getpid();
}
void Plazza::assign_to_kitchen(std::string &pizza)
{
std::list<Fifo>::iterator itin;
std::list<Fifo>::iterator itout;
std::string res;
int _pid;
itin = this->fifosin.begin();
itout = this->fifosout.begin();
while (itin != this->fifosin.end())
{
*itout << pizza;
*itin >> res;
if (res.compare(0, 4, "succ") == 0)
break ;
itin++;
itout++;
}
this->num = this->num + 1;
if (getpid() == this->pid)
{
Fifo fifoin(this->num, 0);
Fifo fifoout(this->num, 1);
this->fifosin.push_back(fifoin);
this->fifosout.push_back(fifoout);
if ((_pid = fork()) < 0)
throw cerrExcept("fork fail");
if(_pid == 0)
{
Kitchen kitchen(this->multiplier, this->cooks, this->time);
kitchen.work(fifoout, fifoin);
}
else
{
fifoout << pizza;
fifoin >> res;
std::cout << res << " res" << std::endl;
}
}
}
void Plazza::reception()
{
std::string msg;
Parse parse;
if (this->pid = getpid())
{
while (1)
{
std::cout << " > Enter the commands :" << std::endl;
getline(std::cin, msg);
if (msg.compare(0, 4, "quit") == 0)
return ;
if (msg.compare(0, 4, "exit") == 0)
return ;
parse.getinfo(msg);
while (parse.get_count())
{
assign_to_kitchen(parse.get_order_one_pizza());
parse.order_pop();
}
}
}
}
Plazza::~Plazza()
{
std::string error;
std::ostringstream fifo;
std::string dir("./fifos/");
for (int i = 0; i != this->num; ++i)
{
fifo << dir << (i + 1) << "a";
if (remove(fifo.str().c_str()) != 0)
{
error.append("cleanfifo: ").append(strerror(errno));
throw cerrExcept(error);
}
fifo.str(""); // clean fifo stream
fifo << dir << (i + 1) << "b";
if (remove(fifo.str().c_str()) != 0)
{
error.append("cleanfifo: ").append(strerror(errno));
throw cerrExcept(error);
}
}
}