1 /**
2 Copyright 2018 Mark Fisher
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 **/
22 module dxx.util.notify;
23 
24 private import std.algorithm;
25 private import std.parallelism;
26 
27 private import std.experimental.logger;
28 
29 /++
30 Simple notification service.
31 
32 An object that inherits NotificationSource maintains a vector of
33 NotificationListener's.
34 
35 ++/
36 
37 interface NotificationListener {
38   shared void handleNotification(void* t);
39 };
40 
41 interface NotificationSource {
42   shared void addNotificationListener(shared(NotificationListener) n);
43   shared void removeNotificationListener(shared(NotificationListener) n);
44 }
45 
46 /++
47 Synchronized notification handler.
48 ++/
49 
50 class SyncNotificationSource : NotificationSource {
51 
52   NotificationListener[] notificationListeners;
53 
54   nothrow shared
55   void send(T)(T* t) {
56     debug(Notify) {
57         sharedLog.info("SyncNotificationSource : send ",typeid(T)," ",notificationListeners.length);
58     }
59     auto ar = notificationListeners.dup;
60     try {
61       ar.parallel.each!(x=>x.handleNotification(cast(void*)&t));
62     } catch(Exception e) {
63       //sharedLog.error(e.message); nothrow!
64     }
65   }
66 
67   override shared void addNotificationListener(shared(NotificationListener) n) {
68     debug(Notify) {
69         sharedLog.info("SyncNotificationSource : addNotificationListener ",notificationListeners.length);
70     }
71     notificationListeners ~= n;
72   }
73 
74   override shared void removeNotificationListener(shared(NotificationListener) n) {
75     debug(Notify) {
76         sharedLog.info("SyncNotificationSource : removeNotificationListener",notificationListeners.length);
77     }
78     notificationListeners = notificationListeners.remove(notificationListeners.countUntil(n));
79   }
80 }
81 
82 unittest {
83     shared(bool) done = false;
84     class TestNotificationListener : NotificationListener {
85         override synchronized void handleNotification(void* t) {
86             done = true;
87         }
88     }
89     auto n = new shared(SyncNotificationSource);
90     shared(NotificationListener) l = new shared(TestNotificationListener);
91     n.addNotificationListener(l);
92     string s = "";
93     n.send!string(&s);
94     assert(done is true);
95 
96     done = false;
97     n.removeNotificationListener(l);
98     n.send!string(&s);
99     assert(done is false);
100 }