Skip to content

Commit

Permalink
Buffer displayed packets update
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessOne91 committed Jul 27, 2024
1 parent edc9308 commit 8d15437
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
57 changes: 37 additions & 20 deletions tui/models/bisturi_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"strconv"
"strings"
"time"

"github.com/NamelessOne91/bisturi/sockets"
"github.com/NamelessOne91/bisturi/tui/styles"
Expand All @@ -26,7 +27,7 @@ const (

type errMsg error

type packetMsg sockets.NetworkPacket
type readPacketsMsg []sockets.NetworkPacket

type bisturiModel struct {
terminalHeight int
Expand All @@ -41,6 +42,7 @@ type bisturiModel struct {
selectedEthType uint16
rawSocket *sockets.RawSocket
packetsChan chan sockets.NetworkPacket
msgChan chan tea.Msg
errChan chan error
err error
}
Expand All @@ -60,8 +62,11 @@ func NewBisturiModel() *bisturiModel {
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#00cc99"))

return &bisturiModel{
step: retrieveIfaces,
spinner: s,
step: retrieveIfaces,
spinner: s,
packetsChan: make(chan sockets.NetworkPacket),
msgChan: make(chan tea.Msg),
errChan: make(chan error),
}
}

Expand Down Expand Up @@ -213,16 +218,12 @@ func (m *bisturiModel) updateRowsInput(msg tea.Msg) (tea.Model, tea.Cmd) {
maxRows, err := strconv.Atoi(m.rowsInput.Value())
if err == nil && maxRows > 0 {
m.packetsTable = newPacketsTable(maxRows, m.terminalWidth)

m.packetsChan = make(chan sockets.NetworkPacket)
m.errChan = make(chan error)

m.step = receivePackets

return m, tea.Batch(func() tea.Msg {
go m.rawSocket.ReadToChan(m.packetsChan, m.errChan)
return nil
}, m.waitForPacket)
go m.rawSocket.ReadToChan(m.packetsChan, m.errChan)
go m.readPackets()

return m, m.pollPacketsMessages()
}
}
}
Expand All @@ -242,18 +243,34 @@ func (m *bisturiModel) updateReceivingPacket(msg tea.Msg) (tea.Model, tea.Cmd) {
m.packetsTable.resizeTable(m.terminalWidth)

return m, nil

case sockets.NetworkPacket:
return m, m.waitForPacket
case readPacketsMsg:
return m, m.pollPacketsMessages()
}
return m, cmd
}

func (m bisturiModel) waitForPacket() tea.Msg {
select {
case packet := <-m.packetsChan:
return packetMsg(packet)
case err := <-m.errChan:
return errMsg(err)
func (m bisturiModel) readPackets() {
readPackets := []sockets.NetworkPacket{}
timer := time.NewTicker(3 * time.Second)
defer timer.Stop()

for {
select {
case packet := <-m.packetsChan:
readPackets = append(readPackets, packet)
case <-timer.C:
if len(readPackets) > 0 {
m.msgChan <- readPacketsMsg(readPackets)
readPackets = []sockets.NetworkPacket{}
}
case err := <-m.errChan:
m.msgChan <- errMsg(err)
}
}
}

func (m bisturiModel) pollPacketsMessages() tea.Cmd {
return func() tea.Msg {
return <-m.msgChan
}
}
32 changes: 17 additions & 15 deletions tui/models/packets_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (m packetsTablemodel) Init() tea.Cmd {

func (m packetsTablemodel) Update(msg tea.Msg) (packetsTablemodel, tea.Cmd) {
switch msg := msg.(type) {
case packetMsg:
m.addRow(msg)
case readPacketsMsg:
m.addRows(msg)
return m, nil

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

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

newRow := table.NewRow(table.RowData{
columnKeyID: m.counter,
columnKeyDate: time.Now().Local().Format(time.Stamp),
columnKeySource: data.Source(),
columnKeyDestination: data.Destination(),
columnKeyInfo: data.Info(),
})
m.cachedRows = append(m.cachedRows, newRow)
newRow := table.NewRow(table.RowData{
columnKeyID: m.counter,
columnKeyDate: time.Now().Local().Format(time.Stamp),
columnKeySource: np.Source(),
columnKeyDestination: np.Destination(),
columnKeyInfo: np.Info(),
})
m.cachedRows = append(m.cachedRows, newRow)
}
m.table = m.table.WithRows(m.cachedRows)
}

0 comments on commit 8d15437

Please sign in to comment.