GCS  0.2.3
gu_prodcons.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Codership Oy <info@codership.com>
3  *
4  * $Id:$
5  */
6 
11 #include "gu_lock.hpp"
12 // For byte_t
13 #include "gu_buffer.hpp"
14 
15 /* Forward declarations */
16 namespace gu
17 {
18  namespace prodcons
19  {
20  class MessageData;
21  class Message;
22  class MessageQueue;
23  class Producer;
24  class Consumer;
25  }
26 }
27 
29 {
30 public:
31  virtual ~MessageData() { }
32 };
33 
38 {
39  Producer* producer;
40  int val;
41  const MessageData* data;
42 
43 
44 public:
52  Message(Producer* prod_ = 0,
53  const MessageData* data_ = 0,
54  int val_ = -1) :
55  producer(prod_),
56  val(val_),
57  data(data_)
58  { }
59 
60  Message(const Message& msg) :
61  producer(msg.producer),
62  val(msg.val),
63  data(msg.data)
64  { }
65 
66  Message& operator=(const Message& msg)
67  {
68  producer = msg.producer;
69  val = msg.val;
70  data = msg.data;
71  return *this;
72  }
73 
79  Producer& get_producer() const { return *producer; }
80 
86  const MessageData* get_data() const { return data; }
87 
93  int get_val() const { return val; }
94 };
95 
100 {
101  gu::Cond cond;
102  Consumer& cons;
109  Cond& get_cond() { return cond; }
110  friend class Consumer;
111 public:
117  Producer(Consumer& cons_) :
118  cond(),
119  cons(cons_)
120  { }
121 
128  void send(const Message& msg, Message* ack);
129 };
130 
135 {
136  Mutex mutex;
137  MessageQueue* mque;
138  MessageQueue* rque;
140  Consumer(const Consumer&);
141  void operator=(const Consumer&);
142 protected:
151  const Message* get_next_msg();
152 
161  void return_ack(const Message& msg);
162 
166  virtual void notify() = 0;
167 public:
171  Consumer();
172 
176  virtual ~Consumer();
177 
184  void queue_and_wait(const Message& msg, Message* ack);
185 };
virtual void notify()=0
Virtual method to notify consumer about queued message.
void send(const Message &msg, Message *ack)
Send message to the consumer and wait for response.
int get_val() const
Get int value associated to the message.
Definition: gu_prodcons.hpp:93
const Message * get_next_msg()
Get the first message from the message queue.
Definition: gu_mutex.hpp:19
Message class for Producer/Consumer communication.
Definition: gu_prodcons.hpp:37
Definition: gu_cond.hpp:19
Consumer()
Default constructor.
Consumer interface.
Definition: gu_prodcons.hpp:134
Producer(Consumer &cons_)
Consturctor.
Definition: gu_prodcons.hpp:117
Producer & get_producer() const
Get producer associated to the message.
Definition: gu_prodcons.hpp:79
virtual ~Consumer()
Default destructor.
const MessageData * get_data() const
Get data associated to the message.
Definition: gu_prodcons.hpp:86
Producer interface.
Definition: gu_prodcons.hpp:99
Message(Producer *prod_=0, const MessageData *data_=0, int val_=-1)
Constructor.
Definition: gu_prodcons.hpp:52
Definition: gu_prodcons.hpp:28
void queue_and_wait(const Message &msg, Message *ack)
Queue message and wait for ack.
void return_ack(const Message &msg)
Return ack message for the producer.