| 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 |
"bytes" |
| 20 |
"context" |
| 21 |
"encoding/json" |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"log" |
| 25 |
"net" |
| 26 |
"os" |
| 27 |
"os/signal" |
| 28 |
"sync" |
| 29 |
"syscall" |
| 30 |
"time" |
| 31 |
|
| 32 |
"github.com/eclipse/paho.golang/paho" |
| 33 |
"github.com/eclipse/paho.golang/paho/extensions/rpc" |
| 34 |
) |
| 35 |
|
| 36 |
func init() { |
| 37 |
ic := make(chan os.Signal, 1) |
| 38 |
signal.Notify(ic, os.Interrupt, syscall.SIGTERM) |
| 39 |
go func() { |
| 40 |
<-ic |
| 41 |
os.Exit(0) |
| 42 |
}() |
| 43 |
} |
| 44 |
|
| 45 |
type Request struct { |
| 46 |
Function string `json:"function"` |
| 47 |
Param1 int `json:"param1"` |
| 48 |
Param2 int `json:"param2"` |
| 49 |
} |
| 50 |
|
| 51 |
type Response struct { |
| 52 |
Value int `json:"value"` |
| 53 |
} |
| 54 |
|
| 55 |
func listener(server, rTopic, username, password string) { |
| 56 |
var v sync.WaitGroup |
| 57 |
|
| 58 |
v.Add(1) |
| 59 |
|
| 60 |
go func() { |
| 61 |
conn, err := net.Dial("tcp", server) |
| 62 |
if err != nil { |
| 63 |
log.Fatalf("Failed to connect to %s: %s", server, err) |
| 64 |
} |
| 65 |
|
| 66 |
c := paho.NewClient(paho.ClientConfig{ |
| 67 |
Conn: conn, |
| 68 |
OnPublishReceived: []func(paho.PublishReceived) (bool, error){ |
| 69 |
func(pr paho.PublishReceived) (bool, error) { |
| 70 |
m := pr.Packet |
| 71 |
if m.Properties != nil && m.Properties.CorrelationData != nil && m.Properties.ResponseTopic != "" { |
| 72 |
log.Printf("Received message with response topic %s and correl id %s\n%s", m.Properties.ResponseTopic, string(m.Properties.CorrelationData), string(m.Payload)) |
| 73 |
|
| 74 |
var r Request |
| 75 |
var resp Response |
| 76 |
|
| 77 |
if err := json.NewDecoder(bytes.NewReader(m.Payload)).Decode(&r); err != nil { |
| 78 |
log.Printf("Failed to decode Request: %v", err) |
| 79 |
} |
| 80 |
|
| 81 |
switch r.Function { |
| 82 |
case "add": |
| 83 |
resp.Value = r.Param1 + r.Param2 |
| 84 |
case "mul": |
| 85 |
resp.Value = r.Param1 * r.Param2 |
| 86 |
case "div": |
| 87 |
resp.Value = r.Param1 / r.Param2 |
| 88 |
case "sub": |
| 89 |
resp.Value = r.Param1 - r.Param2 |
| 90 |
} |
| 91 |
|
| 92 |
body, _ := json.Marshal(resp) |
| 93 |
_, err := pr.Client.Publish(context.Background(), &paho.Publish{ |
| 94 |
Properties: &paho.PublishProperties{ |
| 95 |
CorrelationData: m.Properties.CorrelationData, |
| 96 |
}, |
| 97 |
Topic: m.Properties.ResponseTopic, |
| 98 |
Payload: body, |
| 99 |
}) |
| 100 |
if err != nil { |
| 101 |
log.Fatalf("failed to publish message: %s", err) |
| 102 |
} |
| 103 |
return true, nil |
| 104 |
} |
| 105 |
return false, nil |
| 106 |
}, |
| 107 |
}, |
| 108 |
}) |
| 109 |
|
| 110 |
cp := &paho.Connect{ |
| 111 |
KeepAlive: 30, |
| 112 |
CleanStart: true, |
| 113 |
ClientID: "listen1", |
| 114 |
Username: username, |
| 115 |
Password: []byte(password), |
| 116 |
} |
| 117 |
|
| 118 |
if username != "" { |
| 119 |
cp.UsernameFlag = true |
| 120 |
} |
| 121 |
if password != "" { |
| 122 |
cp.PasswordFlag = true |
| 123 |
} |
| 124 |
|
| 125 |
ca, err := c.Connect(context.Background(), cp) |
| 126 |
if err != nil { |
| 127 |
log.Fatalln(err) |
| 128 |
} |
| 129 |
if ca.ReasonCode != 0 { |
| 130 |
log.Fatalf("Failed to connect to %s : %d - %s", server, ca.ReasonCode, ca.Properties.ReasonString) |
| 131 |
} |
| 132 |
|
| 133 |
fmt.Printf("Connected to %s\n", server) |
| 134 |
|
| 135 |
_, err = c.Subscribe(context.Background(), &paho.Subscribe{ |
| 136 |
Subscriptions: []paho.SubscribeOptions{ |
| 137 |
{Topic: rTopic, QoS: 0}, |
| 138 |
}, |
| 139 |
}) |
| 140 |
if err != nil { |
| 141 |
log.Fatalf("failed to subscribe: %s", err) |
| 142 |
} |
| 143 |
|
| 144 |
v.Done() |
| 145 |
|
| 146 |
for { |
| 147 |
time.Sleep(1 * time.Second) |
| 148 |
} |
| 149 |
}() |
| 150 |
|
| 151 |
v.Wait() |
| 152 |
} |
| 153 |
|
| 154 |
func main() { |
| 155 |
server := flag.String("server", "127.0.0.1:1883", "The full URL of the MQTT server to connect to") |
| 156 |
rTopic := flag.String("rtopic", "rpc/request", "Topic for requests to go to") |
| 157 |
username := flag.String("username", "", "A username to authenticate to the MQTT server") |
| 158 |
password := flag.String("password", "", "Password to match username") |
| 159 |
flag.Parse() |
| 160 |
|
| 161 |
// paho.SetDebugLogger(log.New(os.Stderr, "RPC: ", log.LstdFlags)) |
| 162 |
|
| 163 |
listener(*server, *rTopic, *username, *password) |
| 164 |
|
| 165 |
conn, err := net.Dial("tcp", *server) |
| 166 |
if err != nil { |
| 167 |
log.Fatalf("Failed to connect to %s: %s", *server, err) |
| 168 |
} |
| 169 |
|
| 170 |
ctx, cancel := context.WithCancel(context.Background()) |
| 171 |
defer cancel() |
| 172 |
|
| 173 |
c := paho.NewClient(paho.ClientConfig{ |
| 174 |
OnPublishReceived: []func(paho.PublishReceived) (bool, error){ // Noop handler |
| 175 |
func(pr paho.PublishReceived) (bool, error) { |
| 176 |
return true, nil |
| 177 |
}}, |
| 178 |
Conn: conn, |
| 179 |
}) |
| 180 |
|
| 181 |
cp := &paho.Connect{ |
| 182 |
KeepAlive: 30, |
| 183 |
CleanStart: true, |
| 184 |
Username: *username, |
| 185 |
Password: []byte(*password), |
| 186 |
} |
| 187 |
|
| 188 |
if *username != "" { |
| 189 |
cp.UsernameFlag = true |
| 190 |
} |
| 191 |
if *password != "" { |
| 192 |
cp.PasswordFlag = true |
| 193 |
} |
| 194 |
|
| 195 |
ca, err := c.Connect(context.Background(), cp) |
| 196 |
if err != nil { |
| 197 |
log.Fatalln(err) |
| 198 |
} |
| 199 |
if ca.ReasonCode != 0 { |
| 200 |
log.Fatalf("Failed to connect to %s : %d - %s", *server, ca.ReasonCode, ca.Properties.ReasonString) |
| 201 |
} |
| 202 |
|
| 203 |
fmt.Printf("Connected to %s\n", *server) |
| 204 |
|
| 205 |
h, err := rpc.NewHandler(ctx, c) |
| 206 |
if err != nil { |
| 207 |
log.Fatal(err) |
| 208 |
} |
| 209 |
|
| 210 |
resp, err := h.Request(ctx, &paho.Publish{ |
| 211 |
Topic: *rTopic, |
| 212 |
Payload: []byte(`{"function":"mul", "param1": 10, "param2": 5}`), |
| 213 |
}) |
| 214 |
if err != nil { |
| 215 |
log.Fatal(err) |
| 216 |
} |
| 217 |
|
| 218 |
log.Printf("Received response: %s", string(resp.Payload)) |
| 219 |
} |