-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
69 lines (62 loc) · 1.44 KB
/
connection.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
package main
import (
"encoding/json"
"log"
"time"
)
//Connection presents an interface for finding, inserting, editing, and deleting Documents in a Collection.
type Connection struct {
id int64
opened int64
lastUsed int64
open bool
request chan request
response chan response
buffer [][]byte
}
//listen for responses, remove them from the channel and append them to a local buffer to be read when needed.
func (conn *Connection) listen() {
go func() {
for response := range conn.response {
for _, resp := range response.json {
bytes, err := json.Marshal(resp)
if err != nil {
log.Println(err)
}
conn.buffer = append(conn.buffer, bytes)
}
}
}()
}
//Dispatch an action type and json to the connection Collection.
func (conn *Connection) Dispatch(action string, json ...map[string]interface{}) {
r := request{
action,
time.Now().Unix(),
json,
conn.response,
}
conn.request <- r
}
//GetBytes returns the oldest unread response as an array of bytes.
func (conn *Connection) GetBytes() []byte {
if len(conn.buffer) > 0 {
bytes := make([]byte, len(conn.buffer[0]))
copy(bytes, conn.buffer[0])
conn.buffer = append(conn.buffer[:0], conn.buffer[0+1:]...)
return bytes
}
return nil
}
type request struct {
action string
time int64
json []map[string]interface{}
respond chan response
}
type response struct {
action string
time int64
json []map[string]interface{}
err error
}