func (f *P2PProxy) Send(msg interfaces.IMsg) error { f.logMessage(msg, false) // NODE_TALK_FIX data, err := msg.MarshalBinary() if err != nil { fmt.Println("ERROR on Send: ", err) return err } hash := fmt.Sprintf("%x", msg.GetMsgHash().Bytes()) appType := fmt.Sprintf("%d", msg.Type()) message := factomMessage{message: data, peerHash: msg.GetNetworkOrigin(), appHash: hash, appType: appType} switch { case !msg.IsPeer2Peer(): message.peerHash = p2p.BroadcastFlag f.trace(message.appHash, message.appType, "P2PProxy.Send() - BroadcastFlag", "a") case msg.IsPeer2Peer() && 0 == len(message.peerHash): // directed, with no direction of who to send it to message.peerHash = p2p.RandomPeerFlag f.trace(message.appHash, message.appType, "P2PProxy.Send() - RandomPeerFlag", "a") default: f.trace(message.appHash, message.appType, "P2PProxy.Send() - Addressed by hash", "a") } if msg.IsPeer2Peer() && 1 < f.debugMode { fmt.Printf("%s Sending directed to: %s message: %+v\n", time.Now().String(), message.peerHash, msg.String()) } p2p.BlockFreeChannelSend(f.BroadcastOut, message) return nil }
func (p *P2PProxy) logMessage(msg interfaces.IMsg, received bool) { if 2 < p.debugMode { // if constants.DBSTATE_MSG == msg.Type() { // fmt.Printf("AppMsgLogging: \n Type: %s \n Network Origin: %s \n Message: %s", msg.Type(), msg.GetNetworkOrigin(), msg.String()) // } hash := fmt.Sprintf("%x", msg.GetMsgHash().Bytes()) time := time.Now().Unix() ml := messageLog{hash: hash, received: received, time: time, mtype: msg.Type(), target: msg.GetNetworkOrigin()} p2p.BlockFreeChannelSend(p.logging, ml) } }
// manageInChannel takes messages from the network and stuffs it in the f.BroadcastIn channel func (f *P2PProxy) ManageInChannel() { for data := range f.FromNetwork { switch data.(type) { case p2p.Parcel: parcel := data.(p2p.Parcel) f.trace(parcel.Header.AppHash, parcel.Header.AppType, "P2PProxy.ManageInChannel()", "M") message := factomMessage{message: parcel.Payload, peerHash: parcel.Header.TargetPeer, appHash: parcel.Header.AppHash, appType: parcel.Header.AppType} p2p.BlockFreeChannelSend(f.BroadcastIn, message) default: fmt.Printf("Garbage on f.FromNetwork. %+v", data) } } }
// manageOutChannel takes messages from the f.broadcastOut channel and sends them to the network. func (f *P2PProxy) ManageOutChannel() { for data := range f.BroadcastOut { switch data.(type) { case factomMessage: fmessage := data.(factomMessage) // Wrap it in a parcel and send it out channel ToNetwork. parcel := p2p.NewParcel(p2p.CurrentNetwork, fmessage.message) parcel.Header.Type = p2p.TypeMessage parcel.Header.TargetPeer = fmessage.peerHash parcel.Header.AppHash = fmessage.appHash parcel.Header.AppType = fmessage.appType parcel.Trace("P2PProxy.ManageOutChannel()", "b") p2p.BlockFreeChannelSend(f.ToNetwork, *parcel) default: fmt.Printf("Garbage on f.BrodcastOut. %+v", data) } } }
// NODE_TALK_FIX func (p *P2PProxy) stopProxy() { if 0 < p.debugMode { p2p.BlockFreeChannelSend(p.logging, "stop") } }