xref: /minix3/minix/tests/ds/subs.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #include "inc.h"
2*433d6423SLionel Sambuc 
3*433d6423SLionel Sambuc char *key_u32 = "test_u32";
4*433d6423SLionel Sambuc 
5*433d6423SLionel Sambuc /* SEF functions and variables. */
6*433d6423SLionel Sambuc static void sef_local_startup(void);
7*433d6423SLionel Sambuc 
8*433d6423SLionel Sambuc /*===========================================================================*
9*433d6423SLionel Sambuc  *				main					     *
10*433d6423SLionel Sambuc  *===========================================================================*/
main(void)11*433d6423SLionel Sambuc int main(void)
12*433d6423SLionel Sambuc {
13*433d6423SLionel Sambuc 	int r;
14*433d6423SLionel Sambuc 	message mess;
15*433d6423SLionel Sambuc 	char key[DS_MAX_KEYLEN];
16*433d6423SLionel Sambuc 	int type;
17*433d6423SLionel Sambuc 	u32_t num;
18*433d6423SLionel Sambuc 	char string[17];
19*433d6423SLionel Sambuc 	char buf[1000];
20*433d6423SLionel Sambuc 	size_t length = 1000;
21*433d6423SLionel Sambuc 
22*433d6423SLionel Sambuc 	/* SEF local startup. */
23*433d6423SLionel Sambuc 	sef_local_startup();
24*433d6423SLionel Sambuc 
25*433d6423SLionel Sambuc 	/* Subscribe. */
26*433d6423SLionel Sambuc 	r = ds_subscribe(key_u32, DSF_INITIAL);
27*433d6423SLionel Sambuc 	if(r != OK && r != EEXIST) {
28*433d6423SLionel Sambuc 		printf("SUBSCRIBER: error in ds_subscribe: %d\n", r);
29*433d6423SLionel Sambuc 		return -1;
30*433d6423SLionel Sambuc 	}
31*433d6423SLionel Sambuc 
32*433d6423SLionel Sambuc 	while(1) {
33*433d6423SLionel Sambuc 		/* Wait for a message. */
34*433d6423SLionel Sambuc 		r = sef_receive(ANY, &mess);
35*433d6423SLionel Sambuc 		if(r != OK) {
36*433d6423SLionel Sambuc 			printf("SUBSCRIBER: sef_receive failed.\n");
37*433d6423SLionel Sambuc 			return 1;
38*433d6423SLionel Sambuc 		}
39*433d6423SLionel Sambuc 		/* Only handle notifications from DS. */
40*433d6423SLionel Sambuc 		if(mess.m_source != DS_PROC_NR)
41*433d6423SLionel Sambuc 			continue;
42*433d6423SLionel Sambuc 
43*433d6423SLionel Sambuc 		/* Check which one was changed. */
44*433d6423SLionel Sambuc 		r = ds_check(key, &type, NULL);
45*433d6423SLionel Sambuc 		if(r == ENOENT) {
46*433d6423SLionel Sambuc 			printf("SUBSCRIBER: the key %s was deleted.\n",
47*433d6423SLionel Sambuc 				key);
48*433d6423SLionel Sambuc 			continue;
49*433d6423SLionel Sambuc 		}
50*433d6423SLionel Sambuc 		if(r != OK) {
51*433d6423SLionel Sambuc 			printf("SUBSCRIBER: error in ds_check.\n");
52*433d6423SLionel Sambuc 			continue;
53*433d6423SLionel Sambuc 		}
54*433d6423SLionel Sambuc 
55*433d6423SLionel Sambuc 		/* Retrieve the entry. */
56*433d6423SLionel Sambuc 		printf("SUBSCRIBER: key: %s, ", key);
57*433d6423SLionel Sambuc 		switch(type) {
58*433d6423SLionel Sambuc 		case DSF_TYPE_U32:
59*433d6423SLionel Sambuc 			r = ds_retrieve_u32(key, &num);
60*433d6423SLionel Sambuc 			if(r != OK)
61*433d6423SLionel Sambuc 				printf("error in ds_retrieve_u32.\n");
62*433d6423SLionel Sambuc 			printf("U32: %d\n", num);
63*433d6423SLionel Sambuc 			break;
64*433d6423SLionel Sambuc 		case DSF_TYPE_STR:
65*433d6423SLionel Sambuc 			r = ds_retrieve_str(key, string, sizeof(string)-1);
66*433d6423SLionel Sambuc 			if(r != OK)
67*433d6423SLionel Sambuc 				printf("error in ds_retrieve_str.\n");
68*433d6423SLionel Sambuc 			printf("STR: %s\n", string);
69*433d6423SLionel Sambuc 			break;
70*433d6423SLionel Sambuc 		case DSF_TYPE_MEM:
71*433d6423SLionel Sambuc 			r = ds_retrieve_mem(key, buf, &length);
72*433d6423SLionel Sambuc 			if(r != OK)
73*433d6423SLionel Sambuc 				printf("error in ds_retrieve_mem.\n");
74*433d6423SLionel Sambuc 			break;
75*433d6423SLionel Sambuc 		default:
76*433d6423SLionel Sambuc 			printf("error in type! %d\n", type);
77*433d6423SLionel Sambuc 		}
78*433d6423SLionel Sambuc 	}
79*433d6423SLionel Sambuc 
80*433d6423SLionel Sambuc 	return 0;
81*433d6423SLionel Sambuc }
82*433d6423SLionel Sambuc 
83*433d6423SLionel Sambuc /*===========================================================================*
84*433d6423SLionel Sambuc  *			       sef_local_startup			     *
85*433d6423SLionel Sambuc  *===========================================================================*/
sef_local_startup()86*433d6423SLionel Sambuc static void sef_local_startup()
87*433d6423SLionel Sambuc {
88*433d6423SLionel Sambuc   /* Let SEF perform startup. */
89*433d6423SLionel Sambuc   sef_startup();
90*433d6423SLionel Sambuc }
91*433d6423SLionel Sambuc 
92