Ejemplo n.º 1
0
func main() {
	// Connect to the local bosswave router
	cl, err := bw.Connect("localhost:28589")
	if err != nil {
		bail(err)
	}
	// Tell the local router who we are. Remember that in BW,
	// your identity is key
	us, err := cl.SetEntityFile("balingwire.key")
	if err != nil {
		bail(err)
	}

	// As we are going to do a bunch of publishes on the same
	// URI, it is faster to calculate the permissions once, and reuse
	// them instead of doing it on every message (using AutoChain)
	uri := "castle.bw2.io/hamilton/+/accel"
	// This says "find me any chain that gives me Publish and Consume on
	// the given url"
	pac, e := cl.BuildAnyChain(uri, "PC", us)
	if pac == nil {
		fmt.Println("Could not get permissions: ", e)
		os.Exit(1)
	}
	// Note that if permissions exist, but your local router does not
	// know about them, the above might fail. Simply let your local
	// router know about them by executing
	// bw2 bc --uri <uri> --to <our vk> --permissions "PC" --router <RTR>
	// where RTR is a router that has the missing dots to build a chain
	// for example castle.bw2.io
	// You can refer to a router by its key or by DNS name that will be
	// resolved to a key.
	fmt.Println("using pac: ", pac.Hash)

	msgchan := listenForHamiltonPackets()
	go genDemoPackets(msgchan)
	for {
		m := <-msgchan
		// m is a compliant hamilton accelerometer PO
		err = cl.Publish(&bw.PublishParams{
			// The URI we want to publish to
			URI: "castle.bw2.io/hamilton/" + m.serial + "/accel",
			// The access chain that gives us permissions
			PrimaryAccessChain: pac.Hash,
			// This says include the full PAC in the message
			// It might soon become the default
			ElaboratePAC:   bw.ElaborateFull,
			PayloadObjects: m.POs,
		})
		if err != nil {
			fmt.Println("Error publishing: ", err.Error())
		} else {
			fmt.Println("Published ok")
		}
	}
}
Ejemplo n.º 2
0
func main() {
	var err error

	var chandle codec.Handle = new(codec.MsgpackHandle)

	var cl *bw.BW2Client
	cl, err = bw.Connect("localhost:28589")
	exitOnError(err, "Could not connect")

	var vk string
	vk, err = cl.SetEntityFile("chairb.key")
	exitOnError(err, "Could not use entity")

	var dchain *bw.SimpleChain
	dchain, err = cl.BuildAnyChain(RAWDATAURI, "CP", vk)
	exitOnError(err, "Could not build DOT chain")

	var msgchan chan *bw.SimpleMessage
	msgchan, err = cl.Subscribe(&bw.SubscribeParams{
		URI:                RAWDATAURI,
		PrimaryAccessChain: dchain.Hash,
		ElaboratePAC:       "full",
	})

	var msg *bw.SimpleMessage
	var cid string
	var cm ChairMessage
	for msg = range msgchan {
		cid, cm, err = parseIncomingMessage(msg, chandle)
		if err == nil {
			fmt.Printf("Received message for chair %v: %v\n", cid, cm)
		} else {
			fmt.Printf("Could not handle incoming message: %v\n", err)
		}
	}
}