From cb4a30b9c26317a94e0be16029c8107d830fcd0d Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期一, 26 八月 2019 13:46:48 +0800
Subject: [PATCH] update

---
 main.go |  124 ++++++++++++++++------------------------
 1 files changed, 50 insertions(+), 74 deletions(-)

diff --git a/main.go b/main.go
index fb8204a..c46e870 100644
--- a/main.go
+++ b/main.go
@@ -3,84 +3,55 @@
 import (
 	"context"
 	"demo/deliver"
+	"demo/profile"
 	"flag"
 	"fmt"
 	"os"
+	"os/signal"
+	"time"
+
+	"golang.org/x/sys/unix"
 )
 
-const dLen = 12 * 1024 * 1024
-
-var ctx, cancel = context.WithCancel(context.Background())
-
-func senderMode(ipc string, m deliver.Mode, count int, one bool) {
-	if m == deliver.ReqRep {
-		req(ipc, m)
-	} else if m == deliver.Shm {
-		shmSender(ipc, 2, 32*1024*1024)
-		// shmReciever2(ipc, count, 2, 32*1024*1024)
-	}
-
-	if one {
-		oneSender(ipc, m)
-	} else {
-		nSender(ipc, m, count)
-	}
-}
-
-func recvMode(ipc string, m deliver.Mode, count int, n bool) {
-	if m == deliver.ReqRep {
-		rep(ipc, m)
-	} else if m == deliver.Shm {
-		shmReciever(ipc, count)
-		// shmSender2(ipc, count)
-	}
-
-	if n {
-		nReciever(ipc, m, count)
-	} else {
-		oneReciever(ipc, m)
-	}
-}
-
 var (
-	proc         string
-	procCount    int
-	mode         string
-	ipc          string
-	oneSendnRecv bool
+	server bool
+	proto  string
+	ipc    string
 
-	tmm string
+	count int
 )
 
 const (
-	act  = "act"
-	pass = "pass"
+	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.StringVar(&proc, "p", "act", "proc as sender")
-	flag.IntVar(&procCount, "c", 1, "proc run count")
+	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")
 
-	flag.StringVar(&mode, "m", "pushpull", "proc run mode pushpull or pubsub etc.")
-
-	flag.StringVar(&ipc, "i", "ipc:///tmp/pic.ipc", "ipc label")
-
-	flag.BoolVar(&oneSendnRecv, "n", true, "one send n recv")
-
-	flag.StringVar(&tmm, "t", "server", "")
 }
 
-func modeType(t string) deliver.Mode {
+func deliverMode(m string) deliver.Mode {
 
-	if t == "pushpull" {
+	if m == push || m == pull {
 		return deliver.PushPull
-	} else if t == "pubsub" {
+	} else if m == pub || m == sub {
 		return deliver.PubSub
-	} else if t == "pair" {
+	} else if m == pair {
 		return deliver.Pair
-	} else if t == "reqrep" {
+	} else if m == req || m == rep {
 		return deliver.ReqRep
-	} else if t == "shm" {
+	} else if m == send || m == recv {
 		return deliver.Shm
 	}
 
@@ -90,26 +61,31 @@
 func main() {
 	flag.Parse()
 
-	if tmm == "server" {
-		c, _ := deliver.NewServerWithTimeout(deliver.PushPull, ipc, 1000)
-		// c := deliver.NewServer(deliver.PushPull, ipc)
-		nSenderImpl(c, 0)
-	} else if tmm == "client" {
-		s, _ := deliver.NewServerWithTimeout(deliver.PushPull, ipc, 100)
-
-		oneRecvImpl(s, 0)
-
+	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,
 	}
-	return
-	m := modeType(mode)
-	if m > deliver.ModeStart {
-		if proc == act {
-			senderMode(ipc, m, procCount, oneSendnRecv)
-		} else {
-			recvMode(ipc, m, procCount, oneSendnRecv)
-		}
+
+	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)

--
Gitblit v1.8.0