Preparations for service infrastructure
This commit is contained in:
parent
258cb6e4f9
commit
d21b5cccfe
|
@ -0,0 +1,46 @@
|
|||
// vim:ts=4:sts=4:sw=4:noet:tw=72
|
||||
|
||||
package ircd
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"code.dnix.de/an/irc"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
recvq chan *irc.Message
|
||||
handlers map[string]func(*irc.Message)
|
||||
}
|
||||
|
||||
func NewService() *Service {
|
||||
recvq := make(chan *irc.Message, 1024)
|
||||
handlers := make(map[string]func(*irc.Message))
|
||||
return &Service{recvq: recvq, handlers: handlers}
|
||||
}
|
||||
|
||||
func (svc *Service) Run() {
|
||||
go svc.dispatcher()
|
||||
}
|
||||
|
||||
func (svc *Service) Dispatch(msg *irc.Message) {
|
||||
svc.recvq <- msg
|
||||
}
|
||||
|
||||
func (svc *Service) Handler(cmd string, fn func(*irc.Message)) {
|
||||
svc.handlers[cmd] = fn
|
||||
}
|
||||
|
||||
func (svc *Service) dispatcher() {
|
||||
for {
|
||||
select {
|
||||
case msg := <-svc.recvq:
|
||||
if fn, exists := sv.handlers[msg.Pre]; exists {
|
||||
fn(msg)
|
||||
}
|
||||
default:
|
||||
time.Sleep(100 * time.Microsecond)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue