From 4d3f52102f34d165445554087c7ed86eeb64806e Mon Sep 17 00:00:00 2001 From: erinn Date: Sat, 27 Oct 2018 02:17:35 -0700 Subject: [PATCH] allow retrieving handlers from an aif so we can merge them in cwtch peers --- application/application_factory.go | 16 ++++++++++++++++ application/examples/echobot/main.go | 10 ++++------ connection/handler.go | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/application/application_factory.go b/application/application_factory.go index 1710915..1361e8e 100644 --- a/application/application_factory.go +++ b/application/application_factory.go @@ -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) diff --git a/application/examples/echobot/main.go b/application/examples/echobot/main.go index d1af675..f1bb4a1 100644 --- a/application/examples/echobot/main.go +++ b/application/examples/echobot/main.go @@ -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 }) } diff --git a/connection/handler.go b/connection/handler.go index 9a6c42b..f167b48 100644 --- a/connection/handler.go +++ b/connection/handler.go @@ -27,4 +27,4 @@ type Handler interface { OnOpenChannelRequest(ctype string) (channels.Handler, error) GetSupportedChannelTypes() []string -} +} \ No newline at end of file