Allegato "solNov2010.cpp"
Scarica 1 /*
2 * main.cpp
3 *
4 * Created on: Jun 16, 2011
5 * Author: ianni
6 */
7 #include <JTC/JTC.h>
8 #include <iostream>
9 #include <string>
10 #include <stdlib.h>
11 #include <time.h>
12
13 using namespace std;
14
15 #define LEN 40
16
17 template <class T>
18 class Buffer : public JTCMonitor
19
20 {
21 T thebuffer[LEN];
22 int in;
23 int out;
24 int elements;
25
26 public:
27 Buffer(T initValue)
28 {
29 in = 0;
30 out = 0;
31 elements = 0;
32 for(int i = 0; i < LEN; i++)
33 thebuffer[i] = initValue;
34 }
35
36 T get()
37 {
38 JTCSynchronized sync(*this);
39 while (elements == 0) {
40 wait();
41 }
42 elements--;
43 T c = thebuffer[out];
44 out = ( out + 1 ) % LEN;
45 notify();
46 return c;
47 }
48
49 void put(T c)
50 {
51 JTCSynchronized sync(*this);
52 while (elements == LEN) {
53 wait();
54 }
55 elements++;
56 thebuffer[in] = c;
57 in = (in + 1) % LEN;
58 notify();
59 }
60 //
61 // Fails instead of blocking if buffer full
62 //
63 bool nonBlockingPut(T c)
64 {
65 JTCSynchronized sync(*this);
66 if (elements == LEN) {
67 return false;
68 }
69 elements++;
70 thebuffer[in] = c;
71 in = (in + 1) % LEN;
72 notify();
73 return true;
74 }
75
76 };
77
78 class SBox;
79
80 class SProcessor : public JTCThread
81 {
82 SBox *S;
83 public:
84 SProcessor (SBox* s);
85 virtual void run();
86 };
87
88 class RandomFrameGenerator : public JTCThread
89
90 {
91 Buffer<string*>* line;
92 char c;
93 public:
94 RandomFrameGenerator(Buffer<string*>* l, char c);
95 virtual void run();
96 };
97
98
99 class SBox : public JTCMonitor
100 {
101 int MEMconnection; // 1 = IN1, 2 = IN2
102 int OUTconnection; // 0 = MEM, 1 = IN1, 2 = IN2
103
104 public:
105 Buffer<string*> IN1;
106 Buffer<string*> IN2;
107 Buffer<string*> MEM;
108
109 SBox() : IN1(0) , IN2(0) , MEM(0) {};
110 void start()
111 {
112 MEMconnection = 1;
113 OUTconnection = 1;
114 SProcessor* s = new SProcessor(this);
115 RandomFrameGenerator* r1 = new RandomFrameGenerator(&IN1,'@');
116 RandomFrameGenerator* r2 = new RandomFrameGenerator(&IN2,'+');
117 s->start();
118 r1->start();
119 r2->start();
120 }
121 void setOut(int c)
122 {
123 JTCSynchronized sync(*this);
124 OUTconnection = c;
125 }
126 void setMem(int c)
127 {
128 JTCSynchronized sync(*this);
129 MEMconnection = c;
130 }
131 int getOut()
132 {
133 JTCSynchronized sync(*this);
134 return OUTconnection;
135 }
136 int getMem()
137 {
138 JTCSynchronized sync(*this);
139 return MEMconnection;
140 }
141
142 };
143
144 SProcessor::SProcessor (SBox* s)
145 {
146 S = s;
147 }
148 void SProcessor::run()
149 {
150 while(true)
151 {
152 sleep(1000/50); // Sleeps 1/50-th of second;
153 string* linea1 = S->IN1.get();
154 string* linea2 = S->IN2.get();
155 int mem = S->getMem();
156 switch(mem)
157 {
158 //
159 // Evita di bloccare lo SProcessor se MEM si riempie.
160 //
161 case 1:
162 if (!S->MEM.nonBlockingPut(linea1))
163 cout << "MEM is full" << endl;
164
165 break;
166 case 2:
167 if (!S->MEM.nonBlockingPut(linea2))
168 cout << "MEM is full" << endl;
169 break;
170 }
171 int out = S->getOut();
172 switch (out)
173 {
174 case 0:
175 cout << *(S->MEM.get());
176 break;
177 case 1:
178 cout << *linea1;
179 break;
180 case 2:
181 cout << *linea2;
182 break;
183 }
184 }
185 }
186
187
188
189 RandomFrameGenerator::RandomFrameGenerator(Buffer<string*>* l, char c)
190 {
191 line = l;
192 this->c = c;
193 }
194 void RandomFrameGenerator::run()
195 {
196 srand(time(0)*c);
197 string theLine;
198 while(true)
199 {
200 //cout << "Cycle " << c << endl;
201 for (int i=0; i < 80; i++)
202 if (rand() % 2 >= 1)
203 theLine += c;
204 else theLine += '-';
205 theLine += '\n';
206 int framerate = 40 + rand() % 21; // random frame rate from 1/40-th to 1/60-th sec.
207 sleep(1000/framerate);
208 line->put(&theLine);
209 }
210 }
211
212
213 int main()
214
215 {
216 JTCInitialize init;
217 SBox* s = new SBox();
218 s->start();
219 JTCThread::sleep(5000);
220 s->setOut(2);
221 JTCThread::sleep(5000);
222 s->setOut(1);
223 JTCThread::sleep(5000);
224 s->setOut(0);
225 cout << "FINE" << endl;
226 }
Allegati
Non รจ consentito inserire allegati su questa pagina.