Skip to content
Snippets Groups Projects
Commit 1571dbab authored by Rick Carback's avatar Rick Carback
Browse files

Modify login and separate session loading to ensure that all listeners are...

Modify login and separate session loading to ensure that all listeners are lisening before message receiver starts processing messages
parent 658c404d
No related branches found
No related tags found
No related merge requests found
...@@ -436,51 +436,57 @@ func (cl *Client) Register(preCan bool, registrationCode, nick, email string) (* ...@@ -436,51 +436,57 @@ func (cl *Client) Register(preCan bool, registrationCode, nick, email string) (*
return UID, nil return UID, nil
} }
// Logs in user and sets session on client object // LoadSession loads the session object for the UID
// returns the nickname or error if login fails func (cl *Client) LoadSession(UID *id.User) error {
func (cl *Client) Login(UID *id.User) (string, error) {
session, err := user.LoadSession(cl.storage, UID) session, err := user.LoadSession(cl.storage, UID)
if session == nil {
return "", errors.New("Unable to load session: " + err.Error())
}
(cl.comm).(*io.Messaging).SendGateway =
id.NewNodeFromBytes(cl.ndf.Nodes[0].ID).NewGateway()
(cl.comm).(*io.Messaging).ReceiveGateway =
id.NewNodeFromBytes(cl.ndf.Nodes[len(cl.ndf.Nodes)-1].ID).NewGateway()
if err != nil { if err != nil {
err = errors.New(fmt.Sprintf("Login: Could not login: %s", err = errors.New(fmt.Sprintf("Login: Could not login: %s",
err.Error())) err.Error()))
globals.Log.ERROR.Printf(err.Error()) globals.Log.ERROR.Printf(err.Error())
return "", err return err
}
if session == nil {
return errors.New("Unable to load session: " + err.Error())
} }
cl.session = session cl.session = session
return nil
}
pollWaitTimeMillis := 1000 * time.Millisecond // Logs in user and sets session on client object
// TODO Don't start the message receiver if it's already started. // returns the nickname or error if login fails
// Should be a pretty rare occurrence except perhaps for mobile. func (cl *Client) Login(UID *id.User) (string, error) {
go cl.comm.MessageReceiver(session, pollWaitTimeMillis) (cl.comm).(*io.Messaging).SendGateway =
id.NewNodeFromBytes(cl.ndf.Nodes[0].ID).NewGateway()
(cl.comm).(*io.Messaging).ReceiveGateway =
id.NewNodeFromBytes(cl.ndf.Nodes[len(cl.ndf.Nodes)-1].ID).NewGateway()
// Initialize UDB and nickname "bot" stuff here // Initialize UDB and nickname "bot" stuff here
bots.InitBots(cl.session, cl.comm, cl.topology) bots.InitBots(cl.session, cl.comm, cl.topology)
// Initialize Rekey listeners // Initialize Rekey listeners
rekey.InitRekey(cl.session, cl.comm, cl.topology) rekey.InitRekey(cl.session, cl.comm, cl.topology)
email := session.GetCurrentUser().Email pollWaitTimeMillis := 1000 * time.Millisecond
// TODO Don't start the message receiver if it's already started.
// Should be a pretty rare occurrence except perhaps for mobile.
go cl.comm.MessageReceiver(cl.session, pollWaitTimeMillis)
email := cl.session.GetCurrentUser().Email
if email != "" { if email != "" {
err = cl.registerForUserDiscovery(email) globals.Log.INFO.Printf("Registering user as %s", email)
err := cl.registerForUserDiscovery(email)
if err != nil { if err != nil {
globals.Log.ERROR.Printf( globals.Log.ERROR.Printf(
"Unable to register with UDB: %s", err) "Unable to register with UDB: %s", err)
return "", err return "", err
} }
globals.Log.INFO.Printf("Registered!")
} }
return session.GetCurrentUser().Nick, nil return cl.session.GetCurrentUser().Nick, nil
} }
// Send prepares and sends a message to the cMix network // Send prepares and sends a message to the cMix network
......
...@@ -189,6 +189,12 @@ func sessionInitialization() (*id.User, *api.Client) { ...@@ -189,6 +189,12 @@ func sessionInitialization() (*id.User, *api.Client) {
globals.Log.INFO.Printf("Skipped Registration, user: %v", uid) globals.Log.INFO.Printf("Skipped Registration, user: %v", uid)
} }
err = client.LoadSession(uid)
if err != nil {
globals.Log.FATAL.Panicf("Could not load session: %v", err)
}
return uid, client return uid, client
} }
...@@ -342,6 +348,7 @@ var rootCmd = &cobra.Command{ ...@@ -342,6 +348,7 @@ var rootCmd = &cobra.Command{
if len(keyParams) == 5 { if len(keyParams) == 5 {
setKeyParams(client) setKeyParams(client)
} }
// Set up the listeners for both of the types the client needs for // Set up the listeners for both of the types the client needs for
// the integration test // the integration test
// Normal text messages // Normal text messages
...@@ -522,7 +529,7 @@ func init() { ...@@ -522,7 +529,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&userEmail, rootCmd.PersistentFlags().StringVarP(&userEmail,
"email", "E", "email", "E",
"default@default.com", "",
"Email to register for User Discovery") "Email to register for User Discovery")
rootCmd.PersistentFlags().StringVar(&userNick, rootCmd.PersistentFlags().StringVar(&userNick,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment