コード例 #1
0
ファイル: SimPeer.go プロジェクト: FactomProject/factomd
// Non-blocking return value from channel.
func (f *SimPeer) Recieve() (interfaces.IMsg, error) {

	if f.Delayed == nil {
		select {
		case packet, ok := <-f.BroadcastIn:
			if ok {
				f.Delayed = packet
			}
		default:
			return nil, nil // Nothing to do
		}
	}

	now := time.Now().UnixNano() / 1000000

	if f.Delayed != nil && now-f.Delayed.sent > f.Delay {
		data := f.Delayed.data
		f.Delayed = nil
		msg, err := messages.UnmarshalMessage(data)
		if err != nil {
			fmt.Printf("SimPeer ERROR: %s %x %s\n", err.Error(), data[:8], messages.MessageName(data[0]))
		}

		f.bytesIn += len(data)
		f.computeBandwidth()
		return msg, err
	} else {
		// fmt.Println("dddd Delay: ", now-f.Delayed.sent)
	}
	return nil, nil
}
コード例 #2
0
ファイル: p2pProxy.go プロジェクト: FactomProject/factomd
// Non-blocking return value from channel.
func (f *P2PProxy) Recieve() (interfaces.IMsg, error) {
	select {
	case data, ok := <-f.BroadcastIn:
		if ok {
			switch data.(type) {
			case factomMessage:
				fmessage := data.(factomMessage)
				f.trace(fmessage.appHash, fmessage.appType, "P2PProxy.Recieve()", "N")
				msg, err := messages.UnmarshalMessage(fmessage.message)
				if nil == err {
					msg.SetNetworkOrigin(fmessage.peerHash)
				}
				if 1 < f.debugMode {
					f.logMessage(msg, true) // NODE_TALK_FIX
					fmt.Printf(".")
				}
				return msg, err
			default:
				fmt.Printf("Garbage on f.BroadcastIn. %+v", data)
			}
		}
	default:
	}
	return nil, nil
}
コード例 #3
0
ファイル: loadJournal.go プロジェクト: FactomProject/factomd
func LoadJournalFromReader(s interfaces.IState, r *bufio.Reader) {
	s.SetIsReplaying()
	defer s.SetIsDoneReplaying()

	fmt.Println("Replaying Journal")
	time.Sleep(time.Second * 5)
	fmt.Println("GO!")
	t := 0
	p := 0
	for {
		t++
		fmt.Println("total: ", t, " processed: ", p, "            \r")

		// line is empty if no more data
		line, err := r.ReadBytes('\n')
		if len(line) == 0 || err != nil {
			break
		}

		// Get the next word.  If not MsgHex:, then go to next line.
		adv, word, err := bufio.ScanWords(line, true)
		if string(word) != "MsgHex:" {
			continue // Go to next line.
		}
		line = line[adv:] // Remove "MsgHex:" from the line.

		// Remove spaces.
		adv, data, err := bufio.ScanWords(line, true)
		if err != nil {
			fmt.Println(err)
			return
		}

		// Decode the hex
		binary, err := hex.DecodeString(string(data))
		if err != nil {
			fmt.Println(err)
			return
		}

		// Unmarshal the message.
		msg, err := messages.UnmarshalMessage(binary)
		if err != nil {
			fmt.Println(err)
			return
		}

		// Process the message.
		s.InMsgQueue() <- msg
		p++
		if len(s.InMsgQueue()) > 200 {
			for len(s.InMsgQueue()) > 50 {
				time.Sleep(time.Millisecond * 10)
			}
			time.Sleep(time.Millisecond * 100)
		}
	}

	//Waiting for state to process the message queue
	//before we disable "IsDoneReplaying"
	for len(s.InMsgQueue()) > 0 {
		time.Sleep(time.Millisecond * 100)
	}
}