|
PreCache NetInjector Documentation Center : PreCache NetInjector Application Development Common Cases, Subscription C++ |
|
|
|
Common Cases, subscription code C++ the complete subscription code The previous code samples presented a simple publish/subscribe application. In this tutorial, we have learned how to enhance subscriptions by
#include "PC_evn_Filter.hpp" #include "PC_evn_Proxy.hpp" #include "PC_evn_Notification.hpp" #include "PC_evn_Subscriber.hpp" #include <iostream> using namespace precache::event; using std::cerr; using std::cout; using std::endl; //Define a subscriber. class Subscriber1:public Subscriber { public: virtual void Notify( throw (precache::util::PCException) { PC_STRING message; PC_STRING username; PC_CHAR gender; PC_LONG age;
//Retrieve the values of predefined attributes. pNotification->GetPredefinedAttr( pNotification->GetPredefinedAttr( pNotification->GetPredefinedAttr("Age",age); pNotification->GetPredefinedAttr("Gender",gender); //Build the notification. cout<<"Incoming notification for ( cout<<"From:"<<endl; cout<<"Name = "<< username <<endl; cout<<"Age = "<< (int)age <<endl; if ((gender == 'M')||(gender=='m')) cout<<"Male"<<endl; else if ((gender == 'F')||(gender=='f')) cout<<"Female"<<endl; cout<<endl; } }; //Define a subscriber which can retrieve a class Subscriber2:public Subscriber { public: virtual void Notify( void* pClosure) { PC_STRING message; PC_STRING username; PC_CHAR gender; PC_LONG age; PC_STRING interest; PC_TypeCode typecode; PC_UINT size; //Retrieve the value of the predefined attributes. pNotification->GetPredefinedAttr( pNotification->GetPredefinedAttr( pNotification->GetPredefinedAttr("Age",age); pNotification->GetPredefinedAttr("Gender",gender); pNotification-> //Retrieve the value of the discretionary attribute. GetDiscretionaryAttr( cout<<"Incoming notification for (
//Build the notification. cout<<"From:"<<endl; cout<<"Name = "<<username<<endl; cout<<"Age = "<<(int)age<<endl; if ((gender == 'M')||(gender=='m')) cout<<"Male"<<endl; else if ((gender == 'F')||(gender=='f')) cout<<"Female"<<endl; cout<<"Interest = "<<interest<<endl; cout<<endl; } }; int main(int argc, char* argv[]) { try { //Create a proxy for the channel and client. Proxy p(40,3002); //Submit the subscription. p.Subscribe( p.Subscribe( p.Subscribe( cout<<"Subscriptions were submitted... sleep(30); } { cerr<<e.Message().String()<<endl; } } |
|