57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
could not parse "irc.go": /home/an/src/iqcomm_/src/irc/irc.go:128:20: expected boolean expression, found simple statement (missing parentheses around composite literal?)
|
|
// vim:ts=4:sts=4:sw=4:noet:tw=72
|
|
|
|
package ircd
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"code.dnix.de/an/irc"
|
|
|
|
"github.com/nats-io/nats"
|
|
)
|
|
|
|
type ClusterConnector struct {
|
|
conn *nats.Conn
|
|
subs map[string]*nats.Subscription
|
|
}
|
|
|
|
func NewClusterConnector(servers string, ssl bool) *NatsConnector {
|
|
opts := nats.DefaultOptions
|
|
opts.Servers = strings.Split(servers, ",")
|
|
for i, s := range opts.Servers {
|
|
opts.Servers[i] = strings.Trim(s, " ")
|
|
}
|
|
opts.Secure = ssl
|
|
conn, err := opts.Connect()
|
|
if err != nil {
|
|
// foo
|
|
}
|
|
subs := make(map[string]*nats.Subscription)
|
|
return &NatsConnector{conn: conn, subs: subs}
|
|
}
|
|
|
|
func (cc *ClusterConnector) Subscribe(subj string, ch chan *irc.Message) {
|
|
if _, exists := c.subs[subj]; exists {
|
|
return
|
|
}
|
|
sub, err := c.natsConn.Subscribe(subj, func(n *nats.Msg) {
|
|
m := irc.Parse(string(n.Data))
|
|
ch <- m
|
|
})
|
|
if err != nil {
|
|
c.subs[subj] = sub
|
|
}
|
|
|
|
}
|
|
|
|
func (cc *ClusterConnector) Unsubscribe(subj string) {
|
|
cc.conn.Unsubscribe(subj)
|
|
delete(cc.subs, subj)
|
|
}
|
|
|
|
func (cc *ClusterConnector) Publish(msg *irc.Message) {
|
|
subj := strings.ToLower(msg.Pre)
|
|
cc.conn.Publish(subj, []bytes(msg.String()))
|
|
}
|