2012-12-02 00:06:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-12-02 13:41:05 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2012-12-02 00:06:23 +00:00
|
|
|
"log"
|
2012-12-02 13:41:05 +00:00
|
|
|
"time"
|
2012-12-02 00:06:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/meatballhat/box-o-sand/sylvilagus/go/sylvilagus"
|
2012-12-02 13:41:05 +00:00
|
|
|
"github.com/streadway/amqp"
|
2012-12-02 00:06:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2012-12-02 13:41:05 +00:00
|
|
|
connection, err := amqp.Dial(sylvilagus.AMQP_URI)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to connect!:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer connection.Close()
|
|
|
|
|
|
|
|
channel, err := sylvilagus.CreateRPCTopology(connection)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to build topology!:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pings, err := channel.Consume("ping", "ping", false, false, false, false, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to start consuming!:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
quit := make(chan bool)
|
|
|
|
|
|
|
|
go func(quit chan bool) {
|
|
|
|
log.Println("Waiting for RPC calls...")
|
|
|
|
|
|
|
|
for ping := range pings {
|
|
|
|
if err = ping.Ack(false); err == nil {
|
|
|
|
log.Println("Received API call... replying...")
|
|
|
|
|
|
|
|
pingInst := &sylvilagus.Ping{}
|
|
|
|
err = json.Unmarshal(ping.Body, pingInst)
|
|
|
|
if err == nil {
|
|
|
|
msg := amqp.Publishing{
|
|
|
|
DeliveryMode: amqp.Persistent,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
ContentType: "text/plain",
|
|
|
|
Body: []byte(fmt.Sprintf("Pong! %s", pingInst.Time)),
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.Publish("", ping.ReplyTo, false, false, msg)
|
|
|
|
} else {
|
|
|
|
log.Println("Failed to json.Unmarshal the ping!:", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Println("Failed to ACK the ping!:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(quit)
|
|
|
|
|
|
|
|
<-quit
|
2012-12-02 00:06:23 +00:00
|
|
|
}
|