zhangmeng
2019-08-27 9c770865849b34ea48f66763aa11a83a10aa457c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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 server {
        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 <URL> <ARG> ...\n")
    os.Exit(1)
 
}