예제 #1
0
파일: KVClient.go 프로젝트: kalloldey/Raft
func (kvc *KVC) Get(key []byte) ([]byte, int) {
	cmd := new(comm.SMCommand)
	cmd.Command = "GET"
	cmd.Key = key
	cmd.Value = nil
	cmd.UserId = kvc.MyPid
	cmd.RequestId = kvc.ReqId //koi bhi random number
	kvc.PingTo = kvc.PingTo + 1
	kvc.Server.Outbox() <- &comm.Envelope{Pid: kvc.PingTo % kvc.TotalServer, MsgId: 4, Msg: cmd}
	for {
		select {
		case <-time.After(time.Duration(kvc.SlaTimeOut) * time.Millisecond):
			{
				//fmt.Println("Get time out")
				return nil, 1
			}
		case val := <-kvc.Server.Inbox():
			{
				switch val.Msg.(type) {
				case comm.SMResp:
					res := val.Msg.(comm.SMResp)
					if res.Error == 1 {
						//fmt.Println("Get not found")
						if res.Value != nil {
							//fmt.Println("Response value: ", string(res.Value))
						}
						return nil, 1
					} else {
						//fmt.Println("Get Found")
						return res.Value, 0
					}
				}
			}
		}
	}
}
예제 #2
0
파일: KVClient.go 프로젝트: kalloldey/Raft
func (kvc *KVC) Put(key []byte, value []byte) int {

	cmd := new(comm.SMCommand)
	cmd.Command = "PUT"
	cmd.Key = key
	cmd.Value = value
	cmd.UserId = kvc.MyPid
	cmd.RequestId = kvc.ReqId // or koi bhi random number
	kvc.PingTo = kvc.PingTo + 1
	sendto := kvc.PingTo % kvc.TotalServer
	kvc.Server.Outbox() <- &comm.Envelope{Pid: sendto, MsgId: 4, Msg: cmd}
	for {
		var res comm.SMResp
		select {
		case <-time.After(time.Duration(kvc.SlaTimeOut) * time.Millisecond):
			{
				fmt.Println("[put]Time Up")
				return 1
			}
		case val := <-kvc.Server.Inbox():
			{
				switch val.Msg.(type) {
				case comm.SMResp:
					res = val.Msg.(comm.SMResp)
					if res.Error == 1 {
						fmt.Println(cmd.RequestId, "Put Bad Response: ", string(res.Value))
						return 1
					} else {
						fmt.Println(cmd.RequestId, "Put Good Resp")
						return 0
					}
				}
			}
		}
	}
}