Пример #1
0
func (rn *RaftNode) listenToCommitChannels(connch map[int64]chan *fs.Msg, ff *fs.FS, gversion *int) {
	for {
		//fmt.Printf("for begins...!!!\n")
		ci := <-rn.CommitChannel()
		//fmt.Printf("In serve, ci->%v\n", ci)
		rnmsgcc, err := decode(ci.Data)
		if ci.Err != nil {
			connch[rnmsgcc.ClientId] <- &fs.Msg{'P', rnmsgcc.Msg.Filename, rnmsgcc.Msg.Contents, rnmsgcc.Msg.Numbytes, rnmsgcc.Msg.Exptime, rnmsgcc.Msg.Version}
			continue
		}
		if err != nil {
			//	fmt.Printf("In decode, rnmsgcc.err->%v\n", err)
			return
		}

		response := fs.ProcessMsg(rnmsgcc.Msg, ff, gversion)

		_, ok := connch[rnmsgcc.ClientId]

		//fmt.Printf("Received on channel for client -> %v, msg kind -> %v\n", rnmsgcc.ClientId, rnmsgcc.Msg.Kind)
		if ok == true {
			connch[rnmsgcc.ClientId] <- response
		}
		//fmt.Printf("for finishes..!!\n")
	}
}
Пример #2
0
func serve(conn *net.TCPConn, rn RaftNode, clientId int64, connch map[int64]chan *fs.Msg, ff *fs.FS, gversion *int) {
	reader := bufio.NewReader(conn)
	for {
		msg, msgerr, fatalerr := fs.GetMsg(reader)
		if fatalerr != nil || msgerr != nil {
			reply(conn, &fs.Msg{Kind: 'M'}, "")
			conn.Close()
			break
		}

		if msgerr != nil {
			if (!reply(conn, &fs.Msg{Kind: 'M'}, "")) {
				conn.Close()
				break
			}
		}

		//fmt.Printf("Inside serve, msg:%c\n", msg.Kind)
		if msg.Kind != 'r' {
			tempmsg := Rnmsg{clientId, msg}
			rnmsgbytes, err1 := encode(tempmsg)
			if err1 != nil {
				//fmt.Printf("encoding error:%v", err1)
				return
			}
			rn.Append(rnmsgbytes)
			//fmt.Printf("waiting for response for client %v..!!\n", clientId)
			dmsg := <-connch[clientId]
			//response := fs.ProcessMsg(dmsg,ff,gversion)
			//				fmt.Printf("Received on client channel -> %v, msg kind -> %v\n",clientId, dmsg.Kind)
			if dmsg.Kind == 'P' {
				//fmt.Printf("Voted For in serve->%v\n", rn.sm.votedFor)
				for j := 0; j < len(temp.Peers); j++ {
					if temp.Peers[j].Id == rn.sm.votedFor {
						reply(conn, dmsg, temp.Peers[j].ServerAddress)
					}
				}

			} else {
				//fmt.Printf("msg kind not P on client %v\n", clientId)
				//	response := fs.ProcessMsg(dmsg, ff, gversion)
				if !reply(conn, dmsg, "") {
					conn.Close()
					break
				}
			}

		} else {
			response := fs.ProcessMsg(msg, ff, gversion)
			if !reply(conn, response, "") {
				conn.Close()
				break
			}
		}
	}
}