Пример #1
0
func Peers(fnode *FactomNode) {
	cnt := 0
	for {
		for i := 0; i < 100 && len(fnode.State.APIQueue()) > 0; i++ {
			select {
			case msg := <-fnode.State.APIQueue():
				if msg == nil {
					break
				}
				repeatHash := msg.GetRepeatHash()
				if repeatHash == nil {
					fmt.Println("dddd ERROR!", msg.String())
					break
				}
				cnt++
				msg.SetOrigin(0)
				if fnode.State.Replay.IsTSValid_(constants.NETWORK_REPLAY, repeatHash.Fixed(),
					msg.GetTimestamp(),
					fnode.State.GetTimestamp()) {

					fnode.MLog.add2(fnode, false, fnode.State.FactomNodeName, "API", true, msg)
					if len(fnode.State.InMsgQueue()) < 9000 {
						fnode.State.InMsgQueue() <- msg
					}
				}
			default:

			}
		}

		// Put any broadcasts from our peers into our BroadcastIn queue
		for i, peer := range fnode.Peers {
			for j := 0; j < 100; j++ {

				var msg interfaces.IMsg
				var err error

				if !fnode.State.GetNetStateOff() {
					msg, err = peer.Recieve()
				}

				if msg == nil {
					// Recieve is not blocking; nothing to do, we get a nil.
					break
				}

				cnt++

				if err != nil {
					fmt.Println("ERROR recieving message on", fnode.State.FactomNodeName+":", err)
					break
				}

				msg.SetOrigin(i + 1)
				if fnode.State.Replay.IsTSValid_(constants.NETWORK_REPLAY, msg.GetRepeatHash().Fixed(),
					msg.GetTimestamp(),
					fnode.State.GetTimestamp()) {
					//if state.GetOut() {
					//	fnode.State.Println("In Comming!! ",msg)
					//}
					in := "PeerIn"
					if msg.IsPeer2Peer() {
						in = "P2P In"
					}
					nme := fmt.Sprintf("%s %d", in, i+1)

					fnode.MLog.add2(fnode, false, peer.GetNameTo(), nme, true, msg)

					// Ignore messages if there are too many.
					if len(fnode.State.InMsgQueue()) < 9000 {
						fnode.State.InMsgQueue() <- msg
					}

				} else {
					fnode.MLog.add2(fnode, false, peer.GetNameTo(), "PeerIn", false, msg)
				}
			}
		}
		if cnt == 0 {
			time.Sleep(50 * time.Millisecond)
		}
		cnt = 0
	}
}