design_pattern_for_c  V 1.00
publisher.c
Go to the documentation of this file.
1 
5 #include <stdlib.h>
6 #include <string.h>
7 #include "publish_content.h"
8 #include "dp_util.h"
9 
16 } publisher_g;
17 
18 /* @brief definition convert from ID to INDEX macro */
19 #define PC_INDEX_FROM_ID(content_id) ((content_id)-1)
20 
21 /*************
22  * private interface API implement
23 *************/
29 static PublishContent publisher_get_content(int content_id) {
30  if ((0 <content_id) && (content_id<=publisher_g.content_cnt) && (publisher_g.contents)) {
31  return publisher_g.contents[PC_INDEX_FROM_ID(content_id)];
32  } else {
33  return NULL;
34  }
35 }
36 
37 /*************
38  * public interface API implement
39 *************/
40 int publisher_new(size_t contents_num) {
41  /*is already called it?*/
42  if(publisher_g.contents) {
43  DEBUG_ERRPRINT("publisher_new was already called!\n");
44  return PUBLISHER_FAILED;
45  }
46 
47  if(contents_num==0) {
48  DEBUG_ERRPRINT("Content num is 0!\n");
49  return PUBLISHER_FAILED;
50  }
51 
52  publisher_g.contents = (PublishContent *)calloc(contents_num, sizeof(PublishContent));
53  if(!publisher_g.contents) {
54  DEBUG_ERRPRINT("failed to alloc PublishContent list!\n");
55  return PUBLISHER_FAILED;
56  }
57 
58  publisher_g.content_cnt = contents_num;
59 
60  int i=0;
61  for(i = 0; i < contents_num ; i ++) {
63  if(!publisher_g.contents[i]) {
64  DEBUG_ERRPRINT("failed to alloc PublishContent[%d]!\n", i);
65  goto err;
66  }
67  }
68 
69  return PUBLISHER_SUCCESS;
70 err:
72  return PUBLISHER_FAILED;
73 }
74 
75 void publisher_free(void) {
76  if(!publisher_g.contents) {
77  return;
78  }
79 
80  int i=0;
81  for(i = 0; i < publisher_g.content_cnt ; i ++) {
83  }
84 
86 
87  memset(&publisher_g, 0, sizeof(publisher_g));
88  return;
89 }
90 
91 SubscriberAccount publisher_subscribe(int content_id, int publish_type, void (*notify)(int publish_type, void * detail, void * ctx), void * ctx ) {
92  if(!notify || publish_type == 0) {
93  DEBUG_ERRPRINT("There is no notification information, please set it!\n");
94  return NULL;
95  }
96 
97  PublishContent content = publisher_get_content(content_id);
98  if(!content) {
99  DEBUG_ERRPRINT("invalid content_id\n");
100  return NULL;
101  }
102 
103  SubscriberAccount account= publish_content_subscribe(content, publish_type, notify, ctx);
104  return account;
105 }
106 
107 void publisher_subscribe_oneshot(int content_id, int publish_type, void (*notify)(int publish_type, void * detail, void * ctx), void * ctx ) {
108  if(!notify || publish_type == 0) {
109  DEBUG_ERRPRINT("There is no notification information, please set it!\n");
110  return;
111  }
112 
113  PublishContent content = publisher_get_content(content_id);
114  if(!content) {
115  DEBUG_ERRPRINT("invalid content_id\n");
116  return;
117  }
118 
119  publish_content_subscribe_oneshot(content, publish_type, notify, ctx);
120 }
121 
122 void publisher_unsubscribe(int content_id, SubscriberAccount account) {
123  PublishContent content = publisher_get_content(content_id);
124  if(!content) {
125  DEBUG_ERRPRINT("invalid content_id\n");
126  return;
127  }
128 
129  publish_content_unsubscribe(content, account);
130 }
131 
132 void publisher_publish(int content_id, int publish_type, void * detail) {
133  PublishContent content = publisher_get_content(content_id);
134  if(!content) {
135  DEBUG_ERRPRINT("invalid content_id\n");
136  return;
137  }
138 
139  publish_content_publish(content, publish_type, detail);
140 }
void publisher_free(void)
free All publisher content
Definition: publisher.c:75
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)
int publisher_new(size_t contents_num)
new Publisher content, user can get notify to subscribe.
Definition: publisher.c:40
PublishContent publish_content_new(void)
void(* free)(EventInstance this)
Definition: event_thread.c:39
void publisher_subscribe_oneshot(int content_id, int publish_type, void(*notify)(int publish_type, void *detail, void *ctx), void *ctx)
subscribe only oneshot
Definition: publisher.c:107
static PublishContent publisher_get_content(int content_id)
get content from id
Definition: publisher.c:29
PublishContent * contents
content cnt, to use fail safe (care list size)
Definition: publisher.c:15
struct publisher_mng_t publisher_g
void publisher_unsubscribe(int content_id, SubscriberAccount account)
unsubscribe, if you want to stop subscribe, please call it
Definition: publisher.c:122
void publish_content_publish(PublishContent this, int publish_type, void *detail)
#define PUBLISHER_SUCCESS
Definition: publisher.h:10
SubscriberAccount publisher_subscribe(int content_id, int publish_type, void(*notify)(int publish_type, void *detail, void *ctx), void *ctx)
subscribe
Definition: publisher.c:91
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)
#define PUBLISHER_FAILED
Definition: publisher.h:11
void publish_content_unsubscribe(PublishContent this, SubscriberAccount account)
This is API for PublishContent class which managed in Publisher.
#define DEBUG_ERRPRINT(...)
Definition: dp_debug.h:69
#define PC_INDEX_FROM_ID(content_id)
Definition: publisher.c:19
publisher class instance
Definition: publisher.c:13
void publisher_publish(int content_id, int publish_type, void *detail)
publish, Publisher call subscriber&#39;s notify if type is same
Definition: publisher.c:132