示例#1
0
func main() {
	flag.Parse()

	// start a leader for service discovery
	go func() {
		leader.RunLeader(":8930")
	}()

	go func() {
		agentServer := agent.NewAgentServer(8931, "localhost:8930")
		agentServer.Run()
	}()

	sendChan, err := sender.NewChannel("source", 8931)
	if err != nil {
		panic(err)
	}

	recvChan, err := receiver.NewChannel("source", "127.0.0.1:8930")
	if err != nil {
		panic(err)
	}

	for i := 0; i < 100; i++ {
		sendChan <- []byte("message " + strconv.Itoa(i))
	}

	println("100 messages sent")

	for m := range recvChan {
		println(string(m))
	}

}
示例#2
0
func main() {
	switch kingpin.MustParse(app.Parse(os.Args[1:])) {
	case leader.FullCommand():
		println("listening on", (*leaderIp)+":"+strconv.Itoa(*leaderPort))
		l.RunLeader((*leaderIp) + ":" + strconv.Itoa(*leaderPort))
	case sender.FullCommand():
		var wg sync.WaitGroup
		sendChan, err := s.NewChannel(*sendToChanName, *senderAgentPort, &wg)
		if err != nil {
			panic(err)
		}

		file := os.Stdin
		if *sendFile != "" {
			file, err = os.Open(*sendFile)
			if err != nil {
				log.Fatal(err)
			}
			defer file.Close()
		}

		counter := 0
		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			sendChan <- scanner.Bytes()
			counter++
		}
		if err := scanner.Err(); err != nil {
			log.Fatal(err)
		}
		close(sendChan)
		wg.Wait()

	case receiver.FullCommand():
		rc := r.NewReceiveChannel(*receiveFromChanName, *receiverLeader, 0)
		recvChan, err := rc.GetChannel()
		if err != nil {
			panic(err)
		}
		for m := range recvChan {
			println(string(m))
		}

	case agent.FullCommand():
		agentServer := a.NewAgentServer(*agentDir, *agentPort, *agentLeaderServer)
		agentServer.Run()
	}
}
示例#3
0
func GetSendChannel(name string, wg *sync.WaitGroup) (chan []byte, error) {
	return sender.NewChannel(name, networkContext.AgentPort, wg)
}