PreCache NetInjector Documentation Center : PreCache NetInjector Application Development

Basic Case, Subscription — C++

   







Basic Case, Subscription code — C++

— the complete subscription code


The previous code samples presented the basics of subscription. A subscription application retrieves content in much the same way as it was published, albeit after first defining a subscription class.

Here is the basic sequence of the subscription process:

  1. Create a proxy connection for the channel. ( Proxy Initialization, Basic Case, Publication — C++)
  2. Define a subscriber class. ( Subscriber)
  3. Create the subscription. ( Subscription)
  4. Associate a closure with the subscription. ( Subscription closure)
  5. Retrieve the content of the notification. ( Notification)

#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.

class MySubscriber:public Subscriber

{

      public:

//Define the callback method.

      virtual void Notify(
            Notification* pNotification,
            void* pClosure)
            throw (precache::util::PCException)

      {

            PC_STRING message;

//Retrieve the value of the predefined attribute.

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

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

      }

};

int

main(int argc, char* argv[])

{      

      try

      {

//Create a proxy for the channel and client.

            Proxy p(7,3002);

//Submit the subscription.

            p.Subscribe(
                  "Sample.Intro1",new MySubscriber(),
                  strdup("My First Subscription"));

            cout<<"Subscription was submitted...
            waiting 30 seconds for incoming
            notifications"
            <<endl;

            sleep(30);

      }

      catch (PCException &e)

      {

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

      }

}

In the next chapter, Common Cases, Publication — C++, we will learn more about attributes.