diff --git a/ui/account.go b/ui/account.go index 0cd61c8..f8dcce8 100644 --- a/ui/account.go +++ b/ui/account.go @@ -11,11 +11,12 @@ import ( ) type AccountTab struct { - Config *config.AccountConfig - Worker worker.Worker - Parent *UIState - logger *log.Logger - counter int + Config *config.AccountConfig + Worker worker.Worker + Parent *UIState + logger *log.Logger + counter int + callbacks map[types.WorkerMessage]func(msg types.WorkerMessage) } func NewAccountTab(conf *config.AccountConfig, @@ -26,13 +27,21 @@ func NewAccountTab(conf *config.AccountConfig, return nil, err } go work.Run() - work.PostAction(types.Configure{Config: conf}) - work.PostAction(types.Connect{}) - return &AccountTab{ - Config: conf, - Worker: work, - logger: logger, - }, nil + acc := &AccountTab{ + Config: conf, + Worker: work, + logger: logger, + callbacks: make(map[types.WorkerMessage]func(msg types.WorkerMessage)), + } + acc.postAction(types.Configure{Config: conf}, nil) + acc.postAction(types.Connect{}, func(msg types.WorkerMessage) { + if _, ok := msg.(types.Ack); ok { + acc.logger.Println("Connected.") + } else { + acc.logger.Println("Connection failed.") + } + }) + return acc, nil } func (acc *AccountTab) Name() string { @@ -62,13 +71,21 @@ func (acc *AccountTab) GetChannel() chan types.WorkerMessage { return acc.Worker.GetMessages() } -func (acc *AccountTab) postAction(msg types.WorkerMessage) { +func (acc *AccountTab) postAction(msg types.WorkerMessage, + cb func(msg types.WorkerMessage)) { + acc.logger.Printf("-> %T\n", msg) acc.Worker.PostAction(msg) + if cb != nil { + acc.callbacks[msg] = cb + } } func (acc *AccountTab) HandleMessage(msg types.WorkerMessage) { acc.logger.Printf("<- %T\n", msg) + if cb, ok := acc.callbacks[msg.InResponseTo()]; ok { + cb(msg) + } switch msg.(type) { case types.Ack: // no-op @@ -77,10 +94,10 @@ func (acc *AccountTab) HandleMessage(msg types.WorkerMessage) { acc.logger.Println("Approving certificate") acc.postAction(types.Ack{ Message: types.RespondTo(msg), - }) + }, nil) default: acc.postAction(types.Unsupported{ Message: types.RespondTo(msg), - }) + }, nil) } }