From 1ef101b18b6038761a59fdc38177ab00eb86bca5 Mon Sep 17 00:00:00 2001 From: Fabio Barone Date: Wed, 3 Oct 2018 17:07:53 -0500 Subject: [PATCH] swarm/swap: p2p protocol implementation --- p2p/protocols/protocol.go | 11 +++++++++++ swarm/swap/protocol.go | 1 - swarm/swap/swap.go | 30 ------------------------------ 3 files changed, 11 insertions(+), 31 deletions(-) diff --git a/p2p/protocols/protocol.go b/p2p/protocols/protocol.go index 615f74b569..59115933fc 100644 --- a/p2p/protocols/protocol.go +++ b/p2p/protocols/protocol.go @@ -141,11 +141,22 @@ type Spec struct { // each message must have a single unique data type Messages []interface{} + Services map[string]ProtocolService + initOnce sync.Once codes map[reflect.Type]uint64 types map[uint64]reflect.Type } +type ProtocolService interface { + AddServiceData(data interface{}) + IsSupported(key interface{}) +} + +func (s *Spec) RegisterProtocolService(key string, service ProtocolService) { + s.Services[key] = service +} + func (s *Spec) init() { s.initOnce.Do(func() { s.codes = make(map[reflect.Type]uint64, len(s.Messages)) diff --git a/swarm/swap/protocol.go b/swarm/swap/protocol.go index 452160cd3a..36d5448e66 100644 --- a/swarm/swap/protocol.go +++ b/swarm/swap/protocol.go @@ -81,7 +81,6 @@ type RedeemChequeMsg struct { // SECTION: node.Service interface ///////////////////////////////////////////////////////////////////// func (s *Swap) Start(srv *p2p.Server) error { - s.registerForEvents(srv) log.Debug("Started swap") return nil } diff --git a/swarm/swap/swap.go b/swarm/swap/swap.go index e346ce1706..63886d8ffb 100644 --- a/swarm/swap/swap.go +++ b/swarm/swap/swap.go @@ -163,36 +163,6 @@ func (dpo *DefaultPriceOracle) GetPriceForMsg(event *p2p.PeerEvent) (*big.Int, E return nil, false } -//This swap implementation works by listening to message events on the p2p server. -//It then handles the event received, filtering for messages and evaluating -//if it needs accounting -func (s *Swap) registerForEvents(srv *p2p.Server) { - go func() { - events := make(chan *p2p.PeerEvent) - sub := srv.SubscribeEvents(events) - defer sub.Unsubscribe() - - for { - select { - case event := <-events: - go s.handleMsgEvent(event) - case err := <-sub.Err(): - log.Error(err.Error()) - return - } - } - }() -} - -//Handle the message. -//Determine if it needs accounting, and if yes, account for it -func (s *Swap) handleMsgEvent(event *p2p.PeerEvent) { - if !s.priceOracle.IsAccountedMsg(event) { - return - } - s.accountMsgForPeer(event) -} - //Do the accounting //Depending on the charging type of the message (set in the PriceTag), //it will charge the sender or receiver