00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00016 #include <stdio.h>
00017 #include <string>
00018 #include "pubsub.hpp"
00019
00020 using namespace std;
00021
00023 struct Named {
00024 string name;
00025
00026 Named(const string& n) : name(n)
00027 {
00028 printf("constructed: %s\n", name.c_str());
00029 }
00030
00031 ~Named() { printf("destructed: %s\n", name.c_str()); }
00032 };
00033
00035 struct Pub : Named, Publisher<string> {
00036 Pub(const string& name) : Named(name) {}
00037 };
00038
00040 struct Sub : Named, Subscriber<string> {
00041 Sub(const string& name) : Named(name) {}
00042
00043 virtual bool regularMsg(const Publisher<string>& pub, const string& msg)
00044 {
00045 printf("%s received %s from %s\n", name.c_str(), msg.c_str(),
00046 static_cast<const Pub&>(pub).name.c_str());
00047 return 1;
00048 }
00049
00050 virtual void subscribedMsg(const Publisher<string>& pub)
00051 {
00052 printf("%s received subscribedMsg from %s\n", name.c_str(),
00053 static_cast<const Pub&>(pub).name.c_str());
00054 }
00055
00056 virtual void unsubscribedMsg(const Publisher<string>& pub)
00057 {
00058 printf("%s received unsubscribedMsg from %s\n", name.c_str(),
00059 static_cast<const Pub&>(pub).name.c_str());
00060 }
00061 };
00062
00063 int main()
00064 {
00065 Pub p1("p1");
00066 {
00067 Sub s1("s1");
00068 {
00069 Pub p2("p2");
00070 {
00071 Sub s2("s2");
00072
00073 p1.subscribe(s1);
00074 p1.subscribe(s2);
00075 p2.subscribe(s1);
00076 p2.subscribe(s2);
00077
00078 p1.send("message1");
00079 p2.send("message1");
00080 }
00081
00082 p1.send("message2");
00083 p2.send("message2");
00084 }
00085
00086 p1.send("message3");
00087 }
00088
00089 p1.send("message4");
00090 }