示例#1
0
func main() {
	agent, _ := zap.NewZapAgent()
	go agent.Run(&zap.OpenAccess{})
	defer agent.Destroy()

	broker_endpoint := "inproc://fancy-req"
	service := "echo"

	server_cert := czmq.NewCert()

	b, err := mdp.NewBroker(broker_endpoint)
	if err != nil {
		log.Fatalf("NewBroker(%s) failed: %s", broker_endpoint, err)
	}
	b.CurveCertificate = server_cert
	b.Bind("inproc://fancy-dealer")
	port, err := b.Bind("tcp://*:*")
	log.Printf("Broker available at binding: tcp://127.0.0.1:%d", port)
	go b.Run()

	// Start a worker.
	local_endpoint := fmt.Sprintf("tcp://127.0.0.1:%d", port)
	w := mdp.NewWorker(local_endpoint, service)
	w.CurveServerPublicKey = server_cert.PublicText()
	go w.Run(&echoWorker{})

	RunHTTP(":8111", "/zws/1.0")
}
示例#2
0
func (w *Worker) ensure_connected() error {
	if w.sock != nil {
		return nil
	}

	w.sock = newSock(czmq.Dealer)

	// Setup CURVE if a key is set.
	if len(w.CurveServerPublicKey) > 0 {
		w.sock.SetCurveServerkey(w.CurveServerPublicKey)
	}
	if w.CurveCertificate == nil {
		w.CurveCertificate = czmq.NewCert()
	}
	w.CurveCertificate.Apply(w.sock)

	log.WithFields(log.Fields{"endpoint": w.broker}).Info("Connecting")
	err := w.sock.Connect(w.broker)
	if err != nil {
		w.sock.Destroy()
		w.sock = nil
		return err
	}

	w.poller, err = czmq.NewPoller(w.sock)
	if err != nil {
		w.sock.Destroy()
		return err
	}

	err = w.sendReady()
	if err != nil {
		return err
	}
	return nil
}