func TestFakeSwitchCenter(t *testing.T) { printIndicate() groups := make(map[uint64]bool) groups[uint64(1)] = true groups[uint64(2)] = true groups[uint64(3)] = true fcenter := NewFakeSwitchCenter(groups) assert(nil != fcenter) for id, _ := range groups { fswitch := fcenter.Get(id) assert(nil != fswitch) assert(fswitch.id == id) } go fcenter.Run() defer fcenter.Stop() msg := pb.Message{From: 1, To: 2} fswitch := fcenter.Get(1) fswitch.GetSendChan() <- msg rmsg := <-fcenter.Get(2).GetRecvChan() assert(rmsg.From == 1) assert(rmsg.To == 2) msg.From = 3 msg.To = 1 fcenter.Get(3).GetSendChan() <- msg rmsg = <-fcenter.Get(1).GetRecvChan() assert(rmsg.From == 3) assert(rmsg.To == 1) }
func (sp *spaxos) doTryCatchUp(msg pb.Message) { assert(sp.id == msg.To) const catchUpStep = 10 assert(sp.minIndex <= sp.maxIndex) for i := 0; i < catchUpStep; i++ { catchIndex := sp.minIndex + uint64(i+1) if catchIndex > sp.maxIndex { break } if _, ok := sp.insgroup[catchIndex]; ok { continue } // catchIndex don't have spaxos instance yet catchIns := sp.getSpaxosInstance(catchIndex) assert(nil != catchIns) // => create a new spaxos instance for catchIndex msg.Index = catchIndex catchIns.step(sp, msg) } }
func (sw *SSwitch) handleNetworkConnection(conn net.Conn) { defer conn.Close() reader := bufio.NewReader(conn) assert(nil != reader) for { // err := conn.SetReadDeadline(time.Now().Add(2 * time.Millisecond)) // if nil != err { // LogErr("%s conn.SetReadDeadline conn %s err %s", // GetCurrentFuncName(), conn, err) // return // } var pkglen int var err error { var totalLen uint32 err = binary.Read(reader, binary.BigEndian, &totalLen) if nil != err { LogErr("%s binary.Read err %s", GetCurrentFuncName(), err) return } pkglen = int(totalLen) assert(0 <= pkglen) } var pkg []byte for len(pkg) < pkglen { value := make([]byte, pkglen-len(pkg)) readlen, err := reader.Read(value) if nil != err { LogErr("%s binary.Read pkglen %d err %s", GetCurrentFuncName(), pkglen, err) return } assert(0 < readlen) pkg = append(pkg, value...) } assert(pkglen == len(pkg)) msg := pb.Message{} err = msg.Unmarshal(pkg) if nil != err { LogErr("%s pb.Message.Unmarshal err %s", GetCurrentFuncName(), err) return } if _, ok := sw.peers[msg.From]; !ok || sw.id != msg.To { LogDebug("%s ignore msg %v", GetCurrentFuncName(), msg) continue } LogDebug("net switch hostid %d recv msg (type %s) %v", sw.id, pb.MessageType_name[int32(msg.Type)], msg) // feed msg into node select { case sw.nrecvc <- msg: case <-sw.done: return } } }