Пример #1
0
func pub() {
	id, _ := pastry.NodeIDFromBytes([]byte(strings.Repeat("pub", 20)))
	p := peter.New(id, "127.0.0.1", "127.0.0.1", "home", 1234)
	go p.Listen()
	reader := bufio.NewReader(os.Stdin)
	for {
		fmt.Printf("pub> ")
		text, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println(err)
			return
		}

		text = text[:len(text)-1] // strip EOL
		if text == "" {
			continue
		}
		if text == "exit" {
			break
		}

		if err := p.Broadcast(topic, []byte(text)); err != nil {
			fmt.Println(err)
		}

	}

}
Пример #2
0
func createNodeId() pastry.NodeID {
	hostname, _ := os.Hostname()
	name := fmt.Sprintf("%d %d", port, port) + "on host:" + hostname
	debug.Debugf("%s\n", name)
	id, e := pastry.NodeIDFromBytes([]byte(name))
	if e != nil {
		panic(e)
	}

	return id
}
Пример #3
0
func sub() {
	id, _ := pastry.NodeIDFromBytes([]byte(strings.Repeat("sub", 20)))
	p := peter.New(id, "127.0.0.1", "127.0.0.1", "home", 1235)
	go p.Listen()
	defer p.Stop()

	err := p.Join("127.0.0.1", 1234)
	dieIfError(err)
	err = p.Subscribe(topic)
	dieIfError(err)
	time.Sleep(time.Hour)

}
Пример #4
0
func main() {
	id := createNodeId()

	self := pastry.NewNode(id, "localhost", "12.43.34.11", "home", port)

	credentials := pastry.Passphrase("we are here")
	cluster := pastry.NewCluster(self, credentials)
	app := &app{}
	cluster.RegisterCallback(app)
	switch port {
	case 1091:
		cluster.SetColor("blue")
	case 1092:
		cluster.SetColor("green")
	}

	go startListener(cluster)

	go func() {
		ticker := time.NewTicker(time.Second * 10)
		for _ = range ticker.C {
			debug.Debugf("%s", cluster.LRM())
		}

	}()

	if port != 1090 {
		if err := cluster.Join("localhost", 1090); err != nil {
			panic(err)
		}

	}

	reader := bufio.NewReader(os.Stdin)
	for {
		fmt.Printf("key to route> ")
		text, err := reader.ReadString('\n')
		if err != nil {
			fmt.Println(err)
			return
		}

		text = text[:len(text)-1] // strip EOL
		if text == "" {
			continue
		}
		if text == "exit" {
			cluster.Stop()
			break
		}

		key, err := pastry.NodeIDFromBytes([]byte(text))
		if err != nil {
			fmt.Printf("shit: %s\n", err)
			continue
		}

		fmt.Printf("Your key: %s\n", key)
		msg := cluster.NewMessage(byte(19), key, []byte("we are here"))
		err = cluster.Send(msg)
		if err != nil {
			println(err)
		}
	}

}