Esempio n. 1
0
func (h *HelloHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	name, ok := vars["name"]
	if !ok {
		http.Error(w, "no name", http.StatusBadRequest)
		return
	}

	res, err := beehive.Sync(context.TODO(), HelloHTTP{Name: name})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	fmt.Fprintf(w, "hello %s (%d)\n", name, res.(int))
}
func (httpReceiver *HTTPReceiver) ServeHTTP(
	responseWriter http.ResponseWriter,
	httpRequest *http.Request) {
	logger.Info.Printf("[HTTPReceiver] ServeHTTP %s %s \n",
		httpRequest.Method,
		httpRequest.URL)
	vars := mux.Vars(httpRequest)
	destinationBee, ok := vars["destinationBee"]
	if !ok {
		BadRequest(responseWriter, "No destinationBee")
		return
	}

	fibNumberStr, ok := vars["fibNumber"]
	if !ok {
		BadRequest(responseWriter, "No fibNumber")
		return
	}

	fibNumber, err := strconv.Atoi(fibNumberStr)
	if err != nil {
		BadRequest(responseWriter, "FibNumber must be number")
		return
	}

	message := MessageToBee{
		DestinationBee: destinationBee,
		FibNumber:      fibNumber,
	}
	logger.Trace.Printf("[HTTPReceiver] Message to bee %+v \n", message)

	beeRespond, err := beehive.Sync(context.TODO(), message)
	if err != nil {
		logger.Error.Printf("[HTTPReceiver] %s \n", err.Error())
		http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
		return
	}

	fmt.Fprintf(responseWriter, "%d", beeRespond.(int))
	logger.Trace.Println("[HTTPReceiver] Done sending message to bee")
}
Esempio n. 3
0
func Example_reply() {
	// Create the hello world application and make sure .
	app := beehive.NewApp("hello-world", beehive.Persistent(1))
	// Register the handler for Hello messages.
	app.HandleFunc(HelloReply{}, beehive.RuntimeMap(RcvfReply), RcvfReply)

	// Start the default hive.
	go beehive.Start()
	defer beehive.Stop()

	name := "your name"
	for i := 0; i < 2; i++ {
		// Sync sends the Hello message and waits until it receives the reply.
		res, err := beehive.Sync(context.TODO(), HelloReply{Name: name})
		if err != nil {
			glog.Fatalf("error in sending Hello: %v", err)
		}
		cnt := res.(int)
		fmt.Printf("hello %s (%d)!\n", name, cnt)
	}
}