Esempio n. 1
0
func teding_client(client_id, topic string) {
	//   log.Print("starting client ", i)
	defer func() {
		if err := recover(); err != nil {
			log.Printf("发生错误啦,重连!!!!! %s\n", err)
			time.Sleep(10 * time.Second)
			client(client_id, topic)
		}
	}()

	will_subscribe_count++

	var conn net.Conn
	var err error

	if *ssl {
		conn, err = tls.Dial("tcp", *host, &tls.Config{
			InsecureSkipVerify: true,
		})
		if err != nil {
			panic("failed to connect: " + err.Error())
		}
	} else {
		conn, err = net.Dial("tcp", *host)
		if err != nil {
			log.Print("dial: ", err)
			panic(err)
		}
	}

	cc := mqtt.NewClientConn(conn)
	cc.ClientId = client_id
	cc.KeepAliveTimer = 60
	cc.Dump = *dump

	if publish {
		log.Printf("teding_client %s :尝试连接到:%s\n", client_id, topic)
		if err := cc.Connect(*user, *pass); err != nil {
			log.Fatalf("connect: %v\n", err)
			os.Exit(1)
		}

		//   half := int32(*pace / 2)

		_ = cc.Subscribe([]proto.TopicQos{
			{topic, proto.QosAtLeastOnce},
		})
		subscribe_count++
		log.Printf("已连接上: %d\n", subscribe_count)
	} else {
		return
	}
	//   fmt.Printf("suback: %#v\n", ack)
	if *dump {
	}

	go func() {
		defer func() {
			if err := recover(); err != nil {
				log.Printf("ping 出错: %s\n", err)
				time.Sleep(10 * time.Second)
			}
		}()

		for {
			if publish {
				cc.Ping(&proto.PingReq{Header: proto.Header{}})
				time.Sleep(time.Minute)
			} else {
				return
			}
		}
	}()

	go func() {
		//     for _ = range cc.Incoming {
		for incom := range cc.Incoming {
			//TODO: 解析收到消息时的时间,把消息里的时间和现在的时间一起打印
			time_now := time.Now().UnixNano()
			fmt.Printf("{payload: %s, now: %d },\n", incom.Payload, time_now)
			//       fmt.Println(pub.Payload)
		}
	}()

	for {
		if publish {
			//     if false {
			var send_topic string
			var payload proto.Payload

			//       r := rand.Int31n(5)
			r := 4
			switch r {
			case 0:
				send_topic = topic
				payload = proto.BytesPayload(fmt.Sprintf("%d", time.Now().UnixNano()))
			case 1:
				send_topic = broadcast_topic
				msg := base64.StdEncoding.EncodeToString(([]byte)(fmt.Sprintf("%d", time.Now().UnixNano())))
				message, err := json.Marshal(&BroadCastMessage{Clients: small_qunliao, Payload: msg})
				//         fmt.Println((string)(message))
				if err != nil {
					log.Printf("marshal failed: %s \n", err)
				}
				payload = proto.BytesPayload(message)
			case 2:
				send_topic = broadcast_topic
				msg := base64.StdEncoding.EncodeToString(([]byte)(fmt.Sprintf("%d", time.Now().UnixNano())))
				message, err := json.Marshal(&BroadCastMessage{Clients: middle_qunliao, Payload: msg})
				//         fmt.Println((string)(message))
				if err != nil {
					log.Printf("marshal failed: %s \n", err)
				}
				payload = proto.BytesPayload(message)
			case 3:
				send_topic = broadcast_topic
				msg := base64.StdEncoding.EncodeToString(([]byte)(fmt.Sprintf("%d", time.Now().UnixNano())))
				message, err := json.Marshal(&BroadCastMessage{Clients: big_qunliao, Payload: msg})
				//         fmt.Println((string)(message))
				if err != nil {
					log.Printf("marshal failed: %s \n", err)
				}
				payload = proto.BytesPayload(message)
			case 4:
				send_topic = broadcast_topic
				msg := base64.StdEncoding.EncodeToString(([]byte)(fmt.Sprintf("%d", time.Now().UnixNano())))
				message, err := json.Marshal(&BroadCastMessage{Clients: super_big_qunliao, Payload: msg})
				//         fmt.Println((string)(message))
				if err != nil {
					log.Printf("marshal failed: %s \n", err)
				}
				payload = proto.BytesPayload(message)
			default:
			}

			cc.Publish(&proto.Publish{
				Header:    proto.Header{},
				TopicName: send_topic,
				Payload:   payload,
			})

			//     sltime := rand.Int31n(half) - (half / 2) + int32(*pace)
			time.Sleep((time.Duration)(*tpace) * time.Millisecond)
		} else {
			return
		}
	}
}
Esempio n. 2
0
func client(client_id, topic string) {
	//   log.Print("starting client ", i)
	defer func() {
		if err := recover(); err != nil {
			log.Printf("client_id: %s, 发生错误啦,重连!!!!! %s\n", client_id, err)
			time.Sleep(5 * time.Second)
			client(client_id, topic)
		}
	}()

	send_count := 0

	// First, create the set of root certificates. For this example we only
	// have one. It's also possible to omit this in order to use the
	// default root set of the current operating system.

	var conn net.Conn

	if *ssl {
		now_port++
		var port uint16 = now_port + uint16(*start_port)
		laddr_string := fmt.Sprintf("%s:%d", *laddr, port)
		laddr, err := net.ResolveTCPAddr("tcp", laddr_string)
		if err != nil {
			log.Print("addr err:", err)
			panic(err)
		}
		d := &net.Dialer{LocalAddr: laddr}

		conn, err = tls.DialWithDialer(d, "tcp", *host, &tls.Config{
			//     conn, err = tls.Dial("tcp", *host, &tls.Config{
			InsecureSkipVerify: true,
		})
		if err != nil {
			panic("failed to connect: " + err.Error())
		}
	} else {
		//     conn, err = net.Dial("tcp", *host)
		now_port++
		var port uint16 = now_port + uint16(*start_port)
		laddr_string := fmt.Sprintf("%s:%d", *laddr, port)
		laddr, err := net.ResolveTCPAddr("tcp", laddr_string)
		if err != nil {
			log.Print("addr err:", err)
			panic(err)
		}

		conn, err = net.DialTCP("tcp", laddr, raddr)

		if err != nil {
			log.Print("dial: ", err)
			panic(err)
		}
	}

	cc := mqtt.NewClientConn(conn)
	cc.ClientId = client_id
	cc.KeepAliveTimer = 60
	cc.Dump = *dump

	will_subscribe_count++

	if *reconnect_after > 0 {
		go func(conn net.Conn) {
			time.Sleep(time.Duration(*reconnect_after) * time.Second)
			conn.Close()
		}(conn)
	}

	tmp_id := will_subscribe_count
	if publish {
		log.Printf("client %d :尝试连接到: %s, %s\n", will_subscribe_count, client_id, topic)
		if err := cc.Connect(*user, *pass); err != nil {
			log.Fatalf("connect: %v\n", err)
			os.Exit(1)
		}

		//   half := int32(*pace / 2)

		_ = cc.Subscribe([]proto.TopicQos{
			{topic, proto.QosAtLeastOnce},
		})
		subscribe_count++
		log.Printf("已连接上: %d\n", subscribe_count)
	} else {
		return
	}
	//   fmt.Printf("suback: %#v\n", ack)
	if *dump {
	}

	go func() {
		defer func() {
			if err := recover(); err != nil {
				log.Printf("ping 出错: %s\n", err)
				time.Sleep(10 * time.Second)
			}
		}()

		for {
			if publish {
				cc.Ping(&proto.PingReq{Header: proto.Header{}})
				time.Sleep(time.Minute)
			} else {
				return
			}
		}
	}()

	go func() {
		defer func() {
			if err := recover(); err != nil {
				log.Printf("出错: %s\n", err)
				time.Sleep(10 * time.Second)
			}
		}()

		//     for _ = range cc.Incoming {
		for incom := range cc.Incoming {
			time_now := time.Now().UnixNano()
			fmt.Printf("{payload: %s, now: %d },\n", incom.Payload, time_now)
			//       recv_count++
			//       if recv_count%100 == 0 {
			//         log.Printf("%s 接收消息: %d \n", client_id, recv_count)
			//       }
			//       fmt.Printf("%s\n", incom.Payload)
		}
	}()

	time.Sleep(time.Duration(*start_wait) * time.Second)

	//   log.Printf("%d 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈\n", will_subscribe_count)
	_ = tmp_id
	if *ping_only {
		<-make(chan bool)
	} else if tmp_id < 250 {
		//   if true {
		//   if false {
		for {
			if publish {
				//         payload := base64.StdEncoding.EncodeToString(([]byte)(fmt.Sprintf("hello, I'm %s, now message count is %d", client_id, send_count)))
				payload := base64.StdEncoding.EncodeToString(([]byte)(fmt.Sprintf("%d", time.Now().UnixNano())))
				message, err := json.Marshal(&BroadCastMessage{Clients: recv_qunliao, Payload: payload})
				//         fmt.Println((string)(message))
				if err != nil {
					log.Printf("marshal failed: %s \n", err)
				}
				marshaled_message := proto.BytesPayload(message)
				//         message_payload := proto.BytesPayload(base64.StdEncoding.EncodeToString(marshaled_message))

				cc.Publish(&proto.Publish{
					Header:    proto.Header{},
					TopicName: broadcast_topic,
					Payload:   marshaled_message,
				})
				log.Printf("%s 发送群聊 \n", client_id)
				send_count++
				time.Sleep((time.Duration)(*pace) * time.Second)
				//     sltime := rand.Int31n(half) - (half / 2) + int32(*pace)
			} else {
				return
			}
		}
	} else if tmp_id < 8000 {
		for {
			if publish {
				//         payload := proto.BytesPayload(fmt.Sprintf("hello, I'm %s, now message count is %d", client_id, send_count))
				payload := proto.BytesPayload(fmt.Sprintf("%d", time.Now().UnixNano()))
				//         payload := base64.StdEncoding.EncodeToString(([]byte)(fmt.Sprintf("%d", time.Now().UnixNano())))
				//         message_payload := proto.BytesPayload(base64.StdEncoding.EncodeToString(marshaled_message))

				cc.Publish(&proto.Publish{
					Header:    proto.Header{},
					TopicName: topic,
					Payload:   payload,
				})
				log.Printf("%s 发送私聊 \n", client_id)
				send_count++
				time.Sleep((time.Duration)(*pace) * time.Second)
				//     sltime := rand.Int31n(half) - (half / 2) + int32(*pace)
			} else {
				return
			}
		}
	}
}