Skip to content

Commit 8d15437

Browse files
committed
Buffer displayed packets update
1 parent edc9308 commit 8d15437

File tree

2 files changed

+54
-35
lines changed

2 files changed

+54
-35
lines changed

tui/models/bisturi_model.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net"
66
"strconv"
77
"strings"
8+
"time"
89

910
"github.com/NamelessOne91/bisturi/sockets"
1011
"github.com/NamelessOne91/bisturi/tui/styles"
@@ -26,7 +27,7 @@ const (
2627

2728
type errMsg error
2829

29-
type packetMsg sockets.NetworkPacket
30+
type readPacketsMsg []sockets.NetworkPacket
3031

3132
type bisturiModel struct {
3233
terminalHeight int
@@ -41,6 +42,7 @@ type bisturiModel struct {
4142
selectedEthType uint16
4243
rawSocket *sockets.RawSocket
4344
packetsChan chan sockets.NetworkPacket
45+
msgChan chan tea.Msg
4446
errChan chan error
4547
err error
4648
}
@@ -60,8 +62,11 @@ func NewBisturiModel() *bisturiModel {
6062
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#00cc99"))
6163

6264
return &bisturiModel{
63-
step: retrieveIfaces,
64-
spinner: s,
65+
step: retrieveIfaces,
66+
spinner: s,
67+
packetsChan: make(chan sockets.NetworkPacket),
68+
msgChan: make(chan tea.Msg),
69+
errChan: make(chan error),
6570
}
6671
}
6772

@@ -213,16 +218,12 @@ func (m *bisturiModel) updateRowsInput(msg tea.Msg) (tea.Model, tea.Cmd) {
213218
maxRows, err := strconv.Atoi(m.rowsInput.Value())
214219
if err == nil && maxRows > 0 {
215220
m.packetsTable = newPacketsTable(maxRows, m.terminalWidth)
216-
217-
m.packetsChan = make(chan sockets.NetworkPacket)
218-
m.errChan = make(chan error)
219-
220221
m.step = receivePackets
221222

222-
return m, tea.Batch(func() tea.Msg {
223-
go m.rawSocket.ReadToChan(m.packetsChan, m.errChan)
224-
return nil
225-
}, m.waitForPacket)
223+
go m.rawSocket.ReadToChan(m.packetsChan, m.errChan)
224+
go m.readPackets()
225+
226+
return m, m.pollPacketsMessages()
226227
}
227228
}
228229
}
@@ -242,18 +243,34 @@ func (m *bisturiModel) updateReceivingPacket(msg tea.Msg) (tea.Model, tea.Cmd) {
242243
m.packetsTable.resizeTable(m.terminalWidth)
243244

244245
return m, nil
245-
246-
case sockets.NetworkPacket:
247-
return m, m.waitForPacket
246+
case readPacketsMsg:
247+
return m, m.pollPacketsMessages()
248248
}
249249
return m, cmd
250250
}
251251

252-
func (m bisturiModel) waitForPacket() tea.Msg {
253-
select {
254-
case packet := <-m.packetsChan:
255-
return packetMsg(packet)
256-
case err := <-m.errChan:
257-
return errMsg(err)
252+
func (m bisturiModel) readPackets() {
253+
readPackets := []sockets.NetworkPacket{}
254+
timer := time.NewTicker(3 * time.Second)
255+
defer timer.Stop()
256+
257+
for {
258+
select {
259+
case packet := <-m.packetsChan:
260+
readPackets = append(readPackets, packet)
261+
case <-timer.C:
262+
if len(readPackets) > 0 {
263+
m.msgChan <- readPacketsMsg(readPackets)
264+
readPackets = []sockets.NetworkPacket{}
265+
}
266+
case err := <-m.errChan:
267+
m.msgChan <- errMsg(err)
268+
}
269+
}
270+
}
271+
272+
func (m bisturiModel) pollPacketsMessages() tea.Cmd {
273+
return func() tea.Msg {
274+
return <-m.msgChan
258275
}
259276
}

tui/models/packets_table.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func (m packetsTablemodel) Init() tea.Cmd {
6161

6262
func (m packetsTablemodel) Update(msg tea.Msg) (packetsTablemodel, tea.Cmd) {
6363
switch msg := msg.(type) {
64-
case packetMsg:
65-
m.addRow(msg)
64+
case readPacketsMsg:
65+
m.addRows(msg)
6666
return m, nil
6767

6868
case tea.KeyMsg:
@@ -81,19 +81,21 @@ func (m packetsTablemodel) View() string {
8181
return fmt.Sprintf("Displaying up to the last %d rows\n\n%s", m.maxRows, m.table.View())
8282
}
8383

84-
func (m *packetsTablemodel) addRow(data sockets.NetworkPacket) {
85-
if len(m.cachedRows) >= m.maxRows {
86-
m.cachedRows = m.cachedRows[1:]
87-
}
88-
m.counter += 1
84+
func (m *packetsTablemodel) addRows(packets []sockets.NetworkPacket) {
85+
for _, np := range packets {
86+
if len(m.cachedRows) >= m.maxRows {
87+
m.cachedRows = m.cachedRows[1:]
88+
}
89+
m.counter += 1
8990

90-
newRow := table.NewRow(table.RowData{
91-
columnKeyID: m.counter,
92-
columnKeyDate: time.Now().Local().Format(time.Stamp),
93-
columnKeySource: data.Source(),
94-
columnKeyDestination: data.Destination(),
95-
columnKeyInfo: data.Info(),
96-
})
97-
m.cachedRows = append(m.cachedRows, newRow)
91+
newRow := table.NewRow(table.RowData{
92+
columnKeyID: m.counter,
93+
columnKeyDate: time.Now().Local().Format(time.Stamp),
94+
columnKeySource: np.Source(),
95+
columnKeyDestination: np.Destination(),
96+
columnKeyInfo: np.Info(),
97+
})
98+
m.cachedRows = append(m.cachedRows, newRow)
99+
}
98100
m.table = m.table.WithRows(m.cachedRows)
99101
}

0 commit comments

Comments
 (0)