diff --git a/cmd/root.go b/cmd/root.go index 5daf98b88d919004cb89072b97695a74332abdd4..3cb7d40e3df62a700ea1ff4c2b806b5eb20e5ac3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -96,25 +96,31 @@ var rootCmd = &cobra.Command{ }, } +// setupConnection handles connecting to permissioning and polling for the NDF once connected func setupConnection(impl *notifications.Impl, permissioningCertPath, permissioningAddr string) error { + // Read in permissioning certificate cert, err := utils.ReadFile(permissioningCertPath) if err != nil { - jww.FATAL.Panicf("Could not read permissioning cert: %+v", err) + return errors.Wrap(err, "Could not read permissioning cert") } + // Add host for permissioning server _, err = impl.Comms.AddHost(id.PERMISSIONING, permissioningAddr, cert, true, true) if err != nil { - jww.FATAL.Panicf("Failed to Create permissioning host: %+v", err) + return errors.Wrap(err, "Failed to Create permissioning host") } + // Loop until an NDF is received var def *ndf.NetworkDefinition for def == nil { def, err = notifications.PollNdf(nil, impl.Comms) + // Don't stop if error is expected if err != nil && !strings.Contains(err.Error(), ndf.NO_NDF) { return errors.Wrap(err, "Failed to get NDF") } } + // Update NDF & gateway host err = impl.UpdateNdf(def) if err != nil { return errors.Wrap(err, "Failed to update impl's NDF") diff --git a/notifications/notifications.go b/notifications/notifications.go index 4715d94de452f8f157a285b0f5daee940eaddafa..b2c2766c65982df6ecf10929e3b195ffd3ea5b0f 100644 --- a/notifications/notifications.go +++ b/notifications/notifications.go @@ -72,6 +72,7 @@ func (nb *Impl) RunNotificationLoop(loopDuration int, killChan chan struct{}, er case <-time.After(time.Millisecond * time.Duration(loopDuration)): } + // Poll for UIDs to notify UIDs, err := nb.pollFunc(nb) if err != nil { errChan <- errors.Wrap(err, "Failed to poll gateway for users to notify") @@ -79,6 +80,7 @@ func (nb *Impl) RunNotificationLoop(loopDuration int, killChan chan struct{}, er } for _, id := range UIDs { + // Attempt to notify a given user (will not error if UID not registered) _, err := nb.notifyFunc(nb.fcm, id, fc, nb.Storage) if err != nil { errChan <- errors.Wrapf(err, "Failed to notify user with ID %+v", id) @@ -120,13 +122,15 @@ func StartNotifications(params Params, noTLS, noFirebase bool) (*Impl, error) { } } + // set up stored functions impl.pollFunc = pollForNotifications impl.notifyFunc = notifyUser + // Start notification comms server handler := NewImplementation(impl) - impl.Comms = notificationBot.StartNotificationBot(id.NOTIFICATION_BOT, params.PublicAddress, handler, cert, key) + // Set up firebase messaging client if !noFirebase { app, err := firebase.SetupMessagingApp(params.FBCreds) if err != nil { @@ -159,6 +163,7 @@ func notifyUser(fcm *messaging.Client, uid string, fc *firebase.FirebaseComm, db u, err := db.GetUser(uid) if err != nil { jww.DEBUG.Printf("No registration found for user with ID %+v", uid) + // This path is not an error. if no results are returned, the user hasn't registered for notifications return "", nil } @@ -210,7 +215,7 @@ func (nb *Impl) UnregisterForNotifications(auth *connect.Auth) error { func (nb *Impl) UpdateNdf(ndf *ndf.NetworkDefinition) error { gw := ndf.Gateways[len(ndf.Gateways)-1] - _, err := nb.Comms.AddHost("gw", gw.Address, []byte(gw.TlsCertificate), false, true) + _, err := nb.Comms.AddHost("gw", gw.Address, []byte(gw.TlsCertificate), true, true) if err != nil { return errors.Wrap(err, "Failed to add gateway host from NDF") }