48 lines
768 B
Go
48 lines
768 B
Go
// vim:ts=4:sts=4:sw=4:noet:tw=72
|
|
|
|
package irc
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type Service struct {
|
|
recvq chan *Message
|
|
handlers map[string]func(*Message)
|
|
}
|
|
|
|
func NewService() *Service {
|
|
recvq := make(chan *Message, 1024)
|
|
handlers := make(map[string]func(*Message))
|
|
return &Service{recvq: recvq, handlers: handlers}
|
|
}
|
|
|
|
func (svc *Service) Run() {
|
|
go svc.dispatcher()
|
|
}
|
|
|
|
func (svc *Service) Stop() {
|
|
}
|
|
|
|
func (svc *Service) Dispatch(msg *Message) {
|
|
svc.recvq <- msg
|
|
}
|
|
|
|
func (svc *Service) Handler(cmd string, fn func(*Message)) {
|
|
svc.handlers[cmd] = fn
|
|
}
|
|
|
|
func (svc *Service) dispatcher() {
|
|
for {
|
|
time.Sleep(10 * time.Millisecond)
|
|
select {
|
|
case msg := <-svc.recvq:
|
|
if fn, exists := svc.handlers[msg.Pre]; exists {
|
|
fn(msg)
|
|
}
|
|
default:
|
|
}
|
|
}
|
|
return
|
|
}
|