Preparations for nats clustering

This commit is contained in:
Andreas Neue 2016-08-12 00:13:15 +02:00
parent 00cb057120
commit 3c30adab5c
3 changed files with 55 additions and 6 deletions

View File

@ -86,6 +86,10 @@ func handleCmdJoin(sv *Server, msg *irc.Message) {
}
sv.chUsers[chid][clid] = ""
sv.sendMsg(msg)
// dont proceed further if message was sent by remote origin
if !sv.localOrigin(msg) {
return
}
sv.sendReply(msg.Pre, RPL_TOPIC, msg.Args[0], sv.chTopics[msg.Args[0]])
sv.channelNames(msg.Pre, msg.Args[0])
m, isoper := sv.opers[clid]

30
nats.go Normal file
View File

@ -0,0 +1,30 @@
// vim:ts=4:sts=4:sw=4:noet:tw=72
package ircd
import (
"strings"
"github.com/nats-io/nats"
)
type NatsConnector struct {
natsConn *nats.Conn
subscriptions map[string]*nats.Subscription
}
func NewNatsConnector(servers *string) *NatsConnector {
opts := nats.DefaultOptions
opts.Servers = strings.Split(*servers, ",")
for i, s := range opts.Servers {
opts.Servers[i] = strings.Trim(s, " ")
}
//opts.Secure = *ssl
opts.Secure = false
conn, err := opts.Connect()
if err != nil {
// foo
}
subs := make(map[string]*nats.Subscription)
return &NatsConnector{natsConn: conn, subscriptions: subs}
}

View File

@ -248,7 +248,6 @@ func (sv *Server) addClient(cl Client) {
return
}
sv.clients[clid] = cl
sv.clients[clid] = cl
sv.sendLogon(cl.Name())
sv.connectionsCurrent = float64(len(sv.clients))
cl.Register(true)
@ -305,8 +304,14 @@ func (sv *Server) recvMsg(msg *irc.Message) {
hook.HookFn(sv, msg)
}
// Local delivery of an irc message to channel or client
// Forward an irc message to cluster and deliver locally
func (sv *Server) sendMsg(msg *irc.Message) {
sv.deliverMsg(msg)
sv.forwardMsg(msg)
}
// Local delivery of an irc message to channel or client
func (sv *Server) deliverMsg(msg *irc.Message) {
if strings.HasPrefix(msg.Args[0], "#") {
chid := strings.ToLower(msg.Args[0])
if _, exists := sv.chUsers[chid]; !exists {
@ -332,13 +337,16 @@ func (sv *Server) sendMsg(msg *irc.Message) {
}
}
// Forward an irc message to cluster and deliver locally
// Forward to cluster
func (sv *Server) forwardMsg(msg *irc.Message) {
sv.sendMsg(msg)
if sv.localOrigin(msg) {
// TODO: forward to cluster
println("forward:", msg.String())
}
}
// Send irc reply to local client; drop, if server if client doesnt
// exists locally
// Send irc reply to local client and drop, if client doesnt
// exist locally
func (sv *Server) sendReply(nick, cmd, args, trail string) {
clid := strings.ToLower(nick)
if _, exists := sv.clients[clid]; !exists {
@ -353,6 +361,13 @@ func (sv *Server) sendReply(nick, cmd, args, trail string) {
cl.Receive(irc.M(sv.host, cmd, args, trail))
}
// Check if message's origin is local
func (sv *Server) localOrigin(msg *irc.Message) bool {
_, localClient := sv.clients[strings.ToLower(msg.Pre)]
localServer := (msg.Pre == sv.host)
return localClient || localServer
}
func (sv *Server) sendLogon(nick string) {
sv.sendReply(nick, RPL_WELCOME, "", "Willkommen!")
sv.sendReply(nick, RPL_YOURHOST, "",