-
Notifications
You must be signed in to change notification settings - Fork 12
/
pipesock.go
209 lines (173 loc) · 4.62 KB
/
pipesock.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
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
package main
import (
"bufio"
"golang.org/x/net/websocket"
"encoding/json"
"flag"
"fmt"
"go/build"
"io"
"log"
"net/http"
"os"
"path/filepath"
"time"
)
type Hub struct {
Connections map[*Socket]bool
Pipe chan string
}
type Message struct {
Timestamp int64
Message string
}
type Broadcast struct {
Timestamp int64
Messages []*Message
}
func (h *Hub) BroadcastLoop() {
var currentMessages []*Message
ticker := time.NewTicker(time.Duration(delayMillis) * time.Millisecond)
for {
select {
case str := <-h.Pipe:
t := time.Now()
ts := t.UnixNano() / 1e6
newMessage := &Message{ts, str}
currentMessages = append(currentMessages, newMessage)
case <-ticker.C:
if len(currentMessages) > 0 {
t := time.Now()
ts := t.UnixNano() / 1e6
broadcast := &Broadcast{ts, currentMessages}
broadcastJSON, err := json.Marshal(broadcast)
if err != nil {
log.Println("Buffer JSON Error: ", err)
return
}
for s, _ := range h.Connections {
err := websocket.Message.Send(s.Ws, string(broadcastJSON))
if err != nil {
if err.Error() != ("use of closed network connection") {
log.Println("WS error:", err)
}
s.Ws.Close()
delete(h.Connections, s)
}
}
// Shuffle and push onto buffer, or grow if not yet at max
if len(broadcastBuffer) == bufferSize {
broadcastBuffer = broadcastBuffer[1:]
}
broadcastBuffer = append(broadcastBuffer, broadcast)
}
currentMessages = make([]*Message, 0)
}
}
}
type Socket struct {
Ws *websocket.Conn
}
func (s *Socket) ReceiveMessage() {
for {
var x []byte
err := websocket.Message.Receive(s.Ws, &x)
if err != nil {
break
}
}
s.Ws.Close()
}
func readLoop() {
r := bufio.NewReader(os.Stdin)
for {
str, err := r.ReadString('\n')
if err == io.EOF {
log.Fatal("EOF. Server closed.")
}
if err != nil {
log.Println("Read Line Error:", err)
continue
}
if len(str) == 0 {
continue
}
if passThrough {
fmt.Print(str)
}
h.Pipe <- str
}
}
func IndexHandler(w http.ResponseWriter, req *http.Request) {
var filePath string
if req.URL.Path == "/" {
filePath = fmt.Sprintf("%s/index.html", viewPath)
} else {
filePath = fmt.Sprintf("%s/%s", viewPath, req.URL.Path)
}
if logging {
log.Println(filePath)
}
http.ServeFile(w, req, filePath)
}
func BufferHandler(w http.ResponseWriter, req *http.Request) {
broadcastBufferJSON, err := json.Marshal(broadcastBuffer)
if err != nil {
log.Println("Buffer JSON Error: ", err)
return
}
fmt.Fprintf(w, string(broadcastBufferJSON))
}
func FlushHandler(w http.ResponseWriter, req *http.Request) {
broadcastBuffer = broadcastBuffer[:0]
fmt.Fprintf(w, "Flushed")
}
func wsServer(ws *websocket.Conn) {
s := &Socket{ws}
h.Connections[s] = true
s.ReceiveMessage()
}
var (
h Hub
viewPath string
port, bufferSize, delayMillis int
passThrough, logging bool
broadcastBuffer []*Broadcast
)
func init() {
flag.IntVar(&port, "port", 9193, "Port for the pipesock to sit on.")
flag.IntVar(&port, "p", 9193, "Port for the pipesock to sit on (shorthand).")
flag.BoolVar(&passThrough, "through", false, "Pass output to STDOUT.")
flag.BoolVar(&passThrough, "t", false, "Pass output to STDOUT (shorthand).")
flag.BoolVar(&logging, "log", false, "Log HTTP requetsts to STDOUT")
flag.BoolVar(&logging, "l", false, "Log HTTP requests tp STDOUT (shorthand).")
flag.StringVar(&viewPath, "view", "default", "View directory to serve.")
flag.StringVar(&viewPath, "v", "default", "View directory to serve (shorthand).")
flag.IntVar(&bufferSize, "num", 20, "Number of previous broadcasts to keep in memory.")
flag.IntVar(&bufferSize, "n", 20, "Number of previous broadcasts to keep in memory (shorthand).")
flag.IntVar(&delayMillis, "delay", 2000, "Delay between broadcasts of bundled events in ms.")
flag.IntVar(&delayMillis, "d", 2000, "Delay between broadcasts of bundled events in ms (shorthand).")
broadcastBuffer = make([]*Broadcast, 0)
// Set up hub
h.Connections = make(map[*Socket]bool)
h.Pipe = make(chan string, 1)
}
func main() {
flag.Parse()
pkgInfo, err := build.Import("github.com/minikomi/pipesock", "", 0)
if err != nil {
panic(err)
}
viewPath = filepath.Join(pkgInfo.Dir, "views", viewPath)
go h.BroadcastLoop()
go readLoop()
http.HandleFunc("/", IndexHandler)
http.HandleFunc("/buffer.json", BufferHandler)
http.HandleFunc("/flush", FlushHandler)
http.Handle("/ws", websocket.Handler(wsServer))
portString := fmt.Sprintf(":%d", port)
err = http.ListenAndServe(portString, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}