diff options
author | Wim <wim@42.be> | 2016-11-08 23:44:16 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2016-11-08 23:44:16 +0100 |
commit | 14830d9f1c4e9435ee7a15e0839876ad1f4d1a9f (patch) | |
tree | 66905e31b8a25a66d069998116376e03a5ab80b7 | |
parent | a3dd0f1345fae3d7828ed0c82d6a7c4b610d0983 (diff) | |
download | matterbridge-msglm-14830d9f1c4e9435ee7a15e0839876ad1f4d1a9f.tar.gz matterbridge-msglm-14830d9f1c4e9435ee7a15e0839876ad1f4d1a9f.tar.bz2 matterbridge-msglm-14830d9f1c4e9435ee7a15e0839876ad1f4d1a9f.zip |
Refactor gateway
-rw-r--r-- | gateway/gateway.go | 61 | ||||
-rw-r--r-- | matterbridge.go | 11 |
2 files changed, 41 insertions, 31 deletions
diff --git a/gateway/gateway.go b/gateway/gateway.go index 13b6be1b..07fcdafc 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -17,49 +17,60 @@ type Gateway struct { ChannelsIn map[string][]string ignoreNicks map[string][]string Name string + Message chan config.Message } -func New(cfg *config.Config, gateway *config.Gateway) error { - c := make(chan config.Message) +func New(cfg *config.Config, gateway *config.Gateway) *Gateway { gw := &Gateway{} gw.Name = gateway.Name gw.Config = cfg gw.MyConfig = gateway - exists := make(map[string]bool) - for _, br := range append(gateway.In, gateway.Out...) { - if exists[br.Account] { - continue - } - log.Infof("Starting bridge: %s channel: %s", br.Account, br.Channel) - gw.Bridges = append(gw.Bridges, bridge.New(cfg, &br, c)) - exists[br.Account] = true - } - gw.mapChannels() - //TODO fix mapIgnores - //gw.mapIgnores() - exists = make(map[string]bool) + gw.Message = make(chan config.Message) + return gw +} + +func (gw *Gateway) AddBridge(cfg *config.Bridge) error { for _, br := range gw.Bridges { - err := br.Connect() - if err != nil { - log.Fatalf("Bridge %s failed to start: %v", br.FullOrigin(), err) + if br.FullOrigin() == cfg.Account { + return nil } - for _, channel := range append(gw.ChannelsOut[br.FullOrigin()], gw.ChannelsIn[br.FullOrigin()]...) { - if exists[br.FullOrigin()+channel] { - continue - } + } + log.Infof("Starting bridge: %s ", cfg.Account) + br := bridge.New(gw.Config, cfg, gw.Message) + gw.Bridges = append(gw.Bridges, br) + err := br.Connect() + if err != nil { + return fmt.Errorf("Bridge %s failed to start: %v", br.FullOrigin(), err) + } + exists := make(map[string]bool) + for _, channel := range append(gw.ChannelsOut[br.FullOrigin()], gw.ChannelsIn[br.FullOrigin()]...) { + if !exists[br.FullOrigin()+channel] { log.Infof("%s: joining %s", br.FullOrigin(), channel) br.JoinChannel(channel) exists[br.FullOrigin()+channel] = true } } - gw.handleReceive(c) return nil } -func (gw *Gateway) handleReceive(c chan config.Message) { +func (gw *Gateway) Start() error { + gw.mapChannels() + for _, br := range append(gw.MyConfig.In, gw.MyConfig.Out...) { + err := gw.AddBridge(&br) + if err != nil { + return err + } + } + //TODO fix mapIgnores + //gw.mapIgnores() + go gw.handleReceive() + return nil +} + +func (gw *Gateway) handleReceive() { for { select { - case msg := <-c: + case msg := <-gw.Message: for _, br := range gw.Bridges { gw.handleMessage(msg, br) } diff --git a/matterbridge.go b/matterbridge.go index 28a76fa1..0096b0a0 100644 --- a/matterbridge.go +++ b/matterbridge.go @@ -49,12 +49,11 @@ func main() { continue } fmt.Printf("starting gateway %#v\n", gw.Name) - go func(gw config.Gateway) { - err := gateway.New(cfg, &gw) - if err != nil { - log.Debugf("starting gateway failed %#v", err) - } - }(gw) + g := gateway.New(cfg, &gw) + err := g.Start() + if err != nil { + log.Debugf("starting gateway failed %#v", err) + } } select {} } |