SoDaRadio-5.0.3-master:8901fb5
MultiMBox_Test.cxx
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012, Matthew H. Reilly (kb1vc)
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #include "MultiMBox.hxx"
30 #include "SoDaBase.hxx"
31 #include <iostream>
32 #include <string>
33 #include <boost/thread/mutex.hpp>
34 #include <boost/thread/condition.hpp>
35 
36 boost::mutex iomutex;
37 namespace SoDaTest {
38 
39  class MyMsg : public SoDa::MBoxMessage {
40  public:
41  enum CMD { START, BCAST, KILL, STOP };
42  MyMsg(const std::string ss, CMD _c) {
43  s = ss;
44  cmd = _c;
45  {
46  boost::mutex::scoped_lock lock(iomutex);
47  std::cerr << "Creating message [" << s << "," << cmd << "]" << std::endl;
48  }
49  }
50 
51  MyMsg() {
52  s = "Dunno";
53  }
54 
55  ~MyMsg() {
56  boost::mutex::scoped_lock lock(iomutex);
57  std::cerr << "Destroying message [" << s << "," << cmd << "]" << std::endl;
58  }
59  std::string s;
60  CMD cmd;
61  };
62 
64  public:
65  MultiMBox_Test_Thread(const std::string & n) : SoDaThread(n) {
66  name = n;
67  sub_count = 0;
68  }
69 
71  if(sub_count < 10) {
72  subscriptions[sub_count] = mbox->subscribe();
73  mboxes[sub_count] = mbox;
74  sub_count++;
75  }
76  }
77 
78  void execute(MyMsg * msg, int i) {
79  {
80  boost::mutex::scoped_lock lock(iomutex);
81  std::cerr << "Process " << name << "got message " << msg->cmd << " " << msg->s << " subcount = " << sub_count << " i = " << i << std::endl;
82  }
83  switch (msg->cmd) {
84  case MyMsg::START:
85  if((i + 1) < sub_count) {
86  mboxes[i+1]->put(new MyMsg("from" + name, MyMsg::KILL));
87  name = name + ".";
88  }
89  break;
90  case MyMsg::BCAST:
91  break;
92  case MyMsg::KILL:
93  if((i + 1) < sub_count) {
94  mboxes[i+1]->put(new MyMsg("from" + name, MyMsg::KILL));
95  name = name + ".";
96  }
97  break;
98  break;
99  case MyMsg::STOP:
100  break;
101  default:
102  break;
103  }
104  }
105 
106  void run()
107  {
108  // iterate through our subscriptions and print out the
109  // contents of each one.
110  int i;
111  MyMsg * msg;
112  while(1) {
113  bool flag = false;
114  for(i = 0; i < sub_count; i++) {
115  // check to see if there is a message on the mailbox.
116  if((msg = (mboxes[i]->get(subscriptions[i])))) {
117  execute(msg, i);
118  mboxes[i]->free(msg);
119  flag = true;
120  }
121  }
122 
123  if(!flag) usleep(10000);
124  }
125  }
126 
127 
128  std::string name;
129 
130  int subscriptions[10];
133  };
134 }
135 
136 int main(int argc, char * argv[])
137 {
138  (void) argc; (void) argv;
139 
141 
142  int i;
143  for(i = 0; i < 5; i++) mbox[i] = new SoDa::MultiMBox<SoDaTest::MyMsg>(false);
144 
145  SoDaTest::MultiMBox_Test_Thread a(std::string("A"));
146  SoDaTest::MultiMBox_Test_Thread b(std::string("B"));
147  SoDaTest::MultiMBox_Test_Thread c(std::string("C"));
148  SoDaTest::MultiMBox_Test_Thread d(std::string("D"));
149  SoDaTest::MultiMBox_Test_Thread e(std::string("E"));
150 
152  v[0] = &a; v[1] = &b; v[2] = &c; v[3] = &d; v[4] = &e;
153 
154  int j;
155  for(i = 0; i < 5; i++) {
156  for(j = i; j < 5; j++) {
157  v[j]->subscribe(mbox[i]);
158  }
159  }
160 
161  for(i = 0; i < 5; i++) v[i]->start();
162 
163  mbox[0]->put(new SoDaTest::MyMsg("first_toall", SoDaTest::MyMsg::START));
164  while(1) {
165  usleep(10000);
166  }
167 }
168 
169 
The Baseclass for all SoDa objects, and useful commonly used classes.
The Thread baseclass for all SoDa thread objects.
Definition: SoDaBase.hxx:284
void execute(MyMsg *msg, int i)
int main(int argc, char *argv[])
MultiMBox_Test_Thread(const std::string &n)
MyMsg(const std::string ss, CMD _c)
void subscribe(SoDa::MultiMBox< MyMsg > *mbox)
boost::mutex iomutex
void run()
Each thread object must define its "run" loop.
void put(T *m)
Definition: MultiMBox.hxx:97