コード例 #1
0
ファイル: example.go プロジェクト: chrislusf/netchan
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
ファイル: netchan.go プロジェクト: chrislusf/netchan
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()
	}
}