示例#1
0
文件: main.go 项目: jbeshir/unanimity
func main() {
	// Define and parse flags.
	id := flag.Uint("id", 0, "Set the client node ID to connect to.")
	rate = flag.Uint("rate", 0, "Sets the maximum messages per second.")
	flag.Parse()

	// Validate flags.
	if *id == 0 || *id > 0xFFFF {
		log.Fatal("Invalid node ID specified.")
	}

	// Load configuration from config file.
	loadConfig()

	fmt.Printf("Loaded configuration, ID to connect to is %d.\n", *id)

	conn, err := connect.TestDial(uint16(*id))
	if err != nil {
		log.Fatal(err)
	}

	var msg cliproto_up.Authenticate
	msg.Username = new(string)
	msg.Password = new(string)
	msg.SessionId = new(uint64)
	*msg.Username = "msgsource"
	*msg.Password = "msgsource"
	conn.SendProto(2, &msg)

	for {
		respMsg, ok := <-conn.Received
		if !ok {
			log.Fatal("connection error")
		}

		switch *respMsg.MsgType {
		case 2:
			conn.Close()
			log.Fatal("auth failed")
		case 3:
			log.Print("authenticated, follow msgsink...")

			var followMsg cliproto_up.FollowUsername
			followMsg.Username = new(string)
			*followMsg.Username = "msgsink"
			conn.SendProto(3, &followMsg)
		case 4:
			log.Fatal("follow failed")
		case 6:
			handleData(respMsg.Content)
		case 7:
			log.Print("following msgsink, sending msgs...")
			go sendMessages(conn)
		}
	}
}
示例#2
0
文件: main.go 项目: jbeshir/unanimity
func main() {
	// Define and parse flags.
	id := flag.Uint("id", 0, "Set the client node ID to connect to.")
	print = flag.Uint("print", 10000, "Sets number of messages to print "+
		"after.")

	flag.Parse()

	// Validate flags.
	if *id == 0 || *id > 0xFFFF {
		log.Fatal("Invalid node ID specified.")
	}

	// Load configuration from config file.
	loadConfig()

	fmt.Printf("Loaded configuration, ID to connect to is %d.\n", *id)

	conn, err := connect.TestDial(uint16(*id))
	if err != nil {
		log.Fatal(err)
	}

	var msg cliproto_up.Authenticate
	msg.Username = new(string)
	msg.Password = new(string)
	msg.SessionId = new(uint64)
	*msg.Username = "msgsink"
	*msg.Password = "msgsink"
	conn.SendProto(2, &msg)

	for {
		respMsg, ok := <-conn.Received
		if !ok {
			break
			log.Fatal("connection error")
		}

		switch *respMsg.MsgType {
		case 2:
			conn.Close()
			log.Fatal("auth failed")
		case 3:
			log.Print("authenticated")
		case 10:
			handleMsg(respMsg.Content)
		}
	}
}
示例#3
0
文件: main.go 项目: jbeshir/unanimity
func register(node uint16, prefix string, limit uint, doneChan chan bool) {

	for i := uint(0); i < limit; i++ {
		conn, err := connect.TestDial(node)
		if err != nil {
			log.Fatal(err)
		}

		var msg cliproto_up.Authenticate
		msg.Username = new(string)
		msg.Password = new(string)
		msg.SessionId = new(uint64)
		*msg.Username = prefix + strconv.FormatUint(uint64(i), 10)
		*msg.Password = prefix + strconv.FormatUint(uint64(i), 10)
		conn.SendProto(2, &msg)

	connloop:
		for {
			respMsg, ok := <-conn.Received
			if !ok {
				log.Fatal("connection error to ", node)
				break connloop
			}

			switch *respMsg.MsgType {
			case 2:
				// Terminates.
				handleAuthFail(respMsg.Content)
			case 3:
				conn.Close()
				break connloop
			}
		}
	}

	doneChan <- true
}