示例#1
0
func main() {
	i, err := strconv.Atoi(os.Args[1])
	checkError(err)
	bftsmartclient.CreateServiceProxy(i, os.Args[2])
	var text string
	fmt.Println("acao(add,remove,size,contains,get,sair) :")
	_, err = fmt.Scanln(&text)
	checkError(err)
	fmt.Println(text)
	fmt.Println("iniciando switch")
	for text != "sair" {
		fmt.Println("dentro switch")
		switch text {
		case "add":
			req := new(bftbench.Request)
			act := bftbench.Request_ADD
			req.Action = &act
			fmt.Println("value : ")
			fmt.Scanln(text)
			i, err := strconv.Atoi(text)
			i32 := int32(i)
			checkError(err)
			req.Value = &i32
			data, err := proto.Marshal(req)
			checkError(err)
			respd := bftsmartclient.InvokeOrdered(data)
			rsp := new(bftbench.Response)
			err = proto.Unmarshal(respd, rsp)
			fmt.Println(rsp.GetBoolResponse())
		case "remove":
			req := new(bftbench.Request)
			act := bftbench.Request_REMOVE
			req.Action = &act
			fmt.Println("value : ")
			fmt.Scanln(text)
			i, err := strconv.Atoi(text)
			i32 := int32(i)
			checkError(err)
			req.Value = &i32
			data, err := proto.Marshal(req)
			checkError(err)
			respd := bftsmartclient.InvokeOrdered(data)
			rsp := new(bftbench.Response)
			err = proto.Unmarshal(respd, rsp)
			fmt.Println(rsp.GetBoolResponse())
		case "size":
			fmt.Println("executando size.")
			req := new(bftbench.Request)
			act := bftbench.Request_SIZE
			req.Action = &act
			data, err := proto.Marshal(req)
			checkError(err)
			fmt.Println("invoke ordered size")
			respd := bftsmartclient.InvokeOrdered(data)
			fmt.Println("invoke ordered finalizado")
			rsp := new(bftbench.Response)
			err = proto.Unmarshal(respd, rsp)
			fmt.Println(rsp.GetIntResponse())
		case "contains":
			req := new(bftbench.Request)
			act := bftbench.Request_CONTAINS
			req.Action = &act
			fmt.Println("value : ")
			fmt.Scanln(text)
			i, err := strconv.Atoi(text)
			i32 := int32(i)
			checkError(err)
			req.Value = &i32
			data, err := proto.Marshal(req)
			checkError(err)
			respd := bftsmartclient.InvokeOrdered(data)
			rsp := new(bftbench.Response)
			err = proto.Unmarshal(respd, rsp)
			fmt.Println(rsp.GetBoolResponse())
		case "get":
			req := new(bftbench.Request)
			act := bftbench.Request_GET
			req.Action = &act
			fmt.Println("value : ")
			fmt.Scanln(text)
			i, err := strconv.Atoi(text)
			i32 := int32(i)
			checkError(err)
			req.Value = &i32
			data, err := proto.Marshal(req)
			checkError(err)
			respd := bftsmartclient.InvokeOrdered(data)
			rsp := new(bftbench.Response)
			err = proto.Unmarshal(respd, rsp)
			fmt.Println(rsp.GetIntResponse())
		}
		fmt.Println("acao(add,remove,size,contains,get,sair) :")
		_, err = fmt.Scanln(&text)
		checkError(err)
	}
	fmt.Println("fim.")
	bftsmartclient.FinalizarJvm()
}
示例#2
0
func (r replica) execute(command []byte) []byte {
	req := new(bftbench.Request)
	err := proto.Unmarshal(command, req)
	checkError(err)
	var res bool
	var achou bool
	rsp := new(bftbench.Response)

	if req.GetAction() == bftbench.Request_ADD {
		val := req.GetValue()
		res = false
		achou = false
		for _, element := range r.state {
			if element == val {
				achou = true
				break
			}
		}
		if achou == false {
			r.state = append(r.state, val)
			res = true
		}
		rsp.BoolResponse = &res
		data, err := proto.Marshal(rsp)
		checkError(err)
		return data
	} else if req.GetAction() == bftbench.Request_GET {
		val := req.GetValue()
		if val >= int32(len(r.state)) {
			val = -1
		} else {
			val = r.state[val]
		}
		rsp.IntResponse = &val
		data, err := proto.Marshal(rsp)
		checkError(err)
		return data
	} else if req.GetAction() == bftbench.Request_REMOVE {
		val := req.GetValue()
		res = false
		achou = false
		for index, element := range r.state {
			if element == val {
				achou = true
				val = int32(index)
				break
			}
		}
		if achou == true {
			r.state = append(r.state[:val], r.state[val+1:]...)
			res = true
		}
		rsp.BoolResponse = &res
		data, err := proto.Marshal(rsp)
		checkError(err)
		return data
	} else if req.GetAction() == bftbench.Request_SIZE {
		val := int32(len(r.state))
		rsp.IntResponse = &val
		data, err := proto.Marshal(rsp)
		checkError(err)
		return data
	} else { // contains
		val := req.GetValue()
		res = false
		achou = false
		for _, element := range r.state {
			if element == val {
				achou = true
				break
			}
		}
		if achou == true {
			res = true
		}
		rsp.BoolResponse = &res
		data, err := proto.Marshal(rsp)
		checkError(err)
		return data
	}
}