design_pattern_for_c  V 1.00
main.c
Go to the documentation of this file.
1 #include "publisher.h"
2 #include "event_threadpool.h"
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <errno.h>
8 
9 static void event_publish(int socketfd, int eventflag, void * event_arg) {
10  char buf[256]={0};
11  if(eventflag|EV_TPOOL_READ)
12  read(socketfd, buf, sizeof(buf));
13  publisher_publish(1, socketfd, buf);
14 }
15 
17  event_subscriber_t stdout_subscriber ={STDOUT_FILENO, EV_TPOOL_READ, event_publish};
18  event_tpool_add(tpool, &stdout_subscriber, NULL);
19 }
20 
21 void publisher_notify(int publish_type, void * detail, void *ctx) {
22  fprintf(stderr, "enter publisher_notify\n");
23  char * str = (char *)detail;
24  char * str_ctx = (char *)ctx;
25  printf("input:%s(context:%s)\n", str, str_ctx);
26 }
27 
28 int main() {
29  int maxfd=1, ret = 0;;
31  add_event_publisher(tpool);
32  publisher_new(1);
33 
34  publisher_subscribe(1, STDOUT_FILENO, publisher_notify, "for stdout");//for stdout
35  while(1) {
36  ret = select(maxfd+1, NULL, NULL, NULL, NULL);
37  if(ret<0) {
38  printf("exit\n");
39  break;
40  }
41  }
42  return 0;
43 }
#define EV_TPOOL_READ
EventTPoolManager event_tpool_manager_new(int thread_num, int is_threadsafe, const char *plugin_path)
new EventTPoolManager
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
int main()
Definition: main.c:28
static void event_publish(int socketfd, int eventflag, void *event_arg)
Definition: main.c:9
This is API as ThreadPool design petten by using libevent.
event_tpool_add_result_t event_tpool_add(EventTPoolManager this, EventSubscriber subscriber, void *arg)
add EventSubscriber to threadpool
int publisher_new(size_t contents_num)
new Publisher content, user can get notify to subscribe.
Definition: publisher.c:40
EventTPoolManager class instance definition.
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
EventSubscriber class instance definition, this is storaged in any threads.
static void add_event_publisher(EventTPoolManager tpool)
Definition: main.c:16
void publisher_notify(int publish_type, void *detail, void *ctx)
Definition: main.c:21
This is API as Observer(Publish-Subscribe) design petten.