Esempio n. 1
0
func isLeader() bool {
	me, _ := connector.GetOwnProcess()
	leader := leaderElection.GetLeader()
	return me == leader
}
Esempio n. 2
0
func handleClient(conn net.Conn) {
	for {
		//leader := leaderElection.GetLeader()
		//println("MAIN LEADER: ", leader)
		buf := make([]byte, 4096)
		_, err := conn.Read(buf)
		if err != nil {
			conn.Close()
			//If the client close the connection we get out and start listening again
			break
		}

		//res is where the message is going to be
		var res []string
		string1 := string(buf)
		res = strings.Split(string1, "@")

		if stopFlag {
			if res[0] == "RestoreServer" {
				stopFlag = false
				connector.Stopped = false
			}
			continue

		}
		stringaux := res[1]
		if !strings.Contains(res[0], "Heartbeat") {
			//println("["+time.Now().String()+"]", "RECEIVED: ", res[0])
		}
		//println(" from ", i)
		switch res[0] {
		case "Suspect":
			i, err := strconv.Atoi(stringaux)
			checkError(err)
			handlSuspectChan <- i
		case "Restore":
			i, err := strconv.Atoi(stringaux)
			checkError(err)
			handlRecoveryChan <- i
		case "HeartbeatReply":
			i, err := strconv.Atoi(stringaux)
			checkError(err)
			handlHBReplyChan <- i
		case "HeartbeatRequest":
			i, err := strconv.Atoi(stringaux)
			checkError(err)
			handlHBRequChan <- i
		case "LeaderRequest":
			i, err := strconv.Atoi(stringaux)
			checkError(err)
			handlTrustLeaderChan <- i
		case "Promise":
			handlPromiseLeaderChan <- string1
		case "Prepare":
			inPrepChan <- string1
		case "Accept":
			inAcceptChan <- string1
		case "Learn":
			learnChan <- string1
		case "Value":
			lead := leaderElection.GetLeader()
			if lead == ownProcess {
				valueChan <- string1
			} else {
				connector.Send(string1, lead, nil)
			}
		case "StopServer":
			stopFlag = true
			connector.Stopped = true
		case "Debug":
			debug <- 0
		}
	}

}