-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
62 lines (53 loc) · 1.17 KB
/
module.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
package module
import (
"encoding/gob"
"fmt"
"github.com/glennyonemitsu/reicon/system/activity"
"net"
"os"
)
type Module struct {
Name string
Activities []*activity.Activity
ServerSocket *net.UnixConn
SocketAddr *net.UnixAddr
Listener *net.UnixListener
}
func (m *Module) Run() {
socket_path := os.Getenv("REICON_SYSTEM_SOCKET")
m.SocketAddr = new(net.UnixAddr)
m.SocketAddr.Name = "unix"
m.SocketAddr.Net = socket_path
m.Listener, _ = net.ListenUnix("unix", m.SocketAddr)
m.ListenSocket()
}
func (m *Module) ListenSocket() {
for {
conn, err := m.Listener.AcceptUnix()
if err != nil {
continue
}
go m.handleUnixConnection(conn)
}
}
func (m *Module) handleUnixConnection(conn *net.UnixConn) {
var msg string
enc := gob.NewEncoder(conn)
dec := gob.NewDecoder(conn)
dec.Decode(&msg)
enc.Encode(fmt.Sprintf("your message was: %s", msg))
}
func (m *Module) Shutdown() {
m.ServerSocket.Close()
}
func (m *Module) RegisterActivity(name string) *activity.Activity {
a := new(activity.Activity)
a.Name = name
m.Activities = append(m.Activities, a)
return a
}
func Create(name string) *Module {
m := new(Module)
m.Name = name
return m
}