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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
// $Id: buffer_stream.cpp 91671 2010-09-08 18:39:23Z johnnyw $
// This short program copies stdin to stdout via the use of an ASX
// Stream. It illustrates an implementation of the classic "bounded
// buffer" program using an ASX Stream containing two Modules. Each
// ACE_Module contains two Tasks. Each ACE_Task contains a
// ACE_Message_Queue and a pointer to a ACE_Thread_Manager. Note how
// the use of these reusable components reduces the reliance on global
// variables, as compared with the bounded_buffer.C example.
#include "ace/OS_main.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_time.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Service_Config.h"
#include "ace/Stream.h"
#include "ace/Module.h"
#include "ace/Task.h"
#include "ace/Truncate.h"
#if defined (ACE_HAS_THREADS)
typedef ACE_Stream<ACE_MT_SYNCH> MT_Stream;
typedef ACE_Module<ACE_MT_SYNCH> MT_Module;
typedef ACE_Task<ACE_MT_SYNCH> MT_Task;
class Common_Task : public MT_Task
// = TITLE
// Methods that are common to the producer and consumer.
{
public:
Common_Task (void) {}
//FUZZ: disable check_for_lack_ACE_OS
// ACE_Task hooks
virtual int open (void * = 0);
virtual int close (u_long = 0);
//FUZZ: enable check_for_lack_ACE_OS
};
// Define the Producer interface.
class Producer : public Common_Task
{
public:
Producer (void) {}
// Read data from stdin and pass to consumer.
virtual int svc (void);
};
class Consumer : public Common_Task
// = TITLE
// Define the Consumer interface.
{
public:
Consumer (void) {}
virtual int put (ACE_Message_Block *mb,
ACE_Time_Value *tv = 0);
// Enqueue the message on the ACE_Message_Queue for subsequent
// handling in the svc() method.
virtual int svc (void);
// Receive message from producer and print to stdout.
private:
ACE_Time_Value timeout_;
};
class Filter : public MT_Task
// = TITLE
// Defines a Filter that prepends a line number in front of each
// line.
{
public:
Filter (void): count_ (1) {}
virtual int put (ACE_Message_Block *mb,
ACE_Time_Value *tv = 0);
// Change the size of the message before passing it downstream.
private:
size_t count_;
// Count the number of lines passing through the filter.
};
// Spawn off a new thread.
int
Common_Task::open (void *)
{
if (this->activate (THR_NEW_LWP | THR_DETACHED) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("spawn")),
-1);
return 0;
}
int
Common_Task::close (u_long exit_status)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("(%t) thread is exiting with status %d in module %s\n"),
exit_status,
this->name ()));
// Can do anything here that is required when a thread exits, e.g.,
// storing thread-specific information in some other storage
// location, etc.
return 0;
}
// The Consumer reads data from the stdin stream, creates a message,
// and then queues the message in the message list, where it is
// removed by the consumer thread. A 0-sized message is enqueued when
// there is no more data to read. The consumer uses this as a flag to
// know when to exit.
int
Producer::svc (void)
{
// Keep reading stdin, until we reach EOF.
for (int n; ; )
{
// Allocate a new message (add one to avoid nasty boundary
// conditions).
ACE_Message_Block *mb = 0;
ACE_NEW_RETURN (mb,
ACE_Message_Block (BUFSIZ + 1),
-1);
n = ACE_OS::read (ACE_STDIN, mb->wr_ptr (), BUFSIZ);
if (n <= 0)
{
// Send a shutdown message to the other thread and exit.
mb->length (0);
if (this->put_next (mb) == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("put_next")));
break;
}
// Send the message to the other thread.
else
{
mb->wr_ptr (n);
// NUL-terminate the string (since we use strlen() on it
// later).
mb->rd_ptr ()[n] = '\0';
if (this->put_next (mb) == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n"),
ACE_TEXT ("put_next")));
}
}
return 0;
}
// Simply enqueue the Message_Block into the end of the queue.
int
Consumer::put (ACE_Message_Block *mb, ACE_Time_Value *tv)
{
return this->putq (mb, tv);
}
// The consumer dequeues a message from the ACE_Message_Queue, writes
// the message to the stderr stream, and deletes the message. The
// Consumer sends a 0-sized message to inform the consumer to stop
// reading and exit.
int
Consumer::svc (void)
{
int result = 0;
// Keep looping, reading a message out of the queue, until we
// timeout or get a message with a length == 0, which signals us to
// quit.
for (;;)
{
ACE_Message_Block *mb = 0;
// Wait for upto 4 seconds.
this->timeout_.sec (ACE_OS::time (0) + 4);
result = this->getq (mb, &this->timeout_);
if (result == -1)
break;
int length = ACE_Utils::truncate_cast<int> (mb->length ());
if (length > 0)
ACE_OS::write (ACE_STDOUT,
mb->rd_ptr (),
ACE_OS::strlen (mb->rd_ptr ()));
mb->release ();
if (length == 0)
break;
}
if (result == -1 && errno == EWOULDBLOCK)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%t) %p\n%a"),
ACE_TEXT ("timed out waiting for message"),
1));
return 0;
}
int
Filter::put (ACE_Message_Block *mb,
ACE_Time_Value *tv)
{
if (mb->length () == 0)
return this->put_next (mb, tv);
else
{
char buf[BUFSIZ];
// Stash a copy of the buffer away.
ACE_OS::strncpy (buf, mb->rd_ptr (), sizeof buf);
// Increase the size of the buffer large enough that it will be
// reallocated (in order to test the reallocation mechanisms).
mb->size (mb->length () + BUFSIZ);
mb->length (mb->size ());
// Prepend the line count in front of the buffer.
ACE_OS::sprintf (mb->rd_ptr (),
ACE_SIZE_T_FORMAT_SPECIFIER
": %s",
this->count_++,
buf);
return this->put_next (mb, tv);
}
}
// Main driver function.
int
ACE_TMAIN (int, ACE_TCHAR *argv[])
{
ACE_Service_Config daemon (argv[0]);
// This Stream controls hierachically-related active objects.
MT_Stream stream;
MT_Module *pm = 0;
MT_Module *fm = 0;
MT_Module *cm = 0;
ACE_NEW_RETURN (cm,
MT_Module (ACE_TEXT ("Consumer"),
new Consumer),
-1);
ACE_NEW_RETURN (fm,
MT_Module (ACE_TEXT ("Filter"),
new Filter),
-1);
ACE_NEW_RETURN (pm,
MT_Module (ACE_TEXT ("Producer"),
new Producer),
-1);
// Create Consumer, Filter, and Producer Modules and push them onto
// the Stream. All processing is performed in the Stream.
if (stream.push (cm) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("push")),
1);
else if (stream.push (fm) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("push")),
1);
else if (stream.push (pm) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("push")),
1);
// Barrier synchronization: wait for the threads to exit, then exit
// ourselves.
ACE_Thread_Manager::instance ()->wait ();
return 0;
}
#else
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("threads not supported on this platform\n")));
return 0;
}
#endif /* ACE_HAS_THREADS */
|