Skip to content

Commit

Permalink
Improve client-side code and add PrettyLogger to api/logger.go, updat…
Browse files Browse the repository at this point in the history
…ing metrics.go and clientStatus.go.

This change fixed a bug by adding PrettyLogger to api/logger.go, which in turn improved code in metrics.go and clientStatus.go. The client code in client.go was also updated to generate dummy messages and add slog.Info logging. The index.html file was updated to sort messages based on the concatenation of From and To strings, as well as the message itself.
  • Loading branch information
HannahMarsh committed Jun 25, 2024
1 parent f5f7940 commit fe0d094
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
24 changes: 13 additions & 11 deletions cmd/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ func serveData(w http.ResponseWriter, r *http.Request) {

for _, client := range data.Clients {
for _, sent := range client.MessagesSent {
if _, present := m[sent.Message.Msg]; present {
mstr := sent.Message.From + sent.Message.To + sent.Message.Msg
if _, present := m[mstr]; present {
msg := Message{
From: sent.Message.From,
To: sent.Message.To,
Expand All @@ -136,9 +137,9 @@ func serveData(w http.ResponseWriter, r *http.Request) {
TimeSent: sent.TimeSent,
TimeReceived: m[sent.Message.Msg].TimeReceived,
}
m[sent.Message.Msg] = msg
m[mstr] = msg
} else {
m[sent.Message.Msg] = Message{
m[mstr] = Message{
From: sent.Message.From,
To: sent.Message.To,
RoutingPath: sent.RoutingPath,
Expand All @@ -149,24 +150,25 @@ func serveData(w http.ResponseWriter, r *http.Request) {
}
}
for _, received := range client.MessagesReceived {
if _, present := m[received.Message.Msg]; present {
mstr := received.Message.From + received.Message.To + received.Message.Msg
if _, present := m[mstr]; present {
msg := Message{
From: received.Message.From,
To: received.Message.To,
RoutingPath: m[received.Message.Msg].RoutingPath,
RoutingPath: m[mstr].RoutingPath,
Msg: received.Message.Msg,
TimeSent: m[received.Message.Msg].TimeSent,
TimeReceived: m[received.Message.Msg].TimeReceived,
TimeSent: m[mstr].TimeSent,
TimeReceived: received.TimeReceived,
}
m[received.Message.Msg] = msg
m[mstr] = msg
} else {
m[received.Message.Msg] = Message{
m[mstr] = Message{
From: received.Message.From,
To: received.Message.To,
RoutingPath: make([]api.PublicNodeApi, 0),
Msg: received.Message.Msg,
TimeSent: m[received.Message.Msg].TimeSent,
TimeReceived: time.Time{},
TimeSent: time.Time{},
TimeReceived: received.TimeReceived,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions internal/api/clientStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"encoding/json"
"github.com/HannahMarsh/PrettyLogger"
"golang.org/x/exp/slog"
"sync"
"time"
Expand Down Expand Up @@ -34,6 +35,8 @@ func (cs *ClientStatus) AddSent(clientReceiver PublicNodeApi, routingPath []Publ
Message: message,
TimeSent: time.Now(),
})

slog.Info(PrettyLogger.GetFuncName(), "message", message)
}

func (cs *ClientStatus) AddReceived(message Message) {
Expand All @@ -43,6 +46,7 @@ func (cs *ClientStatus) AddReceived(message Message) {
Message: message,
TimeReceived: time.Now(),
})
slog.Info(PrettyLogger.GetFuncName(), "message", message)
}

func (cs *ClientStatus) GetStatus() string {
Expand Down
38 changes: 19 additions & 19 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,39 +95,36 @@ func (c *Client) RegisterWithBulletinBoard() error {

func (c *Client) StartGeneratingMessages(client_addresses []string) {
slog.Info("Client starting to generate messages", "id", c.ID)
var msgNum int = 0
msgNum := 0
var mu sync.Mutex
for {
select {
case <-config.GlobalCtx.Done():
slog.Info(pl.GetFuncName()+": ctx.done -> Client stopping to generate messages", "id", c.ID)
return // Exit if context is cancelled
default:
mu.Lock()
messages := make([]api.Message, 0)
for _, addr := range client_addresses {
if addr != c.Adddress {
if addr != c.Adddress && addr != "" {
messages = append(messages, api.Message{
From: c.Adddress,
To: addr,
Msg: fmt.Sprintf("Msg#%d from client(id=%d)", msgNum, c.ID),
})
msgNum++
msgNum = msgNum + 1
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
c.mu.Lock()
defer func() {
c.mu.Unlock()
wg.Done()
}()
if err := c.RegisterIntentToSend(messages); err != nil {
slog.Error(pl.GetFuncName()+": Error registering intent to send", err)
} else {
c.Messages = append(c.Messages, messages...)
}
}()
wg.Wait()
mu.Unlock()

c.mu.Lock()
messages = append(c.Messages, messages...)
if err := c.RegisterIntentToSend(messages); err != nil {
slog.Error(pl.GetFuncName()+": Error registering intent to send", err)
} else {
c.Messages = messages
}
c.mu.Unlock()
}
time.Sleep(5 * time.Second)
}
Expand Down Expand Up @@ -197,15 +194,18 @@ func (c *Client) formOnions(start api.StartRunApi) (map[string][]api.OnionApi, e
numMessagesToSend[msg.To]++
}

dummyNum := 0

for addr, numMessages := range numMessagesToSend {
if numMessages < start.NumMessagesPerClient {
numDummyNeeded := start.NumMessagesPerClient - numMessages
for i := 0; i < numDummyNeeded; i++ {
c.Messages = append(c.Messages, api.Message{
From: c.Adddress,
To: addr,
Msg: "dummy",
Msg: fmt.Sprintf("dummy%d", dummyNum),
})
dummyNum++
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ <h1>Pi_t</h1>

// Sort messages by the concatenation of From and To strings
data.Messages.sort((a, b) => {
const aConcat = a.From + a.To;
const bConcat = b.From + b.To;
const aConcat = a.From + a.To + a.Msg;
const bConcat = b.From + b.To + b.Msg;
return aConcat.localeCompare(bConcat);
});

Expand Down Expand Up @@ -129,7 +129,7 @@ <h1>Pi_t</h1>

function startFetchingData() {
fetchData();
setInterval(fetchData, 500); // Update every half second
setInterval(fetchData, 1000); // Update every half second
}

document.addEventListener('DOMContentLoaded', startFetchingData);
Expand Down

0 comments on commit fe0d094

Please sign in to comment.