Created
December 16, 2012 11:29
-
-
Save paddycarver/4306474 to your computer and use it in GitHub Desktop.
A program to test that Wendy functions as expected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/rand" | |
"flag" | |
"fmt" | |
"secondbit.org/wendy" | |
"time" | |
) | |
type Callback struct { | |
Client wendy.NodeID | |
} | |
func (c *Callback) OnError(err error) { | |
panic(c.Client.String() + ": " + err.Error()) | |
} | |
func (c *Callback) OnDeliver(msg wendy.Message) { | |
fmt.Printf("%s: Delivering %s\n", c.Client, msg.Key) | |
} | |
func (c *Callback) OnForward(msg *wendy.Message, next wendy.NodeID) bool { | |
fmt.Printf("%s: Forwarding %s to %s\n", c.Client, msg, next) | |
return true | |
} | |
func (c *Callback) OnNewLeaves(leaves []*wendy.Node) { | |
fmt.Printf("%s: Leafset updated.\n", c.Client) | |
leaves_string := "" | |
for _, leaf := range leaves { | |
leaves_string = leaves_string + leaf.ID.String() + ", " | |
} | |
fmt.Printf("%s: Leaves: \033[4;33m%s\033[0m\n", c.Client, leaves_string) | |
} | |
func (c *Callback) OnNodeJoin(node wendy.Node) { | |
fmt.Printf("%s: Node joined: %s\n", c.Client, node.ID) | |
} | |
func (c *Callback) OnNodeExit(node wendy.Node) { | |
fmt.Printf("%s: Node left: %s\n", c.Client, node.ID) | |
} | |
func (c *Callback) OnHeartbeat(node wendy.Node) { | |
fmt.Printf("%s: Received heartbeat from %s\n", c.Client, node.ID) | |
} | |
func makeCluster(port int, credentials string) *wendy.Cluster { | |
b := make([]byte, 16) | |
_, err := rand.Read(b) | |
if err != nil { | |
panic(err.Error()) | |
} | |
id, err := wendy.NodeIDFromBytes(b) | |
if err != nil { | |
panic(err.Error()) | |
} | |
node := wendy.NewNode(id, "127.0.0.1", "127.0.0.1", "macbookpro", port) | |
cluster := wendy.NewCluster(node, wendy.Passphrase(credentials)) | |
cluster.SetHeartbeatFrequency(10) | |
cluster.SetLogLevel(wendy.LogLevelDebug) | |
return cluster | |
} | |
func main() { | |
var port int | |
flag.IntVar(&port, "port", 8080, "The port to listen on.") | |
var target, credentials string | |
var targetPort int | |
flag.StringVar(&target, "target", "127.0.0.1", "The target to connect to.") | |
flag.IntVar(&targetPort, "targetPort", -1, "The port the target to connect to is listening on.") | |
flag.StringVar(&credentials, "credentials", "test credentials", "The credentials to use when joining the cluster.") | |
flag.Parse() | |
c := makeCluster(port, credentials) | |
c.RegisterCallback(&Callback{Client: c.ID()}) | |
go func(){ | |
defer c.Stop() | |
err := c.Listen() | |
fmt.Println("Listen stopped blocking") | |
if err != nil { | |
fmt.Println(err.Error()) | |
panic(err.Error()) | |
} | |
}() | |
time.Sleep(1 * time.Second) | |
if targetPort != -1 { | |
err := c.Join(target, targetPort) | |
if err != nil { | |
panic(err) | |
} | |
} | |
go func() { | |
for { | |
time.Sleep(20 * time.Second) | |
b := make([]byte, 16) | |
_, err := rand.Read(b) | |
if err != nil { | |
panic(err.Error()) | |
} | |
key, err := wendy.NodeIDFromBytes(b) | |
if err != nil { | |
panic(err.Error()) | |
} | |
msg := c.NewMessage(byte(16), key, []byte("Testing this out.")) | |
err = c.Send(msg) | |
if err != nil { | |
panic(err.Error()) | |
} | |
} | |
}() | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment