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. ( Callback Function)
  3. Create the subscription. ( Subscription)
  4. Associate a closure with the subscription. ( Subscription closure)
  5. Retrieve the content of the notification. ( Notification)

#include <stdio.h>

#include "PC_utl_log.h"

#include "PC_evn_filter.h"

#include "PC_evn_proxy.h"

#include "PC_evn_notification.h"

//Define a callback function.

void notify(PC_evn_Notification notification, void* pClosure)

{

      PC_STRING message;

      PC_UINT size;

//Retrieve the value of the predefined attribute.

      PC_evn_get_predefined_attr_by_name(
            notification,
            "Message",&message,
            PC_STRING_TYPE,&size);
      

      printf("Incoming notification for (%s):%s\n",
            (char*)pClosure,message);

};

int

main(int argc, char* argv[])

{

      PC_Status nStatus;

//Create a proxy for the channel and client.

      PC_evn_Proxy p;

      PC_evn_Subscription_Handle hSubscription;

      nStatus = PC_evn_create_proxy(&p,7,3001);

      

      if (nStatus != PC_SUCCESS)      

      {

            printf("Proxy creation has failed\n");

            return 0;

      }

//Submit the subscription.

      nStatus = PC_evn_subscribe_with_exp(
            p,"Sample.Intro1",
            PC_NULL,
            notify,
            strdup("My First Subscription"),
            PC_NULL, &hSubscription);

      if (nStatus != PC_SUCCESS)

      {

            printf("Subscription submission has failed
                  \n");

            return 0;

      }

            printf("Subscription was submitted
            ... waiting 30 seconds for incoming
            notifications\n");

      sleep(30);

}

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