Skip to content

Commit

Permalink
Add callback for cloud disconnect (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
alei121 authored Oct 20, 2023
1 parent c273b79 commit 3af5955
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 24 deletions.
46 changes: 35 additions & 11 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ type Config struct {
// DeviceDeactivationHandler notifies when a device is deactivated
DeviceDeactivationHandler func(device *Device)

// TenantUnlinkedHandler notifies when a tenant is unlinked from the cloud instead of app calling UnlinkTenant
// Not providing a linked handler because it can only be triggered by calling LinkTenant
// The stored tenant ID, name and token should be discarded
TenantUnlinkedHandler func(tenant *Tenant)

// DeviceMessageHandler is invoked when a new data message is received
DeviceMessageHandler func(messageID string, device *Device, stream string, payload []byte)
}
Expand Down Expand Up @@ -293,14 +298,16 @@ func (app *App) pubsubConnect() error {
}

const (
msgType = "messageType"
msgTypeControl = "control"
msgTypeData = "data"
tenantKey = "tenant"
deviceKey = "device"
msgIDKey = "messageID"
msgTypeActivate = "device:activate"
msgTypeDeactivate = "device:deactivate"
msgType = "messageType"
msgTypeControl = "control"
msgTypeData = "data"
tenantKey = "tenant"
deviceKey = "device"
msgIDKey = "messageID"
msgTypeActivate = "device:activate"
msgTypeDeactivate = "device:deactivate"
msgTypeAppConnect = "app:connect"
msgTypeAppDisconnect = "app:disconnect"
)

// readStreamHandler returns the callback that handles messages received on the app's read stream
Expand Down Expand Up @@ -376,6 +383,21 @@ func (app *App) controlMsgHandler(id string, payload []byte) error {
if app.config.DeviceDeactivationHandler != nil {
app.config.DeviceDeactivationHandler(device)
}
} else if ctrlPayload.Type == msgTypeAppConnect {
// Ignore app connect message because app calls LinkTenant explicitly
} else if ctrlPayload.Type == msgTypeAppDisconnect {
v, ok = app.tenantMap.Load(ctrlPayload.Info.Tenant)
if !ok || v == nil {
return fmt.Errorf("unknown tenant: %s", ctrlPayload.Info.Tenant)
}
tenant := v.(*Tenant)
app.tenantMap.Delete(tenant.ID())
app.deviceMap.Delete(tenant.ID())
if app.config.TenantUnlinkedHandler != nil {
app.config.TenantUnlinkedHandler(tenant)
}
} else {
return fmt.Errorf("unknown control message type: %s", ctrlPayload.Type)
}
return nil
}
Expand Down Expand Up @@ -473,7 +495,12 @@ func (app *App) LinkTenant(otp string) (*Tenant, error) {
}

// UnlinkTenant unlinks a tenant from the application
// The stored tenant ID, name and token should be discarded
func (app *App) UnlinkTenant(tenant *Tenant) error {
// Remove from map first to prevent callback to TenantUnlinkedHandler
app.tenantMap.Delete(tenant.ID())
app.deviceMap.Delete(tenant.ID())

unlinkPath := fmt.Sprintf(
unlinkPath,
url.PathEscape(app.config.ID),
Expand All @@ -492,9 +519,6 @@ func (app *App) UnlinkTenant(tenant *Tenant) error {
return errors.New(errorResp.GetError())
}

app.tenantMap.Delete(tenant.ID())
app.deviceMap.Delete(tenant.ID())

return nil
}

Expand Down
3 changes: 1 addition & 2 deletions appinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ func (app *App) CreateAppInstance(instanceName string) (*App, error) {
// This is called with the parent app, using the parent app_key to authenticate.
func (app *App) DeleteAppInstance(instanceAppId string) error {
var errorResp errorResponse

path := fmt.Sprintf(deleteAppInstancePath, instanceAppId)

r, err := app.httpClient.R().
SetError(&errorResp).
Delete(path)
Expand Down Expand Up @@ -143,6 +141,7 @@ func (app *App) newAppConfig(appID, appApiKey string) Config {
ApiKey: appApiKey,
DeviceActivationHandler: app.config.DeviceActivationHandler,
DeviceDeactivationHandler: app.config.DeviceDeactivationHandler,
TenantUnlinkedHandler: app.config.TenantUnlinkedHandler,
DeviceMessageHandler: app.config.DeviceMessageHandler,
}
}
Expand Down
9 changes: 7 additions & 2 deletions examples/basic-consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ func activationHandler(d *sdk.Device) {
logger.Infof("Device activation: %v", d)
}

func deActivationHandler(d *sdk.Device) {
func deactivationHandler(d *sdk.Device) {
logger.Infof("Device deactivation: %v", d)
}

func tenantUnlinkedHandler(t *sdk.Tenant) {
logger.Infof("Tenant unlinked: %v", t)
}

func loadConfig(file string) (*config, error) {
data, err := os.ReadFile(file)
if err != nil {
Expand Down Expand Up @@ -103,7 +107,8 @@ func main() {
GlobalFQDN: config.App.GlobalFQDN,
RegionalFQDN: config.App.RegionalFQDN,
DeviceActivationHandler: activationHandler,
DeviceDeactivationHandler: deActivationHandler,
DeviceDeactivationHandler: deactivationHandler,
TenantUnlinkedHandler: tenantUnlinkedHandler,
DeviceMessageHandler: messageHandler,
ReadStreamID: config.App.ReadStream,
WriteStreamID: config.App.WriteStream,
Expand Down
5 changes: 5 additions & 0 deletions examples/echo-query/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func deactivationHandler(d *sdk.Device) {
logger.Infof("Device deactivation: %v", d)
}

func tenantUnlinkedHandler(t *sdk.Tenant) {
logger.Infof("Tenant unlinked: %v", t)
}

func loadConfig(file string) (*config, error) {
data, err := os.ReadFile(file)
if err != nil {
Expand Down Expand Up @@ -105,6 +109,7 @@ func main() {
RegionalFQDN: config.App.RegionalFQDN,
DeviceActivationHandler: activationHandler,
DeviceDeactivationHandler: deactivationHandler,
TenantUnlinkedHandler: tenantUnlinkedHandler,
DeviceMessageHandler: messageHandler,
ReadStreamID: config.App.ReadStream,
WriteStreamID: config.App.WriteStream,
Expand Down
5 changes: 5 additions & 0 deletions examples/multi-instance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func deactivationHandler(d *sdk.Device) {
logger.Infof("Device deactivation: %v", d)
}

func tenantUnlinkedHandler(t *sdk.Tenant) {
logger.Infof("Tenant unlinked: %v", t)
}

func loadConfig(file string) (*config, error) {
data, err := os.ReadFile(file)
if err != nil {
Expand Down Expand Up @@ -112,6 +116,7 @@ func main() {
RegionalFQDN: config.App.RegionalFQDN,
DeviceActivationHandler: activationHandler,
DeviceDeactivationHandler: deactivationHandler,
TenantUnlinkedHandler: tenantUnlinkedHandler,
DeviceMessageHandler: messageHandler,
ReadStreamID: config.App.ReadStream,
WriteStreamID: config.App.WriteStream,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ require (
github.com/klauspost/compress v1.15.1 // indirect
github.com/rs/xid v1.2.1
github.com/stretchr/testify v1.8.1
golang.org/x/tools v0.14.0 // indirect
gopkg.in/yaml.v2 v2.4.0
)
33 changes: 33 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,37 @@ github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos=
golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -110,19 +126,36 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
Binary file modified vendor/golang.org/x/net/publicsuffix/data/children
Binary file not shown.
Binary file modified vendor/golang.org/x/net/publicsuffix/data/nodes
Binary file not shown.
2 changes: 1 addition & 1 deletion vendor/golang.org/x/net/publicsuffix/data/text

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions vendor/golang.org/x/net/publicsuffix/table.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ github.com/rs/xid
github.com/stretchr/testify/assert
github.com/stretchr/testify/require
github.com/stretchr/testify/suite
# golang.org/x/net v0.4.0
# golang.org/x/net v0.16.0
golang.org/x/net/publicsuffix
# golang.org/x/tools v0.14.0
## explicit
# gopkg.in/yaml.v2 v2.4.0
## explicit
gopkg.in/yaml.v2
Expand Down

0 comments on commit 3af5955

Please sign in to comment.