-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipc.go
161 lines (138 loc) · 3.33 KB
/
ipc.go
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
// Copyright (c) 2018, Mark "Happy-Ferret" Bauermeister
//
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
package event
import (
"encoding/json"
"fmt"
"net"
"os"
"reflect"
"time"
"github.com/BurntSushi/xgbutil"
"github.com/Anima-OS/Wonderland/logger"
)
var subs subscriptions
func Notifier(X *xgbutil.XUtil, fp string) {
fp = fp + "-notify"
os.Remove(fp)
listener, err := net.Listen("unix", fp)
if err != nil {
logger.Error.Fatalln("Could not start IPC event listener: %s", err)
}
defer listener.Close()
subs = manageSubscriptions()
for {
conn, err := listener.Accept()
if err != nil {
logger.Warning.Printf("Error accepting IPC event conn: %s", err)
continue
}
go handleSubscriber(conn)
}
}
func Notify(ev Event) {
if subs.notify == nil {
return
}
subs.notify <- ev
}
func handleSubscriber(conn net.Conn) {
defer conn.Close()
id, events := subs.subscribe()
defer subs.unsubscribe(id)
logger.Message.Printf("Accepted new event subscriber (id: %d).", id)
encoder := json.NewEncoder(conn)
writeEvent := func(ev Event) error {
if err := encoder.Encode(eventToMap(ev)); err != nil {
return err
}
if _, err := fmt.Fprintf(conn, "%c", 0); err != nil {
return err
}
return nil
}
if err := writeEvent(Subscribed{}); err != nil {
logger.Warning.Printf("Error sending initial subscription: %s", err)
return
}
for {
select {
case <-time.After(5 * time.Second):
if err := writeEvent(Noop{}); err != nil {
logger.Warning.Printf("Subscriber timed out: %s", err)
return
}
case ev := <-events:
if err := writeEvent(ev); err != nil {
logger.Warning.Printf("Error sending event: %s", err)
return
}
}
}
}
// eventToMap converts an event struct into a map.
// This is a terrible hack in order to inject the event name automatically.
func eventToMap(ev Event) map[string]interface{} {
rv := reflect.ValueOf(ev)
rt := rv.Type()
m := make(map[string]interface{})
m["EventName"] = rt.Name()
nf := rv.NumField()
for i := 0; i < nf; i++ {
m[rt.Field(i).Name] = rv.Field(i).Interface()
}
return m
}
type subscriptions struct {
add chan chan subscriber // sends info back on the given channel
remove chan int
notify chan Event
}
type subscriber struct {
id int
events chan Event
}
func (ss subscriptions) subscribe() (int, chan Event) {
recv := make(chan subscriber)
ss.add <- recv
scriber := <-recv
return scriber.id, scriber.events
}
func (ss subscriptions) unsubscribe(id int) {
ss.remove <- id
}
func manageSubscriptions() subscriptions {
nextId := int(1)
subscribed := make(map[int]chan Event)
script := subscriptions{
make(chan chan subscriber),
make(chan int),
make(chan Event),
}
go func() {
for {
select {
case recv := <-script.add:
subscribed[nextId] = make(chan Event, 100)
recv <- subscriber{nextId, subscribed[nextId]}
nextId++
case id := <-script.remove:
close(subscribed[id])
delete(subscribed, id)
logger.Message.Printf("Subscriber disconnected (id: %d).", id)
case ev := <-script.notify:
for _, subscriber := range subscribed {
// Do a non-blocking send so that we drop notifications
// when the client gets too busy (or fails).
select {
case subscriber <- ev:
default:
}
}
}
}
}()
return script
}