Exemplo n.º 1
0
func preSend(message string, pr int) {
	_, err := connector.Send(message, pr, nil)
	if err != nil {
		gotSuspectProc(pr)
	} else {
		gotProcRecov(pr)
	}
}
Exemplo n.º 2
0
func preSend(message string, pr int) {
	if message == "Suspect" || message == "Restore" {
		//If is Suspect or Restore we are going to send the message to ourself to our leader election
		aux := message + "@" + strconv.Itoa(pr)
		connector.Send(aux, ownProcess, nil)

	} else if message == "HeartbeatReply" || message == "HeartbeatRequest" {

		if pr == ownProcess {
			return
		}
		_, err := connector.Send(message, pr, nil)
		if err != nil {
			pSuspect[pr] = true
			preSend("Suspect", pr)
		}
	}

}
Exemplo n.º 3
0
func main() {
	var val, menu, mess string
	var pr, number int
	for {
		fmt.Println("Choose what you want to send")
		fmt.Println("1 : Value")
		fmt.Println("2 : StopServer")
		fmt.Println("3 : RestoreServer")
		fmt.Println("4 : Exit program")
		fmt.Println("5 : 500 values")
		_, err := fmt.Scanln(&menu)
		checkErr(err)
		valmenu, _ := strconv.Atoi(menu)
		number = 1
		switch valmenu {
		case 1:
			fmt.Println("Enter the value to send")
			_, err := fmt.Scanln(&val)
			checkErr(err)
			mess = "Value@" + val
		case 2:
			mess = "StopServer@"
		case 3:
			mess = "RestoreServer@"
		case 4:
			os.Exit(0)
		case 5:
			mess = "Value@"
			number = 500
		}
		fmt.Println("Please select a process between 1 and 3")
		_, err = fmt.Scanln(&pr)
		checkErr(err)
		for i := 1; i <= number; i++ {
			if number > 1 {
				mess = "Value@" + strconv.Itoa(i)
			}
			connector.Send(mess, pr, nil)
			time.Sleep(100 * time.Millisecond)
		}
	}
}
Exemplo n.º 4
0
func preSend(message string, pr int) {
	connector.Send(message, pr, nil)
}
Exemplo n.º 5
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
		}
	}

}