Refactor gateway

This commit is contained in:
Wim 2016-11-08 23:44:16 +01:00
parent a3dd0f1345
commit 14830d9f1c
2 changed files with 41 additions and 31 deletions

View File

@ -17,49 +17,60 @@ type Gateway struct {
ChannelsIn map[string][]string ChannelsIn map[string][]string
ignoreNicks map[string][]string ignoreNicks map[string][]string
Name string Name string
Message chan config.Message
} }
func New(cfg *config.Config, gateway *config.Gateway) error { func New(cfg *config.Config, gateway *config.Gateway) *Gateway {
c := make(chan config.Message)
gw := &Gateway{} gw := &Gateway{}
gw.Name = gateway.Name gw.Name = gateway.Name
gw.Config = cfg gw.Config = cfg
gw.MyConfig = gateway gw.MyConfig = gateway
exists := make(map[string]bool) gw.Message = make(chan config.Message)
for _, br := range append(gateway.In, gateway.Out...) { return gw
if exists[br.Account] { }
continue
} func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
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)
for _, br := range gw.Bridges { for _, br := range gw.Bridges {
err := br.Connect() if br.FullOrigin() == cfg.Account {
if err != nil { return nil
log.Fatalf("Bridge %s failed to start: %v", br.FullOrigin(), err)
} }
for _, channel := range append(gw.ChannelsOut[br.FullOrigin()], gw.ChannelsIn[br.FullOrigin()]...) { }
if exists[br.FullOrigin()+channel] { log.Infof("Starting bridge: %s ", cfg.Account)
continue 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) log.Infof("%s: joining %s", br.FullOrigin(), channel)
br.JoinChannel(channel) br.JoinChannel(channel)
exists[br.FullOrigin()+channel] = true exists[br.FullOrigin()+channel] = true
} }
} }
gw.handleReceive(c)
return nil 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 { for {
select { select {
case msg := <-c: case msg := <-gw.Message:
for _, br := range gw.Bridges { for _, br := range gw.Bridges {
gw.handleMessage(msg, br) gw.handleMessage(msg, br)
} }

View File

@ -49,12 +49,11 @@ func main() {
continue continue
} }
fmt.Printf("starting gateway %#v\n", gw.Name) fmt.Printf("starting gateway %#v\n", gw.Name)
go func(gw config.Gateway) { g := gateway.New(cfg, &gw)
err := gateway.New(cfg, &gw) err := g.Start()
if err != nil { if err != nil {
log.Debugf("starting gateway failed %#v", err) log.Debugf("starting gateway failed %#v", err)
} }
}(gw)
} }
select {} select {}
} }