-
Notifications
You must be signed in to change notification settings - Fork 0
/
pxls.go
186 lines (146 loc) · 3.33 KB
/
pxls.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
package main
import (
"fmt"
"github.com/nsf/termbox-go"
"encoding/json"
"io/ioutil"
"net/http"
"golang.org/x/net/websocket"
"github.com/lucasb-eyer/go-colorful"
)
// PxlsBoard represents a [width][height]color_index pxls board.
type PxlsBoard [][]uint8
// Width returns the length of the PxlsBoard.
func (b *PxlsBoard) Width() int {
return len(*b)
}
// Height returns the length of the first element of the PxlsBoard, or 0 if there is no first element.
func (b *PxlsBoard) Height() int {
if len(*b) > 0 {
return len((*b)[0])
}
return 0
}
// Pxls holds data on the current state of the board, and also handles the websocket connection.
type Pxls struct {
init bool
conn *websocket.Conn
Host string
SecureConn bool
WsMsgCh chan pxlsMsg
ErrCh chan error
Board PxlsBoard
Palette []termbox.Attribute
}
// NewPxls creates a new Pxls instance.
func NewPxls(host string, secure bool) *Pxls {
return &Pxls{
Host: host,
SecureConn: secure,
WsMsgCh: make(chan pxlsMsg),
ErrCh: make(chan error),
}
}
// IsInit returns whether a connection with the Pxls' servers has been made.
func (pxls *Pxls) IsInit() bool {
return pxls.init
}
// Init retrieves board data and info and establishes a connection with Pxls' websockets.
func (pxls *Pxls) Init() error {
if pxls.init {
return nil
}
var apiURLTemplate = "http://%s"
var wsURLTemplate = "ws://%s/ws"
if pxls.SecureConn {
apiURLTemplate = "https://%s"
wsURLTemplate = "wss://%s/ws"
}
var apiURL = fmt.Sprintf(apiURLTemplate, pxls.Host)
var wsURL = fmt.Sprintf(wsURLTemplate, pxls.Host)
var err error
resp, err := http.Get(apiURL + "/info")
if err != nil {
return err
}
defer resp.Body.Close()
infoBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var info pxlsInfo
err = json.Unmarshal(infoBytes, &info)
if err != nil {
return err
}
pxls.Palette = pxls.parsePalette(info.Palette)
resp, err = http.Get(apiURL + "/boarddata")
if err != nil {
return err
}
defer resp.Body.Close()
bd, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
pxls.Board = pxls.makeBoard(info.Width, info.Height, bd)
conn, err := websocket.Dial(wsURL, "", "http://localhost")
if err != nil {
return err
}
go readPxlsWsLoop(conn, pxls.WsMsgCh, pxls.ErrCh)
pxls.conn = conn
pxls.init = true
return nil
}
func (pxls *Pxls) makeBoard(w, h int, data []uint8) (board PxlsBoard) {
board = make(PxlsBoard, w)
for x := range board {
board[x] = make([]uint8, h)
for y := range board[x] {
board[x][y] = data[x + y * w]
}
}
return board
}
func (pxls *Pxls) parsePalette(ip []string) (palette []termbox.Attribute) {
for _, hex := range ip {
c, err := colorful.Hex(hex)
if err != nil {
panic(err)
}
palette = append(palette, colorToTermboxAttr(c))
}
return
}
// Close closes the websocket connection.
func (pxls *Pxls) Close() error {
var err error
if err = pxls.conn.Close(); err != nil {
return err
}
return nil
}
type pxlsInfo struct {
Width, Height int
Palette []string
}
type pxlsMsg struct {
Type string
Count int
Pixels []pxlsPixel
}
type pxlsPixel struct {
X, Y int
Color uint8
}
func readPxlsWsLoop(conn *websocket.Conn, msgCh chan pxlsMsg, errCh chan error) {
for {
var m pxlsMsg
err := websocket.JSON.Receive(conn, &m)
if err != nil {
errCh <- err
}
msgCh <- m
}
}