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(
      Notification* pNotification,
      void* pClosure)

      throw (precache::util::PCException)

      {

            PC_STRING message;

      PC_STRING username;

      PC_CHAR gender;

      PC_LONG age;

//Retrieve the values of predefined attributes.

      pNotification->GetPredefinedAttr(
            "Message",message);      

      pNotification->GetPredefinedAttr(
            "UserName",username);

      pNotification->GetPredefinedAttr("Age",age);

      pNotification->GetPredefinedAttr("Gender",gender);

//Build the notification.

      cout<<"Incoming notification for (
            "<<(char*)pClosure<<"):"<<message<<endl;

      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
//discretionary attribute.

class Subscriber2:public Subscriber

{

public:

      virtual void Notify(
            Notification* pNotification,

            void* pClosure)
            throw (precache::util::PCException)

      {

            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(
            "Message",message);      

      pNotification->GetPredefinedAttr(
            "UserName",username);

      pNotification->GetPredefinedAttr("Age",age);

      pNotification->GetPredefinedAttr("Gender",gender);

      pNotification->

//Retrieve the value of the discretionary attribute.

      GetDiscretionaryAttr(
            "Interest",
            (void*&)interest,
            typecode,size);

      cout<<"Incoming notification for (
            "<<(char*)pClosure<<"):"<<message<<endl;

//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(
                  "Sample.Chat","UserName==\"Jane\"Sample.Chat","UserName==\"Jane\"", new
                  Subscriber1(),
                  strdup("Listen to Jane"));

            p.Subscribe(
                  "Sample.Chat",
                  "((Gender == 'F' or Gender == 'f')
                        and Age < 14)",
                  new Subscriber1(), strdup("Girl Scouts"));

            p.Subscribe(
                  "Sample.Chat",
                  "Interest == \"Country Music\"Music\"",
                  new Subscriber2(),
                  strdup("Country music fans"));

            cout<<"Subscriptions were submitted...
                  waiting 30 seconds for incoming
                  notifications"
                  <<endl;

            sleep(30);

      }
      catch (PCException &e)

      {

            cerr<<e.Message().String()<<endl;

      }

}