@@ -51,12 +51,12 @@ func (n *p2pNode) eachRun(network *Network, wg *sync.WaitGroup, ctx context.Cont
5151 n .alive = true
5252 wg .Done ()
5353
54- for msg := range n . msgQueue {
55- select {
56- case <- ctx . Done ():
57- n . alive = false
58- return
59- default :
54+ select {
55+ case <- ctx . Done ():
56+ n . alive = false
57+ return
58+ default :
59+ for msg := range n . msgQueue {
6060 first := false
6161
6262 n .mu .Lock ()
@@ -117,7 +117,6 @@ func (n *p2pNode) publish(network *Network, msg Message) {
117117 if _ , received := n.recvFrom [content ][edge.targetID ]; received {
118118 continue
119119 }
120- n.sentTo [content ][edge.targetID ] = struct {}{}
121120
122121 willSendEdges = append (willSendEdges , edge )
123122 }
@@ -128,25 +127,52 @@ func (n *p2pNode) publish(network *Network, msg Message) {
128127 })
129128
130129 k := int (float64 (len (willSendEdges )) * network .cfg .GossipFactor )
130+
131131 willSendEdges = willSendEdges [:k ]
132132 }
133133 } else if protocol == Custom {
134+ allEdges := make ([]PeerID , 0 )
135+ for _ , edge := range n .edges {
136+ allEdges = append (allEdges , edge .targetID )
137+ }
134138
139+ sentEdges := make ([]PeerID , 0 )
140+ for targetID := range n .sentTo [content ] {
141+ sentEdges = append (sentEdges , targetID )
142+ }
143+
144+ receivedEdges := make ([]PeerID , 0 )
145+ for senderID := range n .recvFrom [content ] {
146+ receivedEdges = append (receivedEdges , senderID )
147+ }
148+
149+ targets := msg .CustomProtocol (msg , allEdges , sentEdges , receivedEdges , network .cfg .CustomParams )
150+
151+ for _ , targetID := range * targets {
152+ for _ , edge := range n .edges {
153+ if edge .targetID == targetID {
154+ willSendEdges = append (willSendEdges , edge )
155+ break
156+ }
157+ }
158+ }
135159 } else {
136160 return
137161 }
138162
139163 for _ , edge := range willSendEdges {
140164 edgeCopy := edge
165+ n.sentTo [content ][edge.targetID ] = struct {}{}
141166
142167 go func (e p2pEdge ) {
143168 time .Sleep (time .Duration (e .edgeLatency ) * time .Millisecond )
144169
145170 network .nodes [e .targetID ].msgQueue <- Message {
146- From : n .id ,
147- Content : content ,
148- Protocol : protocol ,
149- HopCount : hopCount + 1 ,
171+ From : n .id ,
172+ Content : content ,
173+ Protocol : protocol ,
174+ HopCount : hopCount + 1 ,
175+ CustomProtocol : msg .CustomProtocol ,
150176 }
151177 }(edgeCopy )
152178 }
0 commit comments