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

View File

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