Beispiel #1
0
func chat() {
	node, err := gyre.New()
	if err != nil {
		log.Fatalln(err)
	}
	defer node.Stop()

	err = node.Start()
	if err != nil {
		log.Fatalln(err)
	}
	node.Join("CHAT")

	for {
		select {
		case e := <-node.Events():
			switch e.Type() {
			case gyre.EventShout:
				fmt.Printf("%c[2K\r%s%s> ", 27, string(e.Msg()), *name)
			}
		case msg := <-input:
			node.Shout("CHAT", []byte(msg))
		}
	}
}
Beispiel #2
0
func chat() {
	node, err := gyre.New()
	if err != nil {
		log.Fatalln(err)
	}
	defer node.Stop()

	if gossipConnect != nil {
		for _, u := range gossipConnect {
			node.GossipConnect(u)
		}
	}
	if *gossipBind != "" {
		node.GossipBind(*gossipBind)
	}

	err = node.Start()
	if err != nil {
		log.Fatalln(err)
	}
	node.Join("CHAT")

	for {
		select {
		case e := <-node.Events():
			switch e.Type() {
			case gyre.EventShout:
				fmt.Printf("%c[2K\r%s%s> ", 27, string(e.Msg()), *name)
			}
		case msg := <-input:
			node.Shout("CHAT", []byte(msg))
		}
	}
}
Beispiel #3
0
func newNode(conn *connection, log *logger.Logger) (*gyre.Gyre, error) {
	node, err := gyre.New()
	if err != nil {
		return nil, newError(errInitialize, err.Error())
	}
	if err := node.SetPort(conn.port); err != nil {
		return nil, newError(errPort, err.Error())
	}
	if len(conn.adapter) > 0 {
		if err := node.SetInterface(conn.adapter); err != nil {
			return nil, newError(errInterface, err.Error())
		}
	}
	// If announcing a service, add service headers.
	if conn.server {
		errors := [...]int{
			errGroupHeader, errNodeHeader, errServiceHeader, errVersionHeader}
		values := [...]string{conn.group, node.UUID(), conn.name, conn.version}
		for i, header := range [...]string{"group", "node", "type", "version"} {
			if err := node.SetHeader(header, values[i]); err != nil {
				return nil, newError(errors[i], err.Error())
			}
		}
	}
	if err := node.Start(); err != nil {
		return nil, newError(errStart, err.Error())
	}
	if err := node.Join(group); err != nil {
		node.Stop()
		return nil, newError(errJoin, err.Error())
	}
	var role string
	if conn.server {
		role = conn.name
	} else {
		role = "client-only"
	}
	log.Listen("sleuth: [%s:%d][%s %s]", group, conn.port, role, node.Name())
	return node, nil
}
Beispiel #4
0
func ping() {
	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt, os.Kill)

	node, err := gyre.New()
	if err != nil {
		log.Fatalln(err)
	}
	defer node.Stop()

	err = node.Start()
	if err != nil {
		log.Fatalln(err)
	}
	node.Join(*group)

	for {
		select {
		case e := <-node.Events():
			switch e.Type() {
			case gyre.EventEnter:
				log.Printf("[%s] peer %q entered\n", node.Name(), e.Name())
				node.Whisper(e.Sender(), []byte("Hello"))
			case gyre.EventExit:
				log.Printf("[%s] peer %q exited\n", node.Name(), e.Name())
			case gyre.EventWhisper:
				log.Printf("[%s] received ping (WHISPER) from %q\n", node.Name(), e.Name())
				node.Shout(*group, []byte("Hello"))
			case gyre.EventShout:
				log.Printf("[%s] (%s) received a ping (SHOUT) from %q\n", node.Name(), e.Group(), e.Name())
			}
		case <-c:
			return
		}
	}
}
Beispiel #5
0
func ping() {
	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt, os.Kill)

	node, err := gyre.New()
	if err != nil {
		log.Fatalln(err)
	}
	defer node.Stop()

	if *verbose {
		node.SetVerbose()
		log.SetFlags(log.LstdFlags | log.Lshortfile)
	}

	if gossipConnect != nil {
		for _, u := range gossipConnect {
			node.GossipConnect(u)
		}
	}
	if *gossipBind != "" {
		node.GossipBind(*gossipBind)
	}

	err = node.Start()
	if err != nil {
		log.Fatalln(err)
	}

	if *group != "*" {
		for _, g := range strings.Split(*group, ",") {
			node.Join(strings.TrimSpace(g))
		}
	}

	for {
		select {
		case e := <-node.Events():
			switch e.Type() {
			case gyre.EventEnter:
				log.Printf("[%s] peer %q entered\n", node.Name(), e.Name())

			case gyre.EventExit:
				log.Printf("[%s] peer %q exited\n", node.Name(), e.Name())

			case gyre.EventJoin:
				log.Printf("[%s] peer %q joined to %s\n", node.Name(), e.Name(), e.Group())
				if *group == "*" {
					node.Join(e.Group())
				}

			case gyre.EventLeave:
				log.Printf("[%s] peer %q left\n", node.Name(), e.Name())

			case gyre.EventWhisper:
				log.Printf("[%s] received a WHISPER from %q\n", node.Name(), e.Name())

			case gyre.EventShout:
				log.Printf("[%s] received a SHOUT targeted to %s group from %q\n", node.Name(), e.Group(), e.Name())
			}
		case <-c:
			return
		}
	}
}