Skip to content

Commit

Permalink
format the codes according to golint
Browse files Browse the repository at this point in the history
add io operation check
  • Loading branch information
chao committed Dec 30, 2018
1 parent e8f52fe commit 30fef7b
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 146 deletions.
23 changes: 19 additions & 4 deletions cmd/broadcast/main.go
Expand Up @@ -12,13 +12,21 @@ import (
"time"
)

// InitCoopCastNode creates a coopcast node
func InitCoopCastNode(confignbr string, configallpeer string, t0 float64, t1 float64, t2 float64, base float64, hop int) *coopcast.Node {
rand.Seed(time.Now().UTC().UnixNano())
config1 := NewConfig()
config1.ReadConfigFile(confignbr)
err := config1.ReadConfigFile(confignbr)
if err != nil {
log.Printf("unable to read config file %v", confignbr)
}
selfPeer, peerList, _ := config1.GetPeerInfo()
config2 := NewConfig()
config2.ReadConfigFile(configallpeer)
err = config2.ReadConfigFile(configallpeer)
if err != nil {
log.Printf("unable to read config file %v", configallpeer)
}

_, _, allPeers := config2.GetPeerInfo()
Cache := make(map[coopcast.HashKey]*coopcast.RaptorQImpl)
SenderCache := make(map[coopcast.HashKey]bool)
Expand All @@ -27,12 +35,19 @@ func InitCoopCastNode(confignbr string, configallpeer string, t0 float64, t1 flo
return &node
}

// InitManyCastNode creates a manycast node
func InitManyCastNode(confignbr string, configallpeer string) *manycast.Node {
config1 := NewConfig()
config1.ReadConfigFile(confignbr)
err := config1.ReadConfigFile(confignbr)
if err != nil {
log.Printf("unable to read config file %v", confignbr)
}
selfPeer, peerList, _ := config1.GetPeerInfo()
config2 := NewConfig()
config2.ReadConfigFile(configallpeer)
err = config2.ReadConfigFile(configallpeer)
if err != nil {
log.Printf("unable to read config file %v", configallpeer)
}
_, _, allPeers := config2.GetPeerInfo()
node := manycast.Node{SelfPeer: selfPeer, PeerList: peerList, AllPeers: allPeers}
return &node
Expand Down
30 changes: 12 additions & 18 deletions cmd/broadcast/utils.go
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bufio"
"encoding/hex"
"fmt"
ida "github.com/harmony-one/libunison/internal/ida/coopcast"
"io"
"log"
Expand All @@ -14,7 +13,7 @@ import (
"time"
)

const PubKeySize int = 20
const pubKeySize int = 20

// Entry is a single config of a node.
type Entry struct {
Expand All @@ -37,6 +36,7 @@ func NewConfig() *Config {
return &config
}

// GetPeerInfo returns the selfPeer, peerList, allPeers from config instance, which used to create node instance
func (config *Config) GetPeerInfo() (ida.Peer, []ida.Peer, []ida.Peer) {
var allPeers []ida.Peer
var peerList []ida.Peer
Expand Down Expand Up @@ -79,16 +79,7 @@ func (config *Config) ReadConfigFile(filename string) error {
return nil
}

func test() {
config := NewConfig()
filename := "./config1.txt"
config.ReadConfigFile(filename)
p1, p2, p3 := config.GetPeerInfo()
fmt.Println("self:", p1)
fmt.Println("nbr:", p2)
fmt.Println("all:", p3)
}

// GenerateConfigFromGraph generate config files from graph config file using adjacent map definition of a graph
func GenerateConfigFromGraph(graphfile string) {
file, err := os.Open(graphfile)
if err != nil {
Expand All @@ -104,15 +95,15 @@ func GenerateConfigFromGraph(graphfile string) {
if err != nil {
log.Printf("not able to convert to number of nodes")
}
pubkeys, tcps, udps := InitConfig(n)
pubkeys, tcps, udps := initConfig(n)

for fscanner.Scan() {
p := strings.Split(fscanner.Text(), " ")
WriteGraphRelationToConfig(p, n, pubkeys, tcps, udps)
writeGraphRelationToConfig(p, n, pubkeys, tcps, udps)
}
}

func InitConfig(n int) (map[int][]byte, []int, []int) {
func initConfig(n int) (map[int][]byte, []int, []int) {
filename := "configs/config_allpeers.txt"
f, err := os.Create(filename)
if err != nil {
Expand All @@ -132,8 +123,11 @@ func InitConfig(n int) (map[int][]byte, []int, []int) {
ts := strconv.Itoa(tcpport)
us := strconv.Itoa(udpport)
line := sid + " 127.0.0.1 " + ts + " " + us + " "
buf := make([]byte, PubKeySize)
rand.Read(buf)
buf := make([]byte, pubKeySize)
_, err := rand.Read(buf)
if err != nil {
log.Printf("unable to create random number")
}
pubkey := hex.EncodeToString(buf)
line = line + pubkey + " 2\n"
tcps[i] = tcpport
Expand All @@ -146,7 +140,7 @@ func InitConfig(n int) (map[int][]byte, []int, []int) {
return pubkeys, tcps, udps
}

func WriteGraphRelationToConfig(p []string, n int, pubkeys map[int][]byte, tcps []int, udps []int) {
func writeGraphRelationToConfig(p []string, n int, pubkeys map[int][]byte, tcps []int, udps []int) {
filename := "configs/config_" + p[0] + ".txt"
f, err := os.Create(filename)
if err != nil {
Expand Down
19 changes: 12 additions & 7 deletions internal/ida/coopcast/interface.go
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
Received byte = 0
metaReceived byte = 0
pubKeySize int = 20
stopBroadCastTime time.Duration = 100 // unit is second
cacheClearInterval time.Duration = 250 // clear cache every xx seconds
Expand All @@ -18,21 +18,25 @@ const (
normalChunkSize int = 100 * 1200
symbolSize int = 1200 // must be multiple of Al(=4) required by RFC6330

HashSize int = 20 // sha1 hash size
Threshold float32 = 0.8 // threshold rate of number of neighors decode message successfully
hashSize int = 20 // sha1 hash size
threshold float32 = 0.8 // threshold rate of number of neighors decode message successfully
)

// Peer represent identification information of a peer node
type Peer struct {
IP string
TCPPort string
UDPPort string
PubKey string
Sid int
}
type HashKey [HashSize]byte

// HashKey is the array of fixed size can be used as key in golang dictionary
type HashKey [hashSize]byte

// Node represents a node using coopcast to send and receive message
type Node struct {
GossipIDA
BroadCaster

SelfPeer Peer
PeerList []Peer
Expand All @@ -49,6 +53,7 @@ type Node struct {
mux sync.Mutex
}

// RaptorQImpl represents raptorQ structure holding necessary information for encoding and decoding message
type RaptorQImpl struct {
Encoder map[int]libraptorq.Encoder
Decoder map[int]libraptorq.Decoder
Expand All @@ -66,8 +71,8 @@ type RaptorQImpl struct {
stats map[int]float64 // for benchmark purpose
}

// IDA broadcast using RaptorQ interface
type GossipIDA interface {
// BroadCaster interface define the broadcast interface for coopcast for both receiver and sender sides
type BroadCaster interface {
BroadCast(msg []byte, pc net.PacketConn) (context.CancelFunc, *RaptorQImpl)
StopBroadCast(cancel context.CancelFunc, raptorq *RaptorQImpl)
ListeningOnBroadCast(pc net.PacketConn)
Expand Down

0 comments on commit 30fef7b

Please sign in to comment.