@@ -91,11 +91,11 @@ type Manager struct {
9191 readyToPublishResources map [string ]struct {}
9292 readyToUnpublishResources map [string ]struct {}
9393 creds ocfCloud.CoapSignUpResponse
94+ client * client.Conn
95+ signedIn bool
9496 }
9597
9698 logger log.Logger
97- client * client.Conn
98- signedIn bool
9999 resourcesPublished bool
100100 forceRefreshToken bool
101101 done chan struct {}
@@ -182,6 +182,12 @@ func (c *Manager) isInitialized() bool {
182182 return cfg .URL != ""
183183}
184184
185+ func (c * Manager ) isSignedIn () bool {
186+ c .private .mutex .Lock ()
187+ defer c .private .mutex .Unlock ()
188+ return c .private .signedIn
189+ }
190+
185191func (c * Manager ) handleTrigger (value reflect.Value , closed bool ) {
186192 if closed {
187193 return
@@ -203,7 +209,7 @@ func (c *Manager) handleTrigger(value reflect.Value, closed bool) {
203209 c .resetPublishing ()
204210 return
205211 }
206- if ! c .signedIn {
212+ if ! c .isSignedIn () {
207213 // resources will be published after sign in
208214 c .resetPublishing ()
209215 }
@@ -364,13 +370,7 @@ func (c *Manager) setCreds(creds ocfCloud.CoapSignUpResponse) {
364370 c .private .mutex .Lock ()
365371 defer c .private .mutex .Unlock ()
366372 c .private .creds = creds
367- c .signedIn = false
368- }
369-
370- func (c * Manager ) updateCreds (f func (creds * ocfCloud.CoapSignUpResponse )) {
371- c .private .mutex .Lock ()
372- defer c .private .mutex .Unlock ()
373- f (& c .private .creds )
373+ c .private .signedIn = false
374374}
375375
376376func (c * Manager ) getCreds () ocfCloud.CoapSignUpResponse {
@@ -473,18 +473,32 @@ func (c *Manager) serveCOAP(w mux.ResponseWriter, request *mux.Message) {
473473 }
474474}
475475
476+ func (c * Manager ) replaceClient (client * client.Conn ) * client.Conn {
477+ c .private .mutex .Lock ()
478+ defer c .private .mutex .Unlock ()
479+ c .private .signedIn = false
480+ oldClient := c .private .client
481+ c .private .client = client
482+ return oldClient
483+ }
484+
476485func (c * Manager ) close () error {
477- c . signedIn = false
478- if c . client == nil {
486+ oldClient := c . replaceClient ( nil )
487+ if oldClient == nil {
479488 return nil
480489 }
481- client := c .client
482- c .client = nil
483- return client .Close ()
490+ return oldClient .Close ()
491+ }
492+
493+ func (c * Manager ) getClient () * client.Conn {
494+ c .private .mutex .Lock ()
495+ defer c .private .mutex .Unlock ()
496+ return c .private .client
484497}
485498
486499func (c * Manager ) dial (ctx context.Context ) error {
487- if c .client != nil && c .client .Context ().Err () == nil {
500+ cc := c .getClient ()
501+ if cc != nil && cc .Context ().Err () == nil {
488502 return nil
489503 }
490504 _ = c .close ()
@@ -535,7 +549,16 @@ func (c *Manager) dial(ctx context.Context) error {
535549 if err != nil {
536550 return fmt .Errorf ("cannot dial to %v: %w" , addr .String (), err )
537551 }
538- c .client = conn
552+ conn .AddOnClose (func () {
553+ c .private .mutex .Lock ()
554+ defer c .private .mutex .Unlock ()
555+ if c .private .client == conn {
556+ c .logger .Infof ("cloud connection: closed" )
557+ c .private .client = nil
558+ c .private .signedIn = false
559+ }
560+ })
561+ c .replaceClient (conn )
539562 return nil
540563}
541564
0 commit comments