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 <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 notify1(
      PC_evn_Notification notification,
      void* pClosure)

{

      PC_STRING message;

      PC_STRING username;

      PC_CHAR *gender;

      PC_LONG *age;

      PC_UINT size;

//Retrieve the values of predefined attributes.

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

      PC_evn_get_predefined_attr_by_name(
            notification,
            "UserName",&username,

            PC_STRING_TYPE,&size);

      PC_evn_get_predefined_attr_by_name(
            notification,
            "Age",&age,
            PC_LONG_TYPE,&size);

      PC_evn_get_predefined_attr_by_name(
            notification,
            "Gender",&gender,

            PC_CHAR_TYPE,&size);

//Build the notification.

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

      printf("From:\n");

      printf("Name = %s\n",username);

      printf("Age = %d\n",(int)*age);

      if ((*gender == 'M')||(*gender=='m'))

            printf("Male\n")       ;

      else

            if ((*gender == 'F')||(*gender=='f'))

            printf("Female\n");

};

//Define a callback function which can retrieve a
//discretionary attribute.

void notify2(
      PC_evn_Notification notification,
      void* pClosure)

{

      PC_STRING message;

      PC_STRING username;

      PC_CHAR *gender;

      PC_LONG *age;

      PC_UINT size;

      PC_TypeCode typecode;

      PC_STRING interest;

//Retrieve the value of the predefined attributes.

      PC_evn_get_predefined_attr_by_name(
            notification,
            "Message",&message,

            PC_STRING_TYPE,&size);      

      PC_evn_get_predefined_attr_by_name(
            notification,
            "UserName",&username,

            PC_STRING_TYPE,&size);

      PC_evn_get_predefined_attr_by_name(
            notification,
            "Age",&age,

            PC_LONG_TYPE,&size);

      PC_evn_get_predefined_attr_by_name(
            notification,
            "Gender",&gender,

            PC_CHAR_TYPE,&size);

//Retrieve the value of the discretionary attribute.

      PC_evn_get_discretionary_attr(
            notification,
            "Interest",&interest,
            &typecode,&size);

//Build the notification.

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

      printf("From:\n");

      printf("Name = %s\n",username);

      printf("Age = %d\n",(int)*age);

      if ((*gender == 'M')||(*gender=='m'))

            printf("Male\n")       ;

      else

            if ((*gender == 'F')||(*gender=='f'))

                  printf("Female\n");

      printf("Interest = %s\n\n",interest);

};

int

main(int argc, char* argv[])

{

      PC_Status nStatus;

      PC_evn_Proxy p;

      PC_evn_Subscription_Handle
            hSubscription1,
            hSubscription2,
            hSubscription3;

//Create a proxy for the channel and client.

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

      

      if (nStatus != PC_SUCCESS)      

      {

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

            return 0;

      }      

//Submit the subscription.

      nStatus = PC_evn_subscribe_with_exp(
            p,"Sample.Chat",

            "UserName==\"Jane\"UserName==\"Jane\"",
            notify1,
            strdup("Listen to Jane"),
            PC_NULL, &hSubscription1);

      if (nStatus != PC_SUCCESS)

      {

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

            return 0;

      }

      nStatus = PC_evn_subscribe_with_exp(
            p,"Sample.Chat",
            "((Gender == 'F' or Gender == 'f')
            and Age < 14)",
      notify1,strdup("Girl Scouts"),
      PC_NULL, &hSubscription2);

      if (nStatus != PC_SUCCESS)

      {

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

            return 0;

      }

      nStatus = PC_evn_subscribe_with_exp(
            p,"Sample.Chat",
            "Interest == \"Country Music\"Music\"",
            notify2,strdup("Country music fans"),
            PC_NULL, &hSubscription3);

      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);

}