Tidied up a little

This commit is contained in:
Harry Jeffery 2013-08-23 15:23:27 +01:00
parent af4066c31b
commit ee6dcc8b37
2 changed files with 14 additions and 30 deletions

View File

@ -20,13 +20,9 @@ func main() {
log.Printf("Rosella Initialising.") log.Printf("Rosella Initialising.")
//Init rosella itself //Init rosella itself
server, err := NewServer() server := NewServer()
if err != nil {
panic(err)
}
server.name = *serverName server.name = *serverName
server.Start() server.Run()
tlsConfig := new(tls.Config) tlsConfig := new(tls.Config)

View File

@ -16,9 +16,7 @@ import (
type Server struct { type Server struct {
eventChan chan Event eventChan chan Event
running bool running bool
name string name string
clientMap map[string]*Client //Map of nicks → clients clientMap map[string]*Client //Map of nicks → clients
channelMap map[string]*Channel //Map of channel names → channels channelMap map[string]*Channel //Map of channel names → channels
} }
@ -68,18 +66,19 @@ var (
channelRegexp = regexp.MustCompile(`^#[a-z0-9_\-]+$`) channelRegexp = regexp.MustCompile(`^#[a-z0-9_\-]+$`)
) )
func NewServer() (*Server, error) { func NewServer() *Server {
return &Server{eventChan: make(chan Event), return &Server{eventChan: make(chan Event),
name: "rosella", name: "rosella",
clientMap: make(map[string]*Client), clientMap: make(map[string]*Client),
channelMap: make(map[string]*Channel)}, nil channelMap: make(map[string]*Channel)}
} }
func (s *Server) Start() { func (s *Server) Run() {
if s.running == false { go func() {
s.running = true for {
go s.serverThread() s.handleEvent(<-s.eventChan)
} }
}()
} }
func (s *Server) HandleConnection(conn net.Conn) { func (s *Server) HandleConnection(conn net.Conn) {
@ -93,15 +92,6 @@ func (s *Server) HandleConnection(conn net.Conn) {
go client.clientThread() go client.clientThread()
} }
func (s *Server) serverThread() {
for {
select {
case e := <-s.eventChan:
s.handleEvent(e)
}
}
}
func (s *Server) handleEvent(e Event) { func (s *Server) handleEvent(e Event) {
fields := strings.Fields(e.input) fields := strings.Fields(e.input)
@ -245,7 +235,7 @@ func (s *Server) handleEvent(e Event) {
} }
default: default:
log.Printf("Unknown command: %q", fields[0]) log.Printf("Unknown command: %q", command)
} }
} }
@ -416,8 +406,6 @@ func (c *Client) reply(code int, args ...string) {
c.outputChan <- fmt.Sprintf(":%s 462 :You need a valid nick first", c.server.name) c.outputChan <- fmt.Sprintf(":%s 462 :You need a valid nick first", c.server.name)
case errNoSuchNick: case errNoSuchNick:
c.outputChan <- fmt.Sprintf("%s 401 :No such nick/channel", c.server.name) c.outputChan <- fmt.Sprintf("%s 401 :No such nick/channel", c.server.name)
default:
log.Printf("Client.reply() with unknown reply code: %d", code)
} }
} }