2014-08-23 星期六 20:55:28
见代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/time.h>
typedef
void
(*pUpadte)(
int
);
// 订阅者回调
typedef
struct
_observer_t
{
int
id;
pUpadte update;
}observer_t;
// 订阅者链表,可以直接用linux list
//简单点的话,就用array[id],其实负载多了也不好
typedef
struct
_observer_list_t
{
observer_t *observer;
struct
_observer_list_t *next;
}observer_list_t;
// 发布者
typedef
struct
_subject_t
{
int
data;
observer_list_t *observer_list;
}subject_t;
//update
void
update0(
int
data)
{
printf
(
"line %03d, %s recv data(%d)\n"
, __LINE__, __func__, data);
}
void
update1(
int
data)
{
printf
(
"line %03d, %s recv data(%d)\n"
, __LINE__, __func__, data);
}
void
update2(
int
data)
{
printf
(
"line %03d, %s recv data(%d)\n"
, __LINE__, __func__, data);
}
void
update3(
int
data)
{
printf
(
"line %03d, %s recv data(%d)\n"
, __LINE__, __func__, data);
}
int
myid = 0;
void
initobserver(observer_t *observer, pUpadte update)
{
observer->id = myid++;
observer->update = update;
}
observer_list_t* make_observer_list(observer_t *observer)
{
observer_list_t *observer_list = NULL;
if
( !(observer_list = (observer_list_t *)
malloc
(
sizeof
(observer_list_t))) )
{
printf
(
"line %d, %s, err\n"
, __LINE__, __func__);
return
NULL;
}
if
( !(observer_list->observer = (observer_t *)
malloc
(
sizeof
(observer_t))) )
{
printf
(
"line %d, %s, err\n"
, __LINE__, __func__);
return
NULL;
}
//printf("line %d, %s, observer->id(%d)\n", __LINE__, __func__, observer->id);
observer_list->observer = observer;
observer_list->next = NULL;
return
observer_list;
}
/*在头插入*/
void
insert_at_front(observer_list_t **pphead, observer_list_t *pnode)
{
pnode ->next=*pphead;
*pphead=pnode;
}
/*在尾插入*/
void
insert_at_rear0(observer_list_t **ppfirst, observer_list_t* pnode)
{
observer_list_t *t_observer_list = *ppfirst;
if
(*ppfirst == NULL)
{
*ppfirst = pnode;
printf
(
"line %d, %s, node 0\n"
, __LINE__, __func__);
}
else
{
while
(t_observer_list->next)
{
t_observer_list = t_observer_list->next;
printf
(
"line %d, %s\n"
, __LINE__, __func__);
}
t_observer_list->next = pnode;
}
}
void
insert_at_rear1(observer_list_t **ppfirst, observer_list_t* pnode)
{
while
(*ppfirst)
{
ppfirst = &(*ppfirst)->next;
printf
(
"line %d, %s\n"
, __LINE__, __func__);
}
*ppfirst = pnode;
}
#define insert_at_rear insert_at_rear1
// 加入订阅者链表
int
attach2subject0(subject_t *subject, observer_t *observer)
{
// 将observer加入subject中的observer链表
//assert(subject);
observer_list_t *pnode = make_observer_list(observer);
insert_at_rear(&subject->observer_list, pnode);
return
0;
}
//global,first_observer_list给外部调用
observer_list_t *first_observer_list = NULL;
// 加入订阅者链表
int
attach2subject1(subject_t *subject, observer_t *observer)
{
// 将observer加入subject中的observer链表
//assert(subject);
observer_list_t **ppobserver_list;
ppobserver_list = &first_observer_list;
observer_list_t *pnode = make_observer_list(observer);
while
(*ppobserver_list)
{
ppobserver_list = &(*ppobserver_list)->next;
}
*ppobserver_list = pnode;
subject->observer_list = first_observer_list;
return
0;
}
#define attach2subject attach2subject0
void
dump_observer_list0(observer_list_t *pnode)
{
#if 0
while
(first_observer_list)
{
if
(first_observer_list->observer)
{
printf
(
"line %d, %s, id(%d)\n"
, __LINE__, __func__, first_observer_list->observer->id);
}
first_observer_list = first_observer_list->next;
}
#else //not change first_observer_list
int
cnt = 0;
pnode = first_observer_list;
while
(pnode)
{
if
( (pnode->observer) && (pnode->observer->update) )
{
++cnt;
printf
(
"line %d, %s, id(%d)\n"
, __LINE__, __func__, pnode->observer->id);
}
pnode = pnode->next;
}
printf
(
"line %03d, %s, dump %d observer\n"
, __LINE__, __func__, cnt);
#endif
}
void
dump_observer_list1(observer_list_t *pnode)
{
int
cnt = 0;
while
(pnode)
{
if
( (pnode->observer) && (pnode->observer->update) )
{
++cnt;
printf
(
"line %d, %s, id(%d)\n"
, __LINE__, __func__, pnode->observer->id);
}
pnode = pnode->next;
}
printf
(
"line %03d, %s, dump %d observer\n"
, __LINE__, __func__, cnt);
}
#define dump_observer_list dump_observer_list0
int
free_observer_list(observer_list_t *pnode)
{
if
(pnode)
{
free
(pnode);
}
}
void
clr_observer_list(observer_list_t *pnode)
{
free_observer_list(pnode);
pnode->observer = NULL;
pnode = NULL;
}
// 从订阅者链表删除
int
detach4subject0(subject_t *subject, observer_t *observer)
{
//assert(subject);
if
(!observer)
{
return
-1;
}
printf
(
"\nline %03d, %s, pre to del id(%d)\n"
, __LINE__, __func__, observer->id);
observer_list_t *pcurr, *pprev;
observer_list_t **ppfirst;
ppfirst = &subject->observer_list;
first_observer_list = *ppfirst;
#if 1
for
(pcurr=*ppfirst; pcurr; pprev=pcurr, pcurr=pcurr->next)
{
//first
if
(observer == (*ppfirst)->observer)
{
if
((*ppfirst)->next)
{
printf
(
"line %03d, %s, id(%d)\n"
, __LINE__, __func__, (*ppfirst)->observer->id);
printf
(
"line %03d, %s, id(%d)\n"
, __LINE__, __func__, subject->observer_list->observer->id);
ppfirst = &(*ppfirst)->next;
//free_observer_list(pcurr);
printf
(
"line %03d, %s, id(%d)\n"
, __LINE__, __func__, (*ppfirst)->observer->id);
//printf("line %03d, %s, id(%d)\n", __LINE__, __func__, subject->observer_list->next->observer->id);
}
else
//仅有1个node
{
printf
(
"line %d, %s, only one node\n"
, __LINE__, __func__);
//free_observer_list(*ppfirst);
//free_observer_list(pcurr);
/*不置为null,subject->observer_list为野指针,
在notify2observer()仍旧会调用update
*/
//*ppfirst = NULL;
}
clr_observer_list(pcurr);
printf
(
"line %d, %s, return\n"
, __LINE__, __func__);
return
0;
}
else
if
(observer == pcurr->observer)
{
printf
(
"line %d, %s, return\n"
, __LINE__, __func__);
pprev->next = pcurr->next;
clr_observer_list(pcurr);
return
0;
}
}
#endif
printf
(
"line %d, %s, has deled or not exist\n"
, __LINE__, __func__);
return
-2;
}
int
detach4subject1(subject_t *subject, observer_t *observer)
{
//assert(subject);
observer_list_t **ppobserver_list;
ppobserver_list = &first_observer_list;
while
(subject)
{
}
return
0;
}
#define detach4subject detach4subject0
// 修改被观察数据
int
setdata(subject_t *subject,
int
value)
{
subject->data = value;
}
// 通知观察者
void
notify2observer(subject_t *subject)
{
if
(!subject->observer_list)
{
printf
(
"\nline %03d, %s, observer_list NULL\n"
, __LINE__, __func__);
return
;
}
printf
(
"\nline %03d, %s\n"
, __LINE__, __func__);
int
cnt = 0;
observer_list_t *p_observer_list_t = subject->observer_list;
while
(p_observer_list_t)
{
if
( (p_observer_list_t->observer)
&& (p_observer_list_t->observer->update)
)
{
++cnt;
p_observer_list_t->observer->update(subject->data);
printf
(
"line %03d, %s\n"
, __LINE__, __func__);
}
p_observer_list_t = p_observer_list_t->next;
printf
(
"line %03d, %s\n"
, __LINE__, __func__);
}
if
(cnt)
{
printf
(
"line %03d, %s, has %d observer\n"
, __LINE__, __func__, cnt);
}
else
{
printf
(
"line %03d, %s, has NOT observer\n"
, __LINE__, __func__);
}
}
// 初始化主题对象
void
initsubject(subject_t *subject)
{
#if 0
observer_t *p_observer;
observer_list_t *p_observer_list;
p_observer = (observer_t *)
malloc
(
sizeof
(observer_t));
p_observer_list = (observer_list_t *)
malloc
(
sizeof
(observer_list_t));
p_observer_list->observer = p_observer;
#else
observer_t t_observer;
observer_list_t t_observer_list;
t_observer_list.observer = &t_observer;
#endif
//assert(subject);
memset
(subject, 0,
sizeof
(subject_t));
#if 0
if
(!subject)
{
subject = (subject_t *)
malloc
(
sizeof
(subject_t));
}
#endif
//make_observer_list
//subject->observer_list = (observer_list_t *)malloc(sizeof(observer_list_t));
//subject->observer_list->observer = (observer_t *)malloc(sizeof(observer_t));
}
//subject_t subject;
int
main(
int
argc,
const
char
*agrc[])
{
observer_t observer0;
observer_t observer1;
observer_t observer2;
observer_t observer3;
subject_t subject;
// 初始化订阅者
initobserver(&observer0, update0);
initobserver(&observer1, update1);
initobserver(&observer2, update2);
initobserver(&observer3, update3);
// 初始化发布者
initsubject(&subject);
// 订阅者关联发布者
attach2subject(&subject, &observer0);
attach2subject(&subject, &observer1);
attach2subject(&subject, &observer2);
attach2subject(&subject, &observer3);
printf
(
"\nline %03d, %s, notify date\n"
, __LINE__, __func__);
// 修改状态,并通知观察者
setdata(&subject, 1);
notify2observer(&subject);
printf
(
"\nline %03d, %s, refresh date\n"
, __LINE__, __func__);
setdata(&subject, 2);
notify2observer(&subject);
dump_observer_list(subject.observer_list);
detach4subject(&subject, &observer1);
dump_observer_list(subject.observer_list);
//setdata(&subject, 3);
//notify2observer(&subject);
//return 0;
detach4subject(&subject, &observer2);
detach4subject(&subject, &observer3);
detach4subject(&subject, &observer0);
setdata(&subject, 4);
notify2observer(&subject);
dump_observer_list(subject.observer_list);
return
0;
}
|