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)
|
2024-05-22 22:01:54 +00:00
|
|
|
telemetryUrl := cCtx.String(TelemetryServerURLFlag)
|
2024-04-09 15:44:43 +00:00
|
|
|
|
2024-06-12 08:00:51 +00:00
|
|
|
alice, err := start("Alice", 0, apiModules, telemetryUrl, false, "")
|
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-06-12 08:00:51 +00:00
|
|
|
charlie, err := start("Charlie", 0, apiModules, telemetryUrl, false, "")
|
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
|
2024-06-10 07:10:09 +00:00
|
|
|
err = alice.sendContactRequest(ctx, destID)
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msgID := <-msgCh
|
2024-06-10 07:10:09 +00:00
|
|
|
err = charlie.sendContactRequestAcceptance(ctx, msgID)
|
2024-03-19 00:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-10 07:10:09 +00:00
|
|
|
time.Sleep(WaitingInterval)
|
2024-03-19 00:31:35 +00:00
|
|
|
|
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 {
|
2024-06-10 07:10:09 +00:00
|
|
|
interactiveSendMessageLoop(ctx, alice, charlie)
|
2024-03-19 00:31:35 +00:00
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|