package main import ( "context" "demo/deliver" "demo/profile" "flag" "fmt" "os" "os/signal" "time" "golang.org/x/sys/unix" "net/http" _ "net/http/pprof" ) var ( server bool proto string ipc string count int ) const ( push = "push" pull = "pull" pub = "pub" sub = "sub" req = "req" rep = "rep" send = "send" // shm send recv = "recv" // shm recv pair = "pair" ) func init() { flag.BoolVar(&server, "s", false, "run as server") flag.StringVar(&proto, "p", "", "run protocol e.g. push/pull req/rep send/recv(use shm)") flag.StringVar(&ipc, "i", "", "ipc address") flag.IntVar(&count, "c", 1, "run client count") } func deliverMode(m string) deliver.Mode { if m == push || m == pull { return deliver.PushPull } else if m == pub || m == sub { return deliver.PubSub } else if m == pair { return deliver.Pair } else if m == req || m == rep { return deliver.ReqRep } else if m == send || m == recv { return deliver.Shm } return deliver.NONE } func main() { flag.Parse() if proto == recv { go func() { http.ListenAndServe("0.0.0.0:6061", nil) }() } fnMap := map[string]func(context.Context, bool, string, int){ push: profile.Push, pull: profile.Pull, req: profile.Req, rep: profile.Rep, send: profile.Shmsend, recv: profile.Shmrecv, } ctx, cancel := context.WithCancel(context.Background()) if fn, ok := fnMap[proto]; ok { fn(ctx, server, ipc, count) } else { fmt.Println("NO THIS FUNC: ", proto) } c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill, unix.SIGTERM) <-c cancel() time.Sleep(3 * time.Second) fmt.Fprintf(os.Stderr, "Usage: pushpull push|pull ...\n") os.Exit(1) }