2024-03-19 00:31:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-09 15:44:43 +00:00
|
|
|
"fmt"
|
2024-03-19 00:31:35 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func simulate(cCtx *cli.Context) error {
|
|
|
|
ctx, cancel := context.WithCancel(cCtx.Context)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
sig := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-sig
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
rawLogger, err := zap.NewDevelopment()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing logger: %v", err)
|
|
|
|
}
|
|
|
|
logger = rawLogger.Sugar()
|
|
|
|
|
2024-04-09 15:44:43 +00:00
|
|
|
logger.Info("Running simulate command, flags passed:")
|
|
|
|
for _, flag := range SimulateFlags {
|
2024-03-19 00:31:35 +00:00
|
|
|
logger.Infof("-%s %v", flag.Names()[0], cCtx.Value(flag.Names()[0]))
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:44:43 +00:00
|
|
|
// Start messengers
|
|
|
|
apiModules := cCtx.String(APIModulesFlag)
|
|
|
|
|
|
|
|
alice, err := start(cCtx, "Alice", 0, apiModules)
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-09 15:44:43 +00:00
|
|
|
defer alice.stop()
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-04-09 15:44:43 +00:00
|
|
|
charlie, err := start(cCtx, "Charlie", 0, apiModules)
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-09 15:44:43 +00:00
|
|
|
defer charlie.stop()
|
2024-03-19 00:31:35 +00:00
|
|
|
|
|
|
|
// Retrieve for messages
|
|
|
|
msgCh := make(chan string)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
2024-04-09 15:44:43 +00:00
|
|
|
go alice.retrieveMessagesLoop(ctx, RetrieveInterval, nil, &wg)
|
2024-03-19 00:31:35 +00:00
|
|
|
wg.Add(1)
|
2024-04-09 15:44:43 +00:00
|
|
|
go charlie.retrieveMessagesLoop(ctx, RetrieveInterval, msgCh, &wg)
|
2024-03-19 00:31:35 +00:00
|
|
|
|
2024-04-09 15:44:43 +00:00
|
|
|
// Send contact request from Alice to Charlie, charlie accept the request
|
2024-03-19 00:31:35 +00:00
|
|
|
time.Sleep(WaitingInterval)
|
2024-04-09 15:44:43 +00:00
|
|
|
destID := charlie.messenger.GetSelfContact().ID
|
|
|
|
err = alice.sendContactRequest(cCtx, destID)
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msgID := <-msgCh
|
2024-04-09 15:44:43 +00:00
|
|
|
err = charlie.sendContactRequestAcceptance(cCtx, msgID)
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:44:43 +00:00
|
|
|
// Send DM between alice to charlie
|
2024-03-19 00:31:35 +00:00
|
|
|
interactive := cCtx.Bool(InteractiveFlag)
|
|
|
|
if interactive {
|
|
|
|
sem := make(chan struct{}, 1)
|
|
|
|
wg.Add(1)
|
2024-04-09 15:44:43 +00:00
|
|
|
go alice.sendMessageLoop(ctx, SendInterval, &wg, sem, cancel)
|
2024-03-19 00:31:35 +00:00
|
|
|
wg.Add(1)
|
2024-04-09 15:44:43 +00:00
|
|
|
go charlie.sendMessageLoop(ctx, SendInterval, &wg, sem, cancel)
|
2024-03-19 00:31:35 +00:00
|
|
|
} else {
|
|
|
|
time.Sleep(WaitingInterval)
|
|
|
|
for i := 0; i < cCtx.Int(CountFlag); i++ {
|
2024-04-09 15:44:43 +00:00
|
|
|
err = alice.sendDirectMessage(ctx, fmt.Sprintf("message from alice, number: %d", i+1))
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
time.Sleep(WaitingInterval)
|
|
|
|
|
2024-04-09 15:44:43 +00:00
|
|
|
err = charlie.sendDirectMessage(ctx, fmt.Sprintf("message from charlie, number: %d", i+1))
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
time.Sleep(WaitingInterval)
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
logger.Info("Exiting")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|