Exemplo n.º 1
0
func main() {
	pub := goczmq.NewPubChanneler("tcp://*:5000")
	defer pub.Destroy()

	update := make(chan *cell.UpdateRequest)
	go startInputServer(update)

	tick := time.Tick(time.Second)
	r := cell.Run(cell.RandomField(128, 128), tick, update)
	seq := int32(0)
	for f := range r {
		pf := cell.ToProto(f)
		pf.Seq = seq

		data, err := proto.Marshal(pf)
		if err != nil {
			log.Fatal("Marshaling error: %q\n\tfor Field %#v\n", err.Error(), *f)
		}
		pub.SendChan <- [][]byte{data}
		seq++
	}
}
Exemplo n.º 2
0
func main() {
	pub := os.Getenv("PUB_PORT_5000_TCP")
	if pub == "" {
		log.Fatal("Missing PUB_PORT_5000_TCP environment variable")
	}
	rep := os.Getenv("PUB_PORT_5001_TCP")
	if rep == "" {
		log.Fatal("Missing PUB_PORT_5001_TCP environment variable")
	}
	reqL.Lock()
	var err error
	req, err = goczmq.NewReq(rep)
	reqL.Unlock()
	if err != nil {
		log.Fatal("Error connecting to REP %q: %q\n", rep, err.Error())
	}

	fields[0] = cell.RandomField(128, 128)
	go func() {
		sub := goczmq.NewSubChanneler(pub, "" /* all messages, no topic filtering */)
		defer sub.Destroy()

		for msg := range sub.RecvChan {
			if len(msg) != 1 {
				log.Printf(
					"Message had unexpected number of frames: %d, want 1.\n",
					len(msg))
				continue
			}
			data := msg[0]

			pf := &cell.FieldProto{}
			if err := proto.Unmarshal(data, pf); err != nil {
				log.Printf(
					"Error unmarshalling message:\n\t%q,\n\t%#v\n",
					err.Error(),
					msg)
				continue
			}
			seq := int(pf.Seq) % cacheSize
			if seq < 0 {
				seq = seq + cacheSize
			}

			f, err := cell.FromProto(pf)
			if err != nil {
				log.Printf("Got invalid Field:\n\t%q,\n\t%#v\n", err.Error(), f)
				continue
			}

			cond.L.Lock()
			curr = seq
			fields[curr] = f
			cond.Broadcast()
			cond.L.Unlock()
		}
	}()

	http.HandleFunc("/socket", socketHandler)
	http.HandleFunc("/", viewHandler)
	http.HandleFunc("/field/", fieldHandler)
	http.ListenAndServe(":8080", nil)
}