Skip to content

Commit 910bac2

Browse files
committedMar 31, 2025·
refactor: rename 'Hub' and 'Serialhub' types to 'hub' and 'serialhub' for consistency
1 parent 5e2161e commit 910bac2

File tree

7 files changed

+45
-51
lines changed

7 files changed

+45
-51
lines changed
 

‎conn.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type Upload struct {
8080

8181
var uploadStatusStr = "ProgrammerStatus"
8282

83-
func uploadHandler(h *Hub, pubKey *rsa.PublicKey) func(*gin.Context) {
83+
func uploadHandler(h *hub, pubKey *rsa.PublicKey) func(*gin.Context) {
8484
return func(c *gin.Context) {
8585
data := new(Upload)
8686
if err := c.BindJSON(data); err != nil {
@@ -193,7 +193,7 @@ func uploadHandler(h *Hub, pubKey *rsa.PublicKey) func(*gin.Context) {
193193
// PLogger sends the info from the upload to the websocket
194194
type PLogger struct {
195195
Verbose bool
196-
h *Hub
196+
h *hub
197197
}
198198

199199
// Debug only sends messages if verbose is true (always true for now)
@@ -210,12 +210,12 @@ func (l PLogger) Info(args ...interface{}) {
210210
send(l.h, map[string]string{uploadStatusStr: "Busy", "Msg": output})
211211
}
212212

213-
func send(h *Hub, args map[string]string) {
213+
func send(h *hub, args map[string]string) {
214214
mapB, _ := json.Marshal(args)
215215
h.broadcastSys <- mapB
216216
}
217217

218-
func wsHandler(h *Hub) *WsServer {
218+
func wsHandler(h *hub) *WsServer {
219219
server, err := socketio.NewServer(nil)
220220
if err != nil {
221221
log.Fatal(err)

‎hub.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
log "github.com/sirupsen/logrus"
3030
)
3131

32-
type Hub struct {
32+
type hub struct {
3333
// Registered connections.
3434
connections map[*connection]bool
3535

@@ -47,15 +47,13 @@ type Hub struct {
4747

4848
//TODO globals clients
4949
// Serial hub to communicate with serial ports
50-
serialHub *Serialhub
50+
serialHub *serialhub
5151

52-
serialPortList *SerialPortList
52+
serialPortList *serialPortList
5353
}
5454

55-
// NewHub creates a hub that acts as a central hub for handling
56-
// WebSocket connections, broadcasting messages, and processing commands.
57-
func NewHub(serialhub *Serialhub, serialList *SerialPortList) *Hub {
58-
hub := &Hub{
55+
func newHub(serialhub *serialhub, serialList *serialPortList) *hub {
56+
hub := &hub{
5957
broadcast: make(chan []byte, 1000),
6058
broadcastSys: make(chan []byte, 1000),
6159
register: make(chan *connection),
@@ -102,15 +100,15 @@ const commands = `{
102100
]
103101
}`
104102

105-
func (h *Hub) unregisterConnection(c *connection) {
103+
func (h *hub) unregisterConnection(c *connection) {
106104
if _, contains := h.connections[c]; !contains {
107105
return
108106
}
109107
delete(h.connections, c)
110108
close(c.send)
111109
}
112110

113-
func (h *Hub) sendToRegisteredConnections(data []byte) {
111+
func (h *hub) sendToRegisteredConnections(data []byte) {
114112
for c := range h.connections {
115113
select {
116114
case c.send <- data:
@@ -123,7 +121,7 @@ func (h *Hub) sendToRegisteredConnections(data []byte) {
123121
}
124122
}
125123

126-
func (h *Hub) run() {
124+
func (h *hub) run() {
127125
for {
128126
select {
129127
case c := <-h.register:
@@ -146,7 +144,7 @@ func (h *Hub) run() {
146144
}
147145
}
148146

149-
func (h *Hub) checkCmd(m []byte) {
147+
func (h *hub) checkCmd(m []byte) {
150148
//log.Print("Inside checkCmd")
151149
s := string(m[:])
152150

@@ -284,23 +282,23 @@ func logAction(sl string) {
284282
}
285283
}
286284

287-
func (h *Hub) memoryStats() {
285+
func (h *hub) memoryStats() {
288286
var memStats runtime.MemStats
289287
runtime.ReadMemStats(&memStats)
290288
json, _ := json.Marshal(memStats)
291289
log.Printf("memStats:%v\n", string(json))
292290
h.broadcastSys <- json
293291
}
294292

295-
func (h *Hub) getHostname() {
293+
func (h *hub) getHostname() {
296294
h.broadcastSys <- []byte("{\"Hostname\" : \"" + *hostname + "\"}")
297295
}
298296

299-
func (h *Hub) getVersion() {
297+
func (h *hub) getVersion() {
300298
h.broadcastSys <- []byte("{\"Version\" : \"" + version + "\"}")
301299
}
302300

303-
func (h *Hub) garbageCollection() {
301+
func (h *hub) garbageCollection() {
304302
log.Printf("Starting garbageCollection()\n")
305303
h.broadcastSys <- []byte("{\"gc\":\"starting\"}")
306304
h.memoryStats()

‎info.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func infoHandler(c *gin.Context) {
4141
})
4242
}
4343

44-
func pauseHandler(h *Hub, s *systray.Systray) func(c *gin.Context) {
44+
func pauseHandler(h *hub, s *systray.Systray) func(c *gin.Context) {
4545
return func(c *gin.Context) {
4646
go func() {
4747
ports, _ := serial.GetPortsList()

‎main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ func loop(stray *systray.Systray, configPath *paths.Path) {
191191
}
192192

193193
// serialPorts contains the ports attached to the machine
194-
serialPorts := NewSerialPortList()
195-
serialHub := NewSerialHub()
194+
serialPorts := newSerialPortList()
195+
serialHub := newSerialHub()
196196

197197
// var loggerWs logWriter
198198

199-
hub := NewHub(serialHub, serialPorts)
199+
hub := newHub(serialHub, serialPorts)
200200

201201
logger := func(msg string) {
202202
mapD := map[string]string{"DownloadStatus": "Pending", "Msg": msg}

‎main_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestValidSignatureKey(t *testing.T) {
5656

5757
func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
5858
r := gin.New()
59-
r.POST("/", uploadHandler(NewHub(NewSerialHub(), NewSerialPortList()), utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey))))
59+
r.POST("/", uploadHandler(newHub(newSerialHub(), newSerialPortList()), utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey))))
6060
ts := httptest.NewServer(r)
6161

6262
uploadEvilFileName := Upload{
@@ -93,7 +93,7 @@ func TestUploadHandlerAgainstEvilFileNames(t *testing.T) {
9393
func TestUploadHandlerAgainstBase64WithoutPaddingMustFail(t *testing.T) {
9494
r := gin.New()
9595

96-
r.POST("/", uploadHandler(NewHub(NewSerialHub(), NewSerialPortList()), utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey))))
96+
r.POST("/", uploadHandler(newHub(newSerialHub(), newSerialPortList()), utilities.MustParseRsaPublicKey([]byte(globals.ArduinoSignaturePubKey))))
9797
ts := httptest.NewServer(r)
9898
defer ts.Close()
9999

‎serial.go

+21-25
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/sirupsen/logrus"
3030
)
3131

32-
type Serialhub struct {
32+
type serialhub struct {
3333
// Opened serial ports.
3434
ports map[*serport]bool
3535
mu sync.Mutex
@@ -38,26 +38,22 @@ type Serialhub struct {
3838
OnUnregister func(port *serport)
3939
}
4040

41-
// NewSerialHub creates a new serial hub
42-
// It is used to manage serial ports and their connections.
43-
func NewSerialHub() *Serialhub {
44-
return &Serialhub{
41+
func newSerialHub() *serialhub {
42+
return &serialhub{
4543
ports: make(map[*serport]bool),
4644
}
4745
}
4846

49-
// SerialPortList is the serial port list
50-
type SerialPortList struct {
47+
type serialPortList struct {
5148
Ports []*SpPortItem
5249
portsLock sync.Mutex
5350

5451
OnList func([]byte) `json:"-"`
5552
OnErr func(string) `json:"-"`
5653
}
5754

58-
// NewSerialPortList creates a new serial port list
59-
func NewSerialPortList() *SerialPortList {
60-
return &SerialPortList{}
55+
func newSerialPortList() *serialPortList {
56+
return &serialPortList{}
6157
}
6258

6359
// SpPortItem is the serial port item
@@ -75,7 +71,7 @@ type SpPortItem struct {
7571
}
7672

7773
// Register serial ports from the connections.
78-
func (sh *Serialhub) Register(port *serport) {
74+
func (sh *serialhub) Register(port *serport) {
7975
sh.mu.Lock()
8076
//log.Print("Registering a port: ", p.portConf.Name)
8177
sh.OnRegister(port)
@@ -84,7 +80,7 @@ func (sh *Serialhub) Register(port *serport) {
8480
}
8581

8682
// Unregister requests from connections.
87-
func (sh *Serialhub) Unregister(port *serport) {
83+
func (sh *serialhub) Unregister(port *serport) {
8884
sh.mu.Lock()
8985
//log.Print("Unregistering a port: ", p.portConf.Name)
9086
sh.OnUnregister(port)
@@ -94,7 +90,7 @@ func (sh *Serialhub) Unregister(port *serport) {
9490
sh.mu.Unlock()
9591
}
9692

97-
func (sh *Serialhub) FindPortByName(portname string) (*serport, bool) {
93+
func (sh *serialhub) FindPortByName(portname string) (*serport, bool) {
9894
sh.mu.Lock()
9995
defer sh.mu.Unlock()
10096

@@ -109,7 +105,7 @@ func (sh *Serialhub) FindPortByName(portname string) (*serport, bool) {
109105
}
110106

111107
// List broadcasts a Json representation of the ports found
112-
func (sp *SerialPortList) List() {
108+
func (sp *serialPortList) List() {
113109
sp.portsLock.Lock()
114110
ls, err := json.MarshalIndent(sp, "", "\t")
115111
sp.portsLock.Unlock()
@@ -122,7 +118,7 @@ func (sp *SerialPortList) List() {
122118
}
123119

124120
// Run is the main loop for port discovery and management
125-
func (sp *SerialPortList) Run() {
121+
func (sp *serialPortList) Run() {
126122
for retries := 0; retries < 10; retries++ {
127123
sp.runSerialDiscovery()
128124

@@ -132,7 +128,7 @@ func (sp *SerialPortList) Run() {
132128
logrus.Errorf("Failed restarting serial discovery. Giving up...")
133129
}
134130

135-
func (sp *SerialPortList) runSerialDiscovery() {
131+
func (sp *serialPortList) runSerialDiscovery() {
136132
// First ensure that all the discoveries are available
137133
if err := Tools.Download("builtin", "serial-discovery", "latest", "keep"); err != nil {
138134
logrus.Errorf("Error downloading serial-discovery: %s", err)
@@ -176,13 +172,13 @@ func (sp *SerialPortList) runSerialDiscovery() {
176172
logrus.Errorf("Serial discovery stopped.")
177173
}
178174

179-
func (sp *SerialPortList) reset() {
175+
func (sp *serialPortList) reset() {
180176
sp.portsLock.Lock()
181177
defer sp.portsLock.Unlock()
182178
sp.Ports = []*SpPortItem{}
183179
}
184180

185-
func (sp *SerialPortList) add(addedPort *discovery.Port) {
181+
func (sp *serialPortList) add(addedPort *discovery.Port) {
186182
if addedPort.Protocol != "serial" {
187183
return
188184
}
@@ -226,7 +222,7 @@ func (sp *SerialPortList) add(addedPort *discovery.Port) {
226222
})
227223
}
228224

229-
func (sp *SerialPortList) remove(removedPort *discovery.Port) {
225+
func (sp *serialPortList) remove(removedPort *discovery.Port) {
230226
sp.portsLock.Lock()
231227
defer sp.portsLock.Unlock()
232228

@@ -237,7 +233,7 @@ func (sp *SerialPortList) remove(removedPort *discovery.Port) {
237233
}
238234

239235
// MarkPortAsOpened marks a port as opened by the user
240-
func (sp *SerialPortList) MarkPortAsOpened(portname string) {
236+
func (sp *serialPortList) MarkPortAsOpened(portname string) {
241237
sp.portsLock.Lock()
242238
defer sp.portsLock.Unlock()
243239
port := sp.getPortByName(portname)
@@ -247,7 +243,7 @@ func (sp *SerialPortList) MarkPortAsOpened(portname string) {
247243
}
248244

249245
// MarkPortAsClosed marks a port as no more opened by the user
250-
func (sp *SerialPortList) MarkPortAsClosed(portname string) {
246+
func (sp *serialPortList) MarkPortAsClosed(portname string) {
251247
sp.portsLock.Lock()
252248
defer sp.portsLock.Unlock()
253249
port := sp.getPortByName(portname)
@@ -256,7 +252,7 @@ func (sp *SerialPortList) MarkPortAsClosed(portname string) {
256252
}
257253
}
258254

259-
func (sp *SerialPortList) getPortByName(portname string) *SpPortItem {
255+
func (sp *serialPortList) getPortByName(portname string) *SpPortItem {
260256
for _, port := range sp.Ports {
261257
if port.Name == portname {
262258
return port
@@ -265,13 +261,13 @@ func (sp *SerialPortList) getPortByName(portname string) *SpPortItem {
265261
return nil
266262
}
267263

268-
func (h *Hub) spErr(err string) {
264+
func (h *hub) spErr(err string) {
269265
//log.Println("Sending err back: ", err)
270266
//sh.hub.broadcastSys <- []byte(err)
271267
h.broadcastSys <- []byte("{\"Error\" : \"" + err + "\"}")
272268
}
273269

274-
func (h *Hub) spClose(portname string) {
270+
func (h *hub) spClose(portname string) {
275271
if myport, ok := h.serialHub.FindPortByName(portname); ok {
276272
h.broadcastSys <- []byte("Closing serial port " + portname)
277273
myport.Close()
@@ -280,7 +276,7 @@ func (h *Hub) spClose(portname string) {
280276
}
281277
}
282278

283-
func (h *Hub) spWrite(arg string) {
279+
func (h *hub) spWrite(arg string) {
284280
// we will get a string of comXX asdf asdf asdf
285281
//log.Println("Inside spWrite arg: " + arg)
286282
arg = strings.TrimPrefix(arg, " ")

‎serialport.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (p *serport) writerRaw() {
287287
p.OnMessage([]byte(msgstr))
288288
}
289289

290-
func (h *Hub) spHandlerOpen(portname string, baud int, buftype string) {
290+
func (h *hub) spHandlerOpen(portname string, baud int, buftype string) {
291291

292292
log.Print("Inside spHandler")
293293

0 commit comments

Comments
 (0)
Please sign in to comment.