ircd/irc/service.go

48 lines
768 B
Go
Raw Normal View History

// vim:ts=4:sts=4:sw=4:noet:tw=72
2021-05-19 07:41:47 +00:00
package irc
import (
"time"
)
type Service struct {
2021-05-19 07:41:47 +00:00
recvq chan *Message
handlers map[string]func(*Message)
}
func NewService() *Service {
2021-05-19 07:41:47 +00:00
recvq := make(chan *Message, 1024)
handlers := make(map[string]func(*Message))
return &Service{recvq: recvq, handlers: handlers}
}
func (svc *Service) Run() {
go svc.dispatcher()
}
2017-08-22 05:23:30 +00:00
func (svc *Service) Stop() {
}
2021-05-19 07:41:47 +00:00
func (svc *Service) Dispatch(msg *Message) {
svc.recvq <- msg
}
2021-05-19 07:41:47 +00:00
func (svc *Service) Handler(cmd string, fn func(*Message)) {
svc.handlers[cmd] = fn
}
func (svc *Service) dispatcher() {
for {
2017-08-22 05:23:30 +00:00
time.Sleep(10 * time.Millisecond)
select {
case msg := <-svc.recvq:
2017-08-22 05:23:30 +00:00
if fn, exists := svc.handlers[msg.Pre]; exists {
fn(msg)
}
default:
}
}
return
}