Allegato "CasellaDiPosta.cpp"
Scarica 1 //SOLUZIONE PROPOSTA DA FRANCESCA
2
3 #include <iostream>
4 #include <JTC/JTC.h>
5
6
7 #define NM 3 //numero mail server
8 #define UT 5 //numero utenti
9 #define LEN 10 //dimensione casella di posta (buffer)
10
11 template <class T>
12 class Buffer : public JTCMonitor
13
14 {
15 T thebuffer[LEN];
16 int in;
17 int out;
18 int chars;
19
20 public:
21 Buffer(T initValue)
22 {
23 in = 0;
24 out = 0;
25 chars = 0;
26 for(int i = 0; i < LEN; i++)
27 thebuffer[i] = initValue;
28 }
29
30 T get()
31 {
32 JTCSynchronized sync(*this);
33 while (chars == 0) {
34 wait();
35 }
36 chars--;
37 T c = thebuffer[out];
38 out = ( out + 1 ) % LEN;
39 //cout << "Chars=" << chars << " Out=" << out << endl;
40 //printBuffer();
41 notify();
42 return c;
43 }
44
45 void put(T c)
46 {
47 JTCSynchronized sync(*this);
48 while (chars == LEN) {
49 wait();
50 }
51 chars++;
52 thebuffer[in] = c;
53 in = (in + 1) % LEN;
54 //cout << "Chars=" << chars << " In=" << in << endl;
55 //printBuffer();
56 notify();
57 }
58 };
59
60 class Messaggio
61 {
62 char* testoMsg;
63 int IDMittente;
64 int IDDestinatario;
65
66 public:
67 Messaggio()
68 {
69 testoMsg= "";
70 IDMittente= -1;
71 IDDestinatario=-1;
72
73 }
74
75 Messaggio(char* testo, int mittente, int destinatario)
76 {
77 testoMsg = testo;
78 IDMittente = mittente;
79 IDDestinatario = destinatario;
80 }
81
82 int getDestinatario()
83 {
84 return IDDestinatario;
85 }
86
87 int getMittente()
88 {
89 return IDMittente;
90 }
91 };
92
93 class CentroMsg : public JTCMonitor
94 {
95 Buffer<Messaggio> * casellaDiPosta;//di dimensione LEN
96 Buffer<Messaggio> * mailBox[NM];
97
98 public:
99
100 CentroMsg ()
101 {
102 for( int i=0; i<NM; i++)
103 mailBox[i] = new Buffer<Messaggio>(Messaggio());
104
105 casellaDiPosta = new Buffer<Messaggio>(Messaggio());
106
107 }
108
109
110 void putMsgCP(Messaggio m)
111 {
112 casellaDiPosta->put(m);
113 }
114
115 Messaggio getMsgCP()
116 {
117 return casellaDiPosta->get();
118 }
119
120 void putMsgMB (Messaggio m, int serverID)
121 {
122 mailBox[serverID]->put(m);
123 }
124
125 Messaggio getMsgMB (int serverID)
126 {
127 return mailBox[serverID]->get();
128 }
129
130 };
131
132 CentroMsg* centro = new CentroMsg();
133
134 class Utente : public JTCThread
135 {
136 int id;
137
138 public:
139 Utente(int i)
140 {
141 id=i;
142 }
143
144 virtual void run ()
145 {
146 while(true)
147 {
148 sleep( rand()% 3000);
149
150 int destinatario=rand()%NM;
151
152 printf("l'utente %i inserisce un messaggio per il server %i nel centro messaggi. \n ",id,destinatario);
153
154 Messaggio m("sms",id, destinatario);
155
156 centro->putMsgCP(m);
157
158 printf("l'utente %i va via. \n ",id);
159
160 }
161 }
162
163 };
164
165
166 class Smistatore : public JTCThread
167 {
168 int id;
169
170 public:
171
172 Smistatore(int i)
173 {
174 id = i;
175 }
176
177 virtual void run ()
178 {
179
180 while(true){
181
182 sleep(rand() % 1000);
183
184 printf("lo smistatore è in attesa di prelevare un messaggio dalla casella di posta.\n ");
185
186 Messaggio m = centro->getMsgCP();
187
188 sleep(rand() % 500);//tempo di elaborazione
189
190 int dest= m.getDestinatario();
191
192 int mitt= m.getMittente();
193
194 printf("lo smistatore inserisce il messaggio dell'utente %i nella mail box %i.\n ",mitt,dest);
195
196 centro->putMsgMB(m,dest);
197
198 }
199
200 }
201
202 };
203
204 class MailServer : public JTCThread
205 {
206
207 int id;
208
209 public:
210
211 MailServer(int i)
212 {
213 id=i;
214 }
215
216 virtual void run()
217 {
218 while(true)
219 {
220 sleep(rand() % 1000);
221
222 printf("il mail server %i è in attesa di prelevare un messaggio dalla sua mail box.\n ",id);
223
224 Messaggio messaggio=centro->getMsgMB(id);
225
226 int mittente=messaggio.getMittente();
227
228 sleep(rand() % 300);//tempo elaborazione messaggio
229
230 printf("il mail server %i preleva ed elabora il messaggio dell'utente %i.\n ",id,mittente);
231
232
233
234
235 }
236 }
237 };
238
239 int main(int argc, char* argv[])
240
241 {
242 JTCInitialize initialize;
243
244 Utente* u[UT];
245 Smistatore* s = new Smistatore(0);
246 MailServer* ms[NM];
247
248 s->start();
249
250 for (int i = 0; i < UT; i++) {
251 u[i] = new Utente(i);
252 u[i]->start();
253 }
254 for (int i = 0; i < NM; i++) {
255 ms[i] = new MailServer(i);
256 ms[i]->start();
257 }
258
259
260
261 return 0;
262 }
Allegati
Non è consentito inserire allegati su questa pagina.