Fixed DevStudio 2003 build with memory check code.
[pwlib.git] / samples / queue / main.cxx
blobe7bd3bbfb5ddec9e84b394bcee7ab0f301c9bbd8
1 /*
2 * main.cxx
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
16 * under the License.
18 * The Original Code is Jester
20 * The Initial Developer of the Original Code is Derek J Smithies
22 * Contributor(s): ______________________________________.
24 * $Log$
25 * Revision 1.1 2006/12/07 08:46:17 dereksmithies
26 * Initial cut of code to test the operation of PQueueChannel class.
31 #include <ptlib.h>
33 #include "main.h"
34 #include "version.h"
37 #define new PNEW
40 PCREATE_PROCESS(QueueProcess);
42 ///////////////////////////////////////////////////////////////
44 /////////////////////////////////////////////////////////////////////////////
46 QueueProcess::QueueProcess()
47 : PProcess("Derek Smithies Code Factory", "Queue",
48 1, 1, ReleaseCode, 0)
53 void QueueProcess::Main()
55 // Get and parse all of the command line arguments.
56 PArgList & args = GetArguments();
57 args.Parse(
58 "h-help."
59 "d-durationread:"
60 "D-durationwrite:"
61 "s-sizeread:"
62 "S-sizewrite:"
63 "i-iterations:"
64 #if PTRACING
65 "o-output:"
66 "t-trace."
67 #endif
68 "v-version."
69 , FALSE);
72 if (args.HasOption('h') ) {
73 cout << "Usage : " << GetName() << " [options] \n"
75 "General 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"
81 #if PTRACING
82 " -t --trace : Enable trace, use multiple times for more detail.\n"
83 " -o --output : File for trace output, default is stderr.\n"
84 #endif
86 " -h --help : This help message.\n"
87 " -v --version : report version and program info.\n"
88 "\n"
89 "\n";
90 return;
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";
99 return;
102 #if PTRACING
103 PTrace::Initialise(args.GetOptionCount('t'),
104 args.HasOption('o') ? (const char *)args.GetOptionString('o') : NULL,
105 PTrace::Timestamp|PTrace::Thread|PTrace::FileAndLine);
106 #endif
108 iterations = 100;
109 writeDelay = 20;
110 readDelay = 30;
111 writeSize = 320;
112 readSize = 480;
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;
129 cout << values;
130 PTRACE(3, values);
132 queue.Open(10000);
134 PThread * writer = PThread::Create(PCREATE_NOTIFIER(GenerateBlockData), 0,
135 PThread::NoAutoDeleteThread,
136 PThread::NormalPriority,
137 "generate");
139 PThread * reader = PThread::Create(PCREATE_NOTIFIER(ConsumeBlockData), 0,
140 PThread::NoAutoDeleteThread,
141 PThread::NormalPriority,
142 "consume");
146 writer->WaitForTermination();
148 reader->WaitForTermination();
150 delete writer;
151 delete reader;
156 void QueueProcess::GenerateBlockData(PThread &, INT )
158 PAdaptiveDelay delay;
159 BYTE buffer[writeSize];
160 PINDEX count = 0;
162 for (PINDEX i = 0; i < iterations; i++) {
163 count++;
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 ");
171 queue.Close();
175 void QueueProcess::ConsumeBlockData(PThread &, INT)
177 PAdaptiveDelay delay;
178 BYTE buffer[readSize];
179 PINDEX count = 0;
180 PINDEX readCount = 0;
181 while (queue.GetLength() < (readSize * 2)) {
182 PTRACE(3, "Wait for queue to grow, is only " << queue.GetLength() << " bytes).");
183 PThread::Sleep(10);
185 delay.Restart();
187 while (queue.IsOpen()) {
188 count++;
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.");
193 } else {
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 ///////////////////////////////////////////////////////////////