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 }
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) } } }
func (this *app) OnHeartbeat(node pastry.Node) { debug.Debugf("%+v", node) }
func (this *app) OnNodeExit(node pastry.Node) { debug.Debugf("%+v", node) }
func (this *app) OnNewLeaves(leafset []*pastry.Node) { debug.Debugf("%+v", leafset[0]) }
func (this *app) OnForward(msg *pastry.Message, nextId pastry.NodeID) bool { debug.Debugf("forward to %s for %s", nextId.String(), msg.String()) return true }
func (this *app) OnDeliver(msg pastry.Message) { // msg sent out to cluster debug.Debugf("%s", msg.String()) }
func (this *app) OnError(err error) { debug.Debugf("%s", err) }