| 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 rpc |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"fmt" |
| 21 |
"sync" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/eclipse/paho.golang/paho" |
| 25 |
) |
| 26 |
|
| 27 |
// Handler is the struct providing a request/response functionality for the paho |
| 28 |
// MQTT v5 client |
| 29 |
type Handler struct { |
| 30 |
sync.Mutex |
| 31 |
c *paho.Client |
| 32 |
correlData map[string]chan *paho.Publish |
| 33 |
} |
| 34 |
|
| 35 |
func NewHandler(ctx context.Context, c *paho.Client) (*Handler, error) { |
| 36 |
h := &Handler{ |
| 37 |
c: c, |
| 38 |
correlData: make(map[string]chan *paho.Publish), |
| 39 |
} |
| 40 |
|
| 41 |
responseTopic := fmt.Sprintf("%s/responses", c.ClientID()) |
| 42 |
c.AddOnPublishReceived(func(pr paho.PublishReceived) (bool, error) { |
| 43 |
if pr.Packet.Topic == responseTopic { |
| 44 |
h.responseHandler(pr.Packet) |
| 45 |
return true, nil |
| 46 |
} |
| 47 |
return false, nil |
| 48 |
}) |
| 49 |
|
| 50 |
_, err := c.Subscribe(ctx, &paho.Subscribe{ |
| 51 |
Subscriptions: []paho.SubscribeOptions{ |
| 52 |
{Topic: fmt.Sprintf("%s/responses", c.ClientID()), QoS: 1}, |
| 53 |
}, |
| 54 |
}) |
| 55 |
if err != nil { |
| 56 |
return nil, err |
| 57 |
} |
| 58 |
|
| 59 |
return h, nil |
| 60 |
} |
| 61 |
|
| 62 |
func (h *Handler) addCorrelID(cID string, r chan *paho.Publish) { |
| 63 |
h.Lock() |
| 64 |
defer h.Unlock() |
| 65 |
|
| 66 |
h.correlData[cID] = r |
| 67 |
} |
| 68 |
|
| 69 |
func (h *Handler) getCorrelIDChan(cID string) chan *paho.Publish { |
| 70 |
h.Lock() |
| 71 |
defer h.Unlock() |
| 72 |
|
| 73 |
rChan := h.correlData[cID] |
| 74 |
delete(h.correlData, cID) |
| 75 |
|
| 76 |
return rChan |
| 77 |
} |
| 78 |
|
| 79 |
func (h *Handler) Request(ctx context.Context, pb *paho.Publish) (*paho.Publish, error) { |
| 80 |
cID := fmt.Sprintf("%d", time.Now().UnixNano()) |
| 81 |
rChan := make(chan *paho.Publish, 1) // Buffered to prevent goroutine leak when context cancelled |
| 82 |
|
| 83 |
h.addCorrelID(cID, rChan) |
| 84 |
|
| 85 |
if pb.Properties == nil { |
| 86 |
pb.Properties = &paho.PublishProperties{} |
| 87 |
} |
| 88 |
|
| 89 |
pb.Properties.CorrelationData = []byte(cID) |
| 90 |
pb.Properties.ResponseTopic = fmt.Sprintf("%s/responses", h.c.ClientID()) |
| 91 |
pb.Retain = false |
| 92 |
|
| 93 |
_, err := h.c.Publish(ctx, pb) |
| 94 |
if err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
|
| 98 |
select { |
| 99 |
case resp := <-rChan: |
| 100 |
return resp, nil |
| 101 |
case <-ctx.Done(): |
| 102 |
return nil, ctx.Err() |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
func (h *Handler) responseHandler(pb *paho.Publish) { |
| 108 |
if pb.Properties == nil || pb.Properties.CorrelationData == nil { |
| 109 |
return |
| 110 |
} |
| 111 |
|
| 112 |
rChan := h.getCorrelIDChan(string(pb.Properties.CorrelationData)) |
| 113 |
if rChan == nil { |
| 114 |
return |
| 115 |
} |
| 116 |
|
| 117 |
rChan <- pb |
| 118 |
} |