design_pattern_for_c  V 1.00
publish_content.c
Go to the documentation of this file.
1 
5 #include "publish_content.h"
6 #include <stdlib.h>
7 #include <pthread.h>
8 #include "dp_util.h"
14  void (*notify)(int publish_type, void * detail, void * ctx);
15  void * ctx;
16 };
17 
21  pthread_mutex_t lock;
22 };
23 
24 
26 /* @{ */
30 }
34  free(account);
35 }
39  free(account);
40  return account;
41 }
42 #define PUBLISH_CONTENT_LOCK(content) DPUTIL_LOCK(&(content->lock))
43 #define PUBLISH_CONTENT_UNLOCK DPUTIL_UNLOCK
44 /* @} */
45 
46 /*************
47  * public interface API implement
48 *************/
50  PublishContent content = (PublishContent)calloc(1, sizeof(publish_content_t));
51  pthread_mutex_init(&content->lock, NULL);
52  return content;
53 }
54 
55 SubscriberAccount publish_content_subscribe(PublishContent this, int publish_type, void (*notify)(int publish_type, void * detail, void * ctx), void * ctx) {
56  SubscriberAccount account=NULL;
57 
59  account = (SubscriberAccount)calloc(1, sizeof(publish_content_t));
60  if(account) {
61  account->publish_type = publish_type;
62  account->notify = notify;
63  account->ctx = ctx;
64  publish_content_push_subscriber(this, account);
65  }
67  return account;
68 }
69 
70 void publish_content_subscribe_oneshot(PublishContent this, int publish_type, void (*notify)(int publish_type, void * detail, void * ctx), void * ctx) {
71 
73  SubscriberAccount account = (SubscriberAccount)calloc(1, sizeof(publish_content_t));
74  if(account) {
75  account->is_oneshot = 1;
76  account->publish_type = publish_type;
77  account->notify = notify;
78  account->ctx = ctx;
79  publish_content_push_subscriber(this, account);
80  }
82 }
83 
86  publish_content_pull_subscriber(this, account);
88 
89 }
90 
91 void publish_content_publish(PublishContent this, int publish_type, void * detail) {
93  SubscriberAccount account, account_next;
94  account = this->head;
95  while(account) {
96  /* check type */
97  if((account->publish_type & publish_type) != publish_type) {
98  account=account->next;
99  continue;
100  }
101 
102  /* notify message, notify is not null because publisher check it */
103  account->notify(publish_type, detail, account->ctx);
104  if(account->is_oneshot) {
105  account_next = account->next;
106  publish_content_pull_subscriber(this, account);
107  account=account_next;
108  } else {
109  account=account->next;
110  }
111  }
112 
114 }
115 
118  SubscriberAccount account;
119 
120  /* free all account */
121  account=this->head;
122  while(account) {
123  /* unsubscribe head */
124  account = publish_content_pop_subscriber(this);
125  }
126 
128  /* free this after unlick*/
129  free(this);
130 }
static void publish_content_pull_subscriber(PublishContent this, SubscriberAccount account)
pull subscriber
Utility headers
SubscriberAccount class member definition, to get by publisher_subscribe.
void publish_content_subscribe_oneshot(PublishContent this, int publish_type, void(*notify)(int publish_type, void *detail, void *ctx), void *ctx)
static void publish_content_push_subscriber(PublishContent this, SubscriberAccount account)
push subscriber
SubscriberAccount head
SubscriberAccount next
void dputil_list_pull(DPUtilList this, DPUtilListData data)
list pull
Definition: dp_util.c:59
PublishContent publish_content_new(void)
void(* free)(EventInstance this)
Definition: event_thread.c:39
SubscriberAccount tail
void(* notify)(int publish_type, void *detail, void *ctx)
SubscriberAccount prev
pthread_mutex_t lock
DPUtilListData dputil_list_pop(DPUtilList this)
list pop
Definition: dp_util.c:80
void dputil_list_push(DPUtilList this, DPUtilListData data)
list push
Definition: dp_util.c:21
void publish_content_publish(PublishContent this, int publish_type, void *detail)
struct subscriber_account_t * SubscriberAccount
Definition: publisher.h:19
SubscriberAccount publish_content_subscribe(PublishContent this, int publish_type, void(*notify)(int publish_type, void *detail, void *ctx), void *ctx)
void publish_content_free(PublishContent this)
static SubscriberAccount publish_content_pop_subscriber(PublishContent this)
pop subscriber
void publish_content_unsubscribe(PublishContent this, SubscriberAccount account)
This is API for PublishContent class which managed in Publisher.
struct publish_content_t * PublishContent
#define PUBLISH_CONTENT_UNLOCK
#define PUBLISH_CONTENT_LOCK(content)