Exemple #1
0
func main() {
	c := &service.Client{}

	msgConn := message.NewConnectMessage()
	msgConn.SetVersion(3)
	msgConn.SetCleanSession(true)
	msgConn.SetClientId([]byte("01234567"))
	msgConn.SetKeepAlive(300)

	if err := c.Connect("tcp://127.0.0.1:1883", msgConn); err != nil {
		log.Fatal(err)
	}
	log.Println("connected")

	msgSub := message.NewSubscribeMessage()
	msgSub.AddTopic([]byte("#"), 0)
	onPub := func(msg *message.PublishMessage) error {
		return pub(c, msg)
	}
	onComp := func(msg, ack message.Message, err error) error {
		log.Printf("on complete subscribe: msg=%#v ack=%#v err=%v\n", msg, ack, err)
		return err
	}
	if err := c.Subscribe(msgSub, onComp, onPub); err != nil {
		log.Fatal(err)
	}
	log.Println("subscribed")

	//c.Disconnect()
	//log.Println("disconnected")

	done := make(chan bool)
	<-done
}
Exemple #2
0
func client(cmd *cobra.Command, args []string) {
	// Instantiates a new Client
	c = &service.Client{}

	// Creates a new MQTT CONNECT message and sets the proper parameters
	msg := message.NewConnectMessage()
	msg.SetVersion(4)
	msg.SetCleanSession(true)
	msg.SetClientId([]byte(fmt.Sprintf("pingmqclient%d%d", os.Getpid(), time.Now().Unix())))
	msg.SetKeepAlive(300)

	// Connects to the remote server at 127.0.0.1 port 1883
	if err := c.Connect(clientURI, msg); err != nil {
		log.Fatal(err)
	}

	// Creates a new SUBSCRIBE message to subscribe to topic "abc"
	submsg := message.NewSubscribeMessage()

	for _, t := range clientTopics {
		submsg.AddTopic([]byte(t), 0)
	}

	c.Subscribe(submsg, nil, onPublish)

	<-done
}
Exemple #3
0
func main() {
	flag.Parse()
	done := make(chan bool)

	c := &service.Client{}

	msg := message.NewConnectMessage()
	msg.SetVersion(3)
	msg.SetCleanSession(true)
	msg.SetClientId(clientID)
	msg.SetKeepAlive(300)

	if err := c.Connect("tcp://127.0.0.1:1883", msg); err != nil {
		panic(err)
	}

	pub := message.NewPublishMessage()
	pub.SetTopic([]byte(*topic))
	pub.SetQoS(0)
	pub.SetPayload([]byte(*payload))

	if err := c.Publish(pub, nil); err != nil {
		panic(err)
	}

	fmt.Println("waiting", 1)
	<-done
}
Exemple #4
0
func getConnectMessage(conn io.Closer) (*message.ConnectMessage, error) {
	buf, err := getMessageBuffer(conn)
	if err != nil {
		//glog.Debugf("Receive error: %v", err)
		return nil, err
	}

	msg := message.NewConnectMessage()

	_, err = msg.Decode(buf)
	//glog.Debugf("Received: %s", msg)
	return msg, err
}
Exemple #5
0
func getConnectMessage(conn io.Closer) (*message.ConnectMessage, error) {
	buf, err := getMessageBuffer(conn)
	if err != nil {
		//Log.Debugc(func() string{ return fmt.Sprintf("Receive error: %v", err)})
		return nil, err
	}

	msg := message.NewConnectMessage()

	_, err = msg.Decode(buf)
	//Log.Debugc(func() string{ return fmt.Sprintf("Received: %s", msg)})
	return msg, err
}
Exemple #6
0
func newConnectMessage() *message.ConnectMessage {
	msg := message.NewConnectMessage()
	msg.SetWillQos(1)
	msg.SetVersion(4)
	msg.SetCleanSession(true)
	msg.SetClientId([]byte("surgemq"))
	msg.SetKeepAlive(10)
	msg.SetWillTopic([]byte("will"))
	msg.SetWillMessage([]byte("send me home"))
	msg.SetUsername([]byte("surgemq"))
	msg.SetPassword([]byte("verysecret"))

	return msg
}
Exemple #7
0
func newConnectMessage(cid int) *message.ConnectMessage {
	msg := message.NewConnectMessage()
	msg.SetWillQos(1)
	msg.SetVersion(byte(version))
	msg.SetCleanSession(true)
	msg.SetClientId([]byte(fmt.Sprintf("surgemq%d", cid)))
	msg.SetKeepAlive(10)
	msg.SetWillTopic([]byte("will"))
	msg.SetWillMessage([]byte("send me home"))
	msg.SetUsername([]byte(user))
	msg.SetPassword([]byte(pass))

	return msg
}
Exemple #8
0
func main() {

	flag.Parse()

	c := service.Client{}

	msg := message.NewConnectMessage()
	msg.SetWillQos(1)
	msg.SetVersion(4)
	msg.SetCleanSession(true)
	msg.SetClientId([]byte("surgemq"))
	msg.SetKeepAlive(10)
	msg.SetWillTopic([]byte("will"))
	msg.SetWillMessage([]byte("send me home"))
	msg.SetUsername([]byte("surgemq"))
	msg.SetPassword([]byte("verysecret"))

	// Connects to the remote server at 127.0.0.1 port 1883
	str := *addr + ":" + *port
	c.Connect("tcp://"+str, msg)

	// Creates a new SUBSCRIBE message to subscribe to topic "abc"
	submsg := message.NewSubscribeMessage()
	submsg.AddTopic([]byte("abc"), 0)

	// Subscribes to the topic by sending the message. The first nil in the function
	// call is a OnCompleteFunc that should handle the SUBACK message from the server.
	// Nil means we are ignoring the SUBACK messages. The second nil should be a
	// OnPublishFunc that handles any messages send to the client because of this
	// subscription. Nil means we are ignoring any PUBLISH messages for this topic.
	c.Subscribe(submsg, onCompleteFunc, onPublishFunc)

	// Creates a new PUBLISH message with the appropriate contents for publishing
	pubmsg := message.NewPublishMessage()
	pubmsg.SetTopic([]byte("abc"))
	pubmsg.SetPayload(make([]byte, 1024))
	pubmsg.SetQoS(0)

	// Publishes to the server by sending the message
	c.Publish(pubmsg, onCompleteFunc)

	// Disconnects from the server
	jklog.L().Infoln("disconnected.")
	for {
		time.Sleep(5000 * time.Millisecond)
		// c.Publish(pubmsg, onCompleteFunc)
	}

	c.Disconnect()
}
Exemple #9
0
func (this *Session) Update(msg *message.ConnectMessage) error {
	this.mu.Lock()
	defer this.mu.Unlock()

	this.cbuf = make([]byte, msg.Len())
	this.Cmsg = message.NewConnectMessage()

	if _, err := msg.Encode(this.cbuf); err != nil {
		return err
	}

	if _, err := this.Cmsg.Decode(this.cbuf); err != nil {
		return err
	}

	return nil
}
Exemple #10
0
func (this *Session) Init(msg *message.ConnectMessage) error {
	this.mu.Lock()
	defer this.mu.Unlock()

	if this.initted {
		return fmt.Errorf("Session already initialized")
	}

	this.cbuf = make([]byte, msg.Len())
	this.Cmsg = message.NewConnectMessage()

	if _, err := msg.Encode(this.cbuf); err != nil {
		return err
	}

	if _, err := this.Cmsg.Decode(this.cbuf); err != nil {
		return err
	}

	if this.Cmsg.WillFlag() {
		this.Will = message.NewPublishMessage()
		this.Will.SetQoS(this.Cmsg.WillQos())
		this.Will.SetTopic(this.Cmsg.WillTopic())
		this.Will.SetPayload(this.Cmsg.WillMessage())
		this.Will.SetRetain(this.Cmsg.WillRetain())
	}

	this.topics = make(map[string]byte, 1)

	this.id = string(msg.ClientId())

	this.Pub1ack = newAckqueue(defaultQueueSize)
	this.Pub2in = newAckqueue(defaultQueueSize)
	this.Pub2out = newAckqueue(defaultQueueSize)
	this.Suback = newAckqueue(defaultQueueSize)
	this.Unsuback = newAckqueue(defaultQueueSize)
	this.Pingack = newAckqueue(defaultQueueSize)

	this.initted = true

	return nil
}
Exemple #11
0
func ExampleClient() {
	// Instantiates a new Client
	c := &Client{}

	// Creates a new MQTT CONNECT message and sets the proper parameters
	msg := message.NewConnectMessage()
	msg.SetWillQos(1)
	msg.SetVersion(4)
	msg.SetCleanSession(true)
	msg.SetClientId([]byte("surgemq"))
	msg.SetKeepAlive(10)
	msg.SetWillTopic([]byte("will"))
	msg.SetWillMessage([]byte("send me home"))
	msg.SetUsername([]byte("surgemq"))
	msg.SetPassword([]byte("verysecret"))

	// Connects to the remote server at 127.0.0.1 port 1883
	c.Connect("tcp://127.0.0.1:1883", msg)

	// Creates a new SUBSCRIBE message to subscribe to topic "abc"
	submsg := message.NewSubscribeMessage()
	submsg.AddTopic([]byte("abc"), 0)

	// Subscribes to the topic by sending the message. The first nil in the function
	// call is a OnCompleteFunc that should handle the SUBACK message from the server.
	// Nil means we are ignoring the SUBACK messages. The second nil should be a
	// OnPublishFunc that handles any messages send to the client because of this
	// subscription. Nil means we are ignoring any PUBLISH messages for this topic.
	c.Subscribe(submsg, nil, nil)

	// Creates a new PUBLISH message with the appropriate contents for publishing
	pubmsg := message.NewPublishMessage()
	pubmsg.SetTopic([]byte("abc"))
	pubmsg.SetPayload(make([]byte, 1024))
	pubmsg.SetQoS(0)

	// Publishes to the server by sending the message
	c.Publish(pubmsg, nil)

	// Disconnects from the server
	c.Disconnect()
}
Exemple #12
0
func NewSurgeMQ(numberOfMessages int, testLatency bool) *SurgeMQ {
	uri := "tcp://127.0.0.1:1883"
	client := &service.Client{}

	msg := message.NewConnectMessage()
	msg.SetWillQos(1)
	msg.SetVersion(4)
	msg.SetCleanSession(true)
	msg.SetClientId([]byte("surgemq"))
	msg.SetKeepAlive(10)
	msg.SetWillTopic([]byte("will"))
	msg.SetWillMessage([]byte("send me home"))
	msg.SetUsername([]byte("surgemq"))
	msg.SetPassword([]byte("verysecret"))

	err := client.Connect(uri, msg)

	if err != nil {
		panic(err)
	}

	var handler benchmark.MessageHandler
	if testLatency {
		handler = &benchmark.LatencyMessageHandler{
			NumberOfMessages: numberOfMessages,
			Latencies:        []float32{},
		}
	} else {
		handler = &benchmark.ThroughputMessageHandler{NumberOfMessages: numberOfMessages}
	}

	return &SurgeMQ{
		handler:     handler,
		subject:     "test",
		client:      client,
		testLatency: testLatency,
	}
}