1
0
Fork 0
mirror of https://github.com/vbatts/flaming-happiness.git synced 2024-11-23 09:05:40 +00:00

merge the zmq differences to a single command

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2014-11-25 11:42:54 -05:00
parent c54dd0d43b
commit dd835429b3
5 changed files with 93 additions and 71 deletions

View file

@ -19,7 +19,7 @@ if you have ZeroMQ v3 installed, run:
for ZeroMQ v2
go get github.com/vbatts/flaming-happiness/noti2
go get -tags zmq2 github.com/vbatts/flaming-happiness/noti
If you're on Fedora, for dependencies, run:

View file

@ -1,12 +1,11 @@
package main
import (
"bytes"
"encoding/json"
"flag"
zmq "github.com/pebbe/zmq3"
"github.com/vbatts/flaming-happiness/common"
"log"
"github.com/vbatts/flaming-happiness/common"
)
func main() {
@ -16,28 +15,43 @@ func main() {
common.SetIgnores(ignoreChannels)
}
subscriber, _ := zmq.NewSocket(zmq.SUB)
if len(flag.Args()) == 1 {
subscriber.Connect(flag.Args()[0])
if len(flag.Args()) != 1 {
log.Fatalf("provide the zmq_notify publisher! like tcp://example.com:2428")
}
subscriber, err := NewSubscriber(flag.Args()[0])
if err != nil {
log.Fatalf("ERROR: %s", err)
}
if err = subscriber.Connect(); err != nil {
log.Fatalf("ERROR: %s", err)
}
if !quiet {
log.Printf("Connected to [%s]", flag.Args()[0])
}
} else {
log.Fatalf("provide the zmq_notify publisher! like tcp://example.com:2428")
if err = subscriber.SetSubscribe(""); err != nil {
log.Fatalf("ERROR: %s", err)
}
subscriber.SetSubscribe("")
for {
msg, err := subscriber.RecvMessage(0)
msg, err := subscriber.RecvMessageBytes(0)
if err != nil {
log.Fatalf("ERROR: %s", err)
break
}
noti_msg := common.IrcNotify{}
json.Unmarshal(bytes.NewBufferString(msg[0]).Bytes(), &noti_msg)
json.Unmarshal(msg[0], &noti_msg)
go common.Display(noti_msg, linger, quiet)
}
}
type Subscriber interface {
Connect() error
SetSubscribe(string) error
RecvMessage(flag int) (msg []string, err error)
RecvMessageBytes(flag int) (msg [][]byte, err error)
}
var (
linger int64 = 5
quiet bool = false

31
noti/sub_zmq2.go Normal file
View file

@ -0,0 +1,31 @@
// +build zmq2
package main
import "github.com/pebbe/zmq2"
func NewSubscriber(endpoint string) (Subscriber, error) {
sub, err := zmq2.NewSocket(zmq2.SUB)
if err != nil {
return nil, err
}
return v2Subscriber{Socket: sub}, nil
}
type v2Subscriber struct {
Socket *zmq2.Socket
endpoint string
}
func (v2b v2Subscriber) Connect() error {
return v2b.Socket.Connect(v2b.endpoint)
}
func (v2b v2Subscriber) SetSubscribe(topic string) error {
return v2b.Socket.SetSubscribe(topic)
}
func (v2b v2Subscriber) RecvMessage(flag int) (msg []string, err error) {
return v2b.Socket.RecvMessage(zmq2.Flag(flag))
}
func (v2b v2Subscriber) RecvMessageBytes(flag int) (msg [][]byte, err error) {
return v2b.Socket.RecvMessageBytes(zmq2.Flag(flag))
}

31
noti/sub_zmq3.go Normal file
View file

@ -0,0 +1,31 @@
// +build !zmq2
package main
import "github.com/pebbe/zmq3"
func NewSubscriber(endpoint string) (Subscriber, error) {
sub, err := zmq3.NewSocket(zmq3.SUB)
if err != nil {
return nil, err
}
return v3Subscriber{Socket: sub}, nil
}
type v3Subscriber struct {
Socket *zmq3.Socket
endpoint string
}
func (v3b v3Subscriber) Connect() error {
return v3b.Socket.Connect(v3b.endpoint)
}
func (v3b v3Subscriber) SetSubscribe(topic string) error {
return v3b.Socket.SetSubscribe(topic)
}
func (v3b v3Subscriber) RecvMessage(flag int) (msg []string, err error) {
return v3b.Socket.RecvMessage(zmq3.Flag(flag))
}
func (v3b v3Subscriber) RecvMessageBytes(flag int) (msg [][]byte, err error) {
return v3b.Socket.RecvMessageBytes(zmq3.Flag(flag))
}

View file

@ -1,54 +0,0 @@
package main
import (
"bytes"
"encoding/json"
"flag"
zmq "github.com/pebbe/zmq2"
"github.com/vbatts/flaming-happiness/common"
"log"
)
func main() {
flag.Parse()
if len(ignoreChannels) > 0 {
common.SetIgnores(ignoreChannels)
}
subscriber, _ := zmq.NewSocket(zmq.SUB)
if len(flag.Args()) == 1 {
subscriber.Connect(flag.Args()[0])
if !quiet {
log.Printf("Connected to [%s]", flag.Args()[0])
}
} else {
log.Fatalf("provide the zmq_notify publisher! like tcp://example.com:2428")
}
subscriber.SetSubscribe("")
for {
msg, err := subscriber.RecvMessage(0)
if err != nil {
break
}
noti_msg := common.IrcNotify{}
json.Unmarshal(bytes.NewBufferString(msg[0]).Bytes(), &noti_msg)
go common.Display(noti_msg, linger, quiet)
}
}
var (
linger int64 = 5
quiet bool = false
ignoreChannels string
)
func init() {
flag.Int64Var(&linger, "linger",
linger, "time to let the notification linger")
flag.BoolVar(&quiet, "quiet",
false, "less output")
flag.StringVar(&ignoreChannels, "ignore",
"", "comma seperated list of pattern of channels to ignore")
}