-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestActiveObjects.cs
69 lines (59 loc) · 2.2 KB
/
TestActiveObjects.cs
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
using System;
using ConcurrencyUtilities;
using Colorizer = AnsiColor.AnsiColor;
namespace TestConcurrencyUtilities
{
public static class TestActiveObjects
{
public static void Run() {
Channel<string> words = new Channel<string>();
Channel<string> wordsReversed = new Channel<string>();
TestActiveObjectWordGenerator wordGenerator = new TestActiveObjectWordGenerator(words);
TestActiveObjectWordReverser wordReverser = new TestActiveObjectWordReverser(words, wordsReversed);
TestActiveObjectWordOutputter wordOutputter = new TestActiveObjectWordOutputter(wordsReversed);
wordOutputter.Start();
wordReverser.Start();
wordGenerator.Start();
}
}
class TestActiveObjectWordGenerator: ActiveObjectOutput<string>
{
string[] _words;
int _numWordsOutputted;
public TestActiveObjectWordGenerator(Channel<string> output): base(output) {
string phrase = "Hello world! I am a word reversing and printing program.";
_words = phrase.Split(' ');
_numWordsOutputted = 0;
}
protected override string Process() {
string word = "";
if (_numWordsOutputted < _words.Length) {
word = _words[_numWordsOutputted];
_numWordsOutputted++;
} else {
Console.WriteLine(Colorizer.Colorize("{black}TestActiveObjectWordGenerator finished sending words " +
"to its output channel; Stopping ActiveObject"));
// Stop(); // Doesn't seem to have much effect currently... (it just continues to loop for a while)
(new Semaphore(0)).Acquire(); // Force pause
}
TestSupport.SleepThread(100, false); // No '...'
return word;
}
}
class TestActiveObjectWordReverser: ActiveObjectInputOutput<string,string>
{
public TestActiveObjectWordReverser(Channel<string> input, Channel<string> output): base(input, output) {}
protected override string Process(string item) {
char[] charArray = item.ToString().ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
class TestActiveObjectWordOutputter: ActiveObjectInput<string>
{
public TestActiveObjectWordOutputter(Channel<string> input): base(input) {}
protected override void Process(string item) {
Console.WriteLine(Colorizer.Colorize("{green}" + item));
}
}
}