func handleCommand(w http.ResponseWriter, r *http.Request) { idAndNumber := r.URL.Path[config.LenCommandPath:] var cmd, serialNumber int fmt.Sscanf(idAndNumber, "%d/%d", &cmd, &serialNumber) if !state.AmILeader() { if state.GetLeader() != nil { str := fmt.Sprintf("http://%s:%d%s%d/%d", state.GetLeader().Ip, state.GetLeader().Port, config.CommandPath, cmd, serialNumber) log.Print(str) http.Redirect(w, r, str, http.StatusSeeOther) } else { http.NotFound(w, r) } return } resp, err := processCommand(cmd, serialNumber) if err != nil { http.NotFound(w, r) } else { fmt.Fprintf(w, "Response: %d\n", resp) } }
func handleHeartBeat(w http.ResponseWriter, r *http.Request) { sender := r.URL.Path[config.LenHeartbeatPath:] log.Print("received heartbeat from ", sender) if state.AmILeader() && config.UniqueId != sender { panic("Got heartbeat from " + sender + ", but I am the leader") } state.HeartbeatChan <- sender }