cashless2ecash

cashless2ecash: pay with cards for digital cash (experimental)
Log | Files | Refs | README

main.go (3278B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"os"
      6 
      7 	"gopkg.in/yaml.v3"
      8 )
      9 
     10 var CONFIG SimulationConfig
     11 
     12 type SimulatedPhysicalInteraction struct {
     13 	Msg string
     14 }
     15 
     16 func main() {
     17 
     18 	p := "./config.yaml"
     19 	if len(os.Args) > 1 && os.Args[1] != "" {
     20 		p = os.Args[1]
     21 	}
     22 
     23 	cfg, err := parseSimulationConfig(p)
     24 	if err != nil {
     25 		fmt.Println(err.Error())
     26 		return
     27 	}
     28 	CONFIG = *cfg
     29 	TERMINAL_USER_ID = "Simulation-" + CONFIG.TerminalId
     30 
     31 	if !c2ecAlive() {
     32 		fmt.Println("start c2ec first.")
     33 		return
     34 	}
     35 
     36 	kill := make(chan error)
     37 	finish := make(chan interface{})
     38 	// start simulated wire watch
     39 	go WireWatch(finish, kill)
     40 
     41 	go func() {
     42 		for i := 0; i < CONFIG.ParallelWithdrawals; i++ {
     43 
     44 			fmt.Println("Starting withdrawal", i)
     45 
     46 			toTerminal := make(chan *SimulatedPhysicalInteraction, 10)
     47 			toWallet := make(chan *SimulatedPhysicalInteraction, 10)
     48 
     49 			// start simulated terminal
     50 			go Terminal(toTerminal, toWallet, kill)
     51 
     52 			// start simulated wallet
     53 			go Wallet(toWallet, toTerminal, kill)
     54 
     55 		}
     56 	}()
     57 
     58 	for err := range kill {
     59 		if err == nil {
     60 			fmt.Print("simulation successful.")
     61 			os.Exit(0)
     62 		}
     63 		fmt.Println("simulation error: ", err.Error())
     64 		os.Exit(1)
     65 	}
     66 
     67 	<-finish
     68 	fmt.Println("simulation ended")
     69 }
     70 
     71 func c2ecAlive() bool {
     72 
     73 	cfg, status, err := HttpGet(
     74 		CONFIG.C2ecBaseUrl+"/config",
     75 		map[string]string{
     76 			"Authorization": TerminalAuth(),
     77 		},
     78 		NewJsonCodec[TerminalConfig]())
     79 	if err != nil || status != 200 {
     80 		fmt.Println("Error from c2ec:", err)
     81 		fmt.Println("Status from c2ec:", status)
     82 		return false
     83 	}
     84 
     85 	fmt.Println("ALIVE   :", cfg.Name, cfg.Version, cfg.ProviderName, cfg.WireType)
     86 	return true
     87 }
     88 
     89 type SimulationConfig struct {
     90 	DisableDelays       bool   `yaml:"disable-delays"`
     91 	ParallelWithdrawals int    `yaml:"parallel-withdrawals"`
     92 	C2ecBaseUrl         string `yaml:"c2ec-base-url"`
     93 	// simulates the terminal talking to its backend system and executing the payment.
     94 	ProviderBackendPaymentDelay int `yaml:"provider-backend-payment-delay"`
     95 	// simulates the user presenting his card to the terminal
     96 	Amount                  string `yaml:"amount"`
     97 	Fees                    string `yaml:"fees"`
     98 	TerminalAcceptCardDelay int    `yaml:"terminal-accept-card-delay"`
     99 	TerminalProvider        string `yaml:"terminal-provider"`
    100 	TerminalId              string `yaml:"terminal-id"`
    101 	TerminalUserId          string `yaml:"terminal-user-id"`
    102 	TerminalAccessToken     string `yaml:"terminal-access-token"`
    103 	TerminalLongPollMs      string `yaml:"terminal-long-poll-ms"`
    104 	TerminalQrCodeBase      string `yaml:"terminal-qr-base"`
    105 	// simulates the user scanning the QR code presented at the terminal
    106 	WalletScanQrDelay   int    `yaml:"wallet-scan-qr-delay"`
    107 	WalletLongPollMs    string `yaml:"wallet-long-poll-ms"`
    108 	WireGatewayUsername string `yaml:"wire-gateway-user"`
    109 	WireGatewayPassword string `yaml:"wire-gateway-password"`
    110 }
    111 
    112 func parseSimulationConfig(path string) (*SimulationConfig, error) {
    113 
    114 	f, err := os.Open(path)
    115 	if err != nil {
    116 		return nil, err
    117 	}
    118 	defer f.Close()
    119 
    120 	stat, err := f.Stat()
    121 	if err != nil {
    122 		return nil, err
    123 	}
    124 
    125 	content := make([]byte, stat.Size())
    126 	_, err = f.Read(content)
    127 	if err != nil {
    128 		return nil, err
    129 	}
    130 
    131 	cfg := new(SimulationConfig)
    132 	err = yaml.Unmarshal(content, cfg)
    133 	if err != nil {
    134 		return nil, err
    135 	}
    136 	return cfg, nil
    137 }