use a select in the server commands loop

This commit is contained in:
Jeremy Latt 2014-02-16 22:38:43 -08:00
parent d5c9681a10
commit af5a05f390
2 changed files with 37 additions and 47 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"github.com/jlatt/ergonomadic/irc" "github.com/jlatt/ergonomadic/irc"
"log" "log"
"sync"
) )
func main() { func main() {
@ -18,10 +17,5 @@ func main() {
irc.DEBUG_CHANNEL = config.Debug["channel"] irc.DEBUG_CHANNEL = config.Debug["channel"]
irc.DEBUG_SERVER = config.Debug["server"] irc.DEBUG_SERVER = config.Debug["server"]
irc.NewServer(config) irc.NewServer(config).ReceiveCommands()
// never finishes
wg := sync.WaitGroup{}
wg.Add(1)
wg.Wait()
} }

View File

@ -16,6 +16,7 @@ type Server struct {
channels ChannelNameMap channels ChannelNameMap
clients ClientNameMap clients ClientNameMap
commands chan Command commands chan Command
conns chan net.Conn
ctime time.Time ctime time.Time
motdFile string motdFile string
name string name string
@ -28,6 +29,7 @@ func NewServer(config *Config) *Server {
channels: make(ChannelNameMap), channels: make(ChannelNameMap),
clients: make(ClientNameMap), clients: make(ClientNameMap),
commands: make(chan Command), commands: make(chan Command),
conns: make(chan net.Conn),
ctime: time.Now(), ctime: time.Now(),
motdFile: config.MOTD, motdFile: config.MOTD,
name: config.Name, name: config.Name,
@ -39,7 +41,6 @@ func NewServer(config *Config) *Server {
server.operators[opConf.Name] = opConf.Password server.operators[opConf.Name] = opConf.Password
} }
go server.receiveCommands()
for _, listenerConf := range config.Listeners { for _, listenerConf := range config.Listeners {
go server.listen(listenerConf) go server.listen(listenerConf)
} }
@ -47,8 +48,13 @@ func NewServer(config *Config) *Server {
return server return server
} }
func (server *Server) receiveCommands() { func (server *Server) ReceiveCommands() {
for command := range server.commands { for {
select {
case conn := <-server.conns:
NewClient(server, conn)
case command := <-server.commands:
if DEBUG_SERVER { if DEBUG_SERVER {
log.Printf("%s → %s %+v", command.Client(), server, command) log.Printf("%s → %s %+v", command.Client(), server, command)
} }
@ -59,7 +65,7 @@ func (server *Server) receiveCommands() {
authCommand, ok := command.(AuthServerCommand) authCommand, ok := command.(AuthServerCommand)
if !ok { if !ok {
client.Destroy() client.Destroy()
return continue
} }
authCommand.HandleAuthServer(server) authCommand.HandleAuthServer(server)
@ -67,7 +73,7 @@ func (server *Server) receiveCommands() {
regCommand, ok := command.(RegServerCommand) regCommand, ok := command.(RegServerCommand)
if !ok { if !ok {
client.Destroy() client.Destroy()
return continue
} }
regCommand.HandleRegServer(server) regCommand.HandleRegServer(server)
@ -75,13 +81,14 @@ func (server *Server) receiveCommands() {
serverCommand, ok := command.(ServerCommand) serverCommand, ok := command.(ServerCommand)
if !ok { if !ok {
client.Reply(ErrUnknownCommand(server, command.Name())) client.Reply(ErrUnknownCommand(server, command.Name()))
return continue
} }
client.Touch() client.Touch()
serverCommand.HandleServer(server) serverCommand.HandleServer(server)
} }
} }
} }
}
func (server *Server) InitPhase() Phase { func (server *Server) InitPhase() Phase {
if server.password == "" { if server.password == "" {
@ -123,9 +130,7 @@ func (s *Server) listen(config ListenerConfig) {
log.Print("Server.Accept: ", conn.RemoteAddr()) log.Print("Server.Accept: ", conn.RemoteAddr())
} }
s.commands <- &NewClientCommand{ s.conns <- conn
conn: conn,
}
} }
} }
@ -537,12 +542,3 @@ func (msg *NoticeCommand) HandleServer(server *Server) {
} }
target.Reply(RplNotice(client, target, msg.message)) target.Reply(RplNotice(client, target, msg.message))
} }
type NewClientCommand struct {
BaseCommand
conn net.Conn
}
func (msg *NewClientCommand) HandleServer(server *Server) {
NewClient(server, conn)
}