4 * Queue - a tester of the PQueueChannel class in pwlib
6 * Copyright (c) 2006 Derek J Smithies
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
15 * the License for the specific language governing rights and limitations
18 * The Original Code is Jester
20 * The Initial Developer of the Original Code is Derek J Smithies
22 * Contributor(s): ______________________________________.
25 * Revision 1.1 2006/12/07 08:46:17 dereksmithies
26 * Initial cut of code to test the operation of PQueueChannel class.
40 PCREATE_PROCESS(QueueProcess
);
42 ///////////////////////////////////////////////////////////////
44 /////////////////////////////////////////////////////////////////////////////
46 QueueProcess::QueueProcess()
47 : PProcess("Derek Smithies Code Factory", "Queue",
53 void QueueProcess::Main()
55 // Get and parse all of the command line arguments.
56 PArgList
& args
= GetArguments();
72 if (args
.HasOption('h') ) {
73 cout
<< "Usage : " << GetName() << " [options] \n"
76 " -d --durationread : interval, in ms between reads (30ms)\n"
77 " -D --durationwrite : interval, in ms between writes (20ms)\n"
78 " -s --sizeread : number of bytes read at each read iteration (480)\n"
79 " -S --sizewrite : number of bytes written at each write iteration (320)\n"
80 " -i --iterations # : number of iteration of the write loop (100) \n"
82 " -t --trace : Enable trace, use multiple times for more detail.\n"
83 " -o --output : File for trace output, default is stderr.\n"
86 " -h --help : This help message.\n"
87 " -v --version : report version and program info.\n"
93 if (args
.HasOption('v')) {
94 cout
<< GetName() << endl
95 << " Version " << GetVersion(TRUE
) << endl
96 << " by " << GetManufacturer() << endl
97 << " on " << GetOSClass() << ' ' << GetOSName() << endl
98 << " (" << GetOSVersion() << '-' << GetOSHardware() << ")\n\n";
103 PTrace::Initialise(args
.GetOptionCount('t'),
104 args
.HasOption('o') ? (const char *)args
.GetOptionString('o') : NULL
,
105 PTrace::Timestamp
|PTrace::Thread
|PTrace::FileAndLine
);
114 if (args
.HasOption('i'))
115 iterations
= args
.GetOptionString('i').AsInteger();
116 if (args
.HasOption('d'))
117 writeDelay
= args
.GetOptionString('d').AsInteger();
118 if (args
.HasOption('D'))
119 readDelay
= args
.GetOptionString('D').AsInteger();
120 if (args
.HasOption('s'))
121 writeSize
= args
.GetOptionString('s').AsInteger();
122 if (args
.HasOption('S'))
123 readSize
= args
.GetOptionString('S').AsInteger();
125 PStringStream values
;
126 values
<< " processs " << iterations
<< " iterations of the write loop" << endl
;
127 values
<< " Read " << readSize
<< " bytes per loop, delay " << readDelay
<< " ms" << endl
;
128 values
<< " Write " << writeSize
<< " bytes per loop, delay " << writeDelay
<< " ms" << endl
;
134 PThread
* writer
= PThread::Create(PCREATE_NOTIFIER(GenerateBlockData
), 0,
135 PThread::NoAutoDeleteThread
,
136 PThread::NormalPriority
,
139 PThread
* reader
= PThread::Create(PCREATE_NOTIFIER(ConsumeBlockData
), 0,
140 PThread::NoAutoDeleteThread
,
141 PThread::NormalPriority
,
146 writer
->WaitForTermination();
148 reader
->WaitForTermination();
156 void QueueProcess::GenerateBlockData(PThread
&, INT
)
158 PAdaptiveDelay delay
;
159 BYTE buffer
[writeSize
];
162 for (PINDEX i
= 0; i
< iterations
; i
++) {
164 delay
.Delay(writeDelay
);
165 queue
.Write(buffer
, writeSize
);
166 PTRACE(3, "Write " << writeSize
<< " bytes to the queue ("
167 << queue
.GetLength() << " bytes)"
168 << " " << (count
* writeSize
));
170 PTRACE(3, "End of generate data blocks ");
175 void QueueProcess::ConsumeBlockData(PThread
&, INT
)
177 PAdaptiveDelay delay
;
178 BYTE buffer
[readSize
];
180 PINDEX readCount
= 0;
181 while (queue
.GetLength() < (readSize
* 2)) {
182 PTRACE(3, "Wait for queue to grow, is only " << queue
.GetLength() << " bytes).");
187 while (queue
.IsOpen()) {
189 delay
.Delay(readDelay
);
190 if (!queue
.Read(buffer
, readSize
)) {
191 PTRACE(2, "Failed in read from the queue. Read only "
192 << queue
.GetLastReadCount() << " bytes.");
194 readCount
+= queue
.GetLastReadCount();
195 PTRACE(3, "Read " << queue
.GetLastReadCount() << " bytes from the queue ("
196 << queue
.GetLength() << " bytes)"
197 << " " << (count
* readSize
)
198 << " (" << readCount
<< ")");
202 PTRACE(3, "End of read data blocks from the queue");
206 // End of File ///////////////////////////////////////////////////////////////