Debian Codegraph @

paho/cmd/stdoutsub/main.go

raw
1 /*
2 * Copyright (c) 2024 Contributors to the Eclipse Foundation
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v2.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * https://www.eclipse.org/legal/epl-2.0/
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14 */
15
16 package main
17
18 import (
19 "context"
20 "flag"
21 "fmt"
22 "log"
23 "net"
24 "os"
25 "os/signal"
26 "syscall"
27
28 "github.com/eclipse/paho.golang/paho"
29 )
30
31 func main() {
32 server := flag.String("server", "127.0.0.1:1883", "The MQTT server to connect to ex: 127.0.0.1:1883")
33 topic := flag.String("topic", "#", "Topic to subscribe to")
34 qos := flag.Int("qos", 0, "The QoS to subscribe to messages at")
35 clientid := flag.String("clientid", "", "A clientid for the connection")
36 username := flag.String("username", "", "A username to authenticate to the MQTT server")
37 password := flag.String("password", "", "Password to match username")
38 flag.Parse()
39
40 logger := log.New(os.Stdout, "SUB: ", log.LstdFlags)
41
42 msgChan := make(chan *paho.Publish)
43
44 conn, err := net.Dial("tcp", *server)
45 if err != nil {
46 log.Fatalf("Failed to connect to %s: %s", *server, err)
47 }
48
49 c := paho.NewClient(paho.ClientConfig{
50 OnPublishReceived: []func(paho.PublishReceived) (bool, error){ // Noop handler
51 func(pr paho.PublishReceived) (bool, error) {
52 msgChan <- pr.Packet
53 return true, nil
54 }},
55 Conn: conn,
56 })
57 c.SetDebugLogger(logger)
58 c.SetErrorLogger(logger)
59
60 cp := &paho.Connect{
61 KeepAlive: 30,
62 ClientID: *clientid,
63 CleanStart: true,
64 Username: *username,
65 Password: []byte(*password),
66 }
67
68 if *username != "" {
69 cp.UsernameFlag = true
70 }
71 if *password != "" {
72 cp.PasswordFlag = true
73 }
74
75 ca, err := c.Connect(context.Background(), cp)
76 if err != nil {
77 log.Fatalln(err)
78 }
79 if ca.ReasonCode != 0 {
80 log.Fatalf("Failed to connect to %s : %d - %s", *server, ca.ReasonCode, ca.Properties.ReasonString)
81 }
82
83 fmt.Printf("Connected to %s\n", *server)
84
85 ic := make(chan os.Signal, 1)
86 signal.Notify(ic, os.Interrupt, syscall.SIGTERM)
87 go func() {
88 <-ic
89 fmt.Println("signal received, exiting")
90 if c != nil {
91 d := &paho.Disconnect{ReasonCode: 0}
92 c.Disconnect(d)
93 }
94 os.Exit(0)
95 }()
96
97 sa, err := c.Subscribe(context.Background(), &paho.Subscribe{
98 Subscriptions: []paho.SubscribeOptions{
99 {Topic: *topic, QoS: byte(*qos)},
100 },
101 })
102 if err != nil {
103 log.Fatalln(err)
104 }
105 if sa.Reasons[0] != byte(*qos) {
106 log.Fatalf("Failed to subscribe to %s : %d", *topic, sa.Reasons[0])
107 }
108 log.Printf("Subscribed to %s", *topic)
109
110 for m := range msgChan {
111 log.Println("Received message:", string(m.Payload))
112 }
113 }