allow retrieving handlers from an aif so we can merge them in cwtch peers

This commit is contained in:
erinn 2018-10-27 02:17:35 -07:00
parent ad9d0efb02
commit 4d3f52102f
3 changed files with 21 additions and 7 deletions

View File

@ -27,6 +27,22 @@ func (af *ApplicationInstanceFactory) AddHandler(ctype string, chandler func(*Ap
af.handlerMap[ctype] = chandler
}
func (af *ApplicationInstanceFactory) GetHandlers() []string {
keys := make([]string, len(af.handlerMap))
i := 0
for k := range af.handlerMap {
keys[i] = k
i++
}
return keys
}
func (af *ApplicationInstanceFactory) GetHandler(ctype string) func(*ApplicationInstance) func() channels.Handler {
return af.handlerMap[ctype]
}
// GetApplicationInstance builds a new application instance using a connection as a base.
func (af *ApplicationInstanceFactory) GetApplicationInstance(rc *connection.Connection) *ApplicationInstance {
rai := new(ApplicationInstance)

View File

@ -40,9 +40,7 @@ func (ebi *EchoBotInstance) OpenInbound() {
func (ebi *EchoBotInstance) ChatMessage(messageID uint32, when time.Time, message string) bool {
log.Printf("message from %v - %v", ebi.rai.RemoteHostname, message)
go ebi.ra.Broadcast(func(rai *application.ApplicationInstance) {
ebi.SendChatMessage(rai, ebi.rai.RemoteHostname+" "+message)
})
ebi.SendChatMessage(ebi.rai, ebi.rai.RemoteHostname+" "+message)
return true
}
@ -51,14 +49,14 @@ func (ebi *EchoBotInstance) ChatMessageAck(messageID uint32, accepted bool) {
}
func (ebi *EchoBotInstance) SendChatMessage(rai *application.ApplicationInstance, message string) {
rai.Connection.Do(func() error {
channel := rai.Connection.Channel("im.ricochet.chat", channels.Outbound)
ebi.rai.Connection.Do(func() error {
channel := ebi.rai.Connection.Channel("im.ricochet.chat", channels.Outbound)
if channel != nil {
chatchannel, ok := channel.Handler.(*channels.ChatChannel)
if ok {
chatchannel.SendMessage(message)
}
}
} //TODO: else?
return nil
})
}

View File

@ -27,4 +27,4 @@ type Handler interface {
OnOpenChannelRequest(ctype string) (channels.Handler, error)
GetSupportedChannelTypes() []string
}
}