From d810811d6f277e78993579bf3778daec233372d7 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Tue, 13 Nov 2018 22:21:20 -0800 Subject: [PATCH] simple ida --- p2p/ida/errors.go | 23 +++++++++++++++++++++++ p2p/ida/ida.go | 34 ++++++++++++++++++++++++++++++---- p2p/ida/interface.go | 1 + 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 p2p/ida/errors.go diff --git a/p2p/ida/errors.go b/p2p/ida/errors.go new file mode 100644 index 000000000..9e25b0c09 --- /dev/null +++ b/p2p/ida/errors.go @@ -0,0 +1,23 @@ +// Copyright (c) 2014, Suryandaru Triandana +// All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Package errors provides common error types used throughout leveldb. +package ida + +import ( + "errors" +) + +// Common errors. +var ( + ErrRaptorImpNotFound = New("raptor implementation: not found") + ErrTimeOut = New("timeout: time's up now") +) + +// New returns an error that formats as the given text. +func New(text string) error { + return errors.New(text) +} diff --git a/p2p/ida/ida.go b/p2p/ida/ida.go index 788e1cbfd..2f15bd1f8 100644 --- a/p2p/ida/ida.go +++ b/p2p/ida/ida.go @@ -1,11 +1,37 @@ package ida -// HarmonyIDA implements IDA interface. -type HarmonyIDA struct { - raptorQImp *RaptorQ +import ( + "time" + + "github.com/simple-rules/harmony-benchmark/p2p" +) + +// IDAImp implements IDA interface. +type IDAImp struct { + raptorQImp RaptorQ } // TakeRaptorQ takes RaptorQ implementation. -func (ida *HarmonyIDA) TakeRaptorQ(raptorQImp *RaptorQ) { +func (ida *IDAImp) TakeRaptorQ(raptorQImp RaptorQ) { ida.raptorQImp = raptorQImp } + +// Process implements very simple IDA logic. +func (ida *IDAImp) Process(msg Message, peers []p2p.Peer, done chan struct{}, timeout time.Duration) error { + if ida.raptorQImp == nil { + return ErrRaptorImpNotFound + } + chunkStream := ida.raptorQImp.Process(msg) + id := 0 + for { + select { + case <-done: + return nil + case <-time.After(timeout): + return ErrTimeOut + case chunk := <-chunkStream: + p2p.SendMessage(peers[id], chunk) + id++ + } + } +} diff --git a/p2p/ida/interface.go b/p2p/ida/interface.go index a1b6d9e67..99b0023e0 100644 --- a/p2p/ida/interface.go +++ b/p2p/ida/interface.go @@ -18,5 +18,6 @@ type RaptorQ interface { // IDA interface. type IDA interface { + TakeRaptorQ(raptorQImp *RaptorQ) Process(msg Message, peers []p2p.Peer, timeout int) }